inlineC float byVal & byRef working example #6535
Replies: 1 comment
-
Posted at 2023-12-22 by d3nd3-o0 This code is a simpler layout of the above by reference. It could be there is an error in the wrapper stuff in cFloat object. This mb works? Not sure yet.
Edit: Yes this works, there is something wrong with the cFloat wrapper function I created. Yet I dont know what it is. So good news for the float ref working I guess.
Posted at 2023-12-22 by d3nd3-o0 So when I wrap stuff in functions, it crashes, when the code is not inside a function call, it works. This must be a bug...? Nevermind, so when I save this as a file on the watch its self. It doesn't have an issue running it. Why is this? I always get confused with the Posted at 2023-12-23 by @fanoush did not have time to test your code but in general there is garbage collector and memory defragmentation done from time to time so the address of variables in memory may change, also the Posted at 2023-12-23 by d3nd3-o0 Thanks for that heads up. I have tested that the return value from E.getAddressOf is not 0, and I still have to upload to file for it to work. Mb its related to that old post I made about the interpreter uploading line by line idk, I'm trying to think how it can go wrong here, its essentially a call to Btw: Whats really annoying/strange is that if you add some random lines of code, like eg. adding Posted at 2023-12-23 by @fanoush Try to use only something made from flat string for E.getAddressOf. it can crash because of bad address (=stale pointer corrupting different data) or maybe it can be unaligned address, flat strings are aligned to 32 bits, other data may not be. Posted at 2023-12-23 by d3nd3-o0 Good news, I got it working by forcing to Flat string!!! You solved it! Initial post updated with fixed code. Posted at 2023-12-23 by d3nd3-o0 It could be that Mb its wrong to assume that all Int32Array etc are flat, cos if you give small length, its not?
Unless I am missing some information about flat array buffers, perhaps it does not refer to the underlying buffer... That is why I didn't think about flatstring being the culprit for so longer, was so happy checking the return value of E.getAddressOf(). Perhaps it only applies to strings?
The reason it was important for this to work with Posted at 2023-12-23 by @fanoush
Maybe. It returns address if it makes sense = data is stored in one block, even normal short string can have the data in one block This is also the case for your array
It can be seen the float array is backed by short string that takes up single block so it is makes sense to get the address of it. However you can see that in my case the address is not aligned (ends with 5) so reading or writing float value from/to FPU register with such pointer will crash on unaligned memory access. Posted at 2023-12-23 by d3nd3-o0 That is interesting information. So my theory about it working from storage isn't so complete. I further compared upload as "test.js" and used load("test.js") to call it. It doesnt' work there, the example where it works is when I load the file by renaming it to I checked the alignment of the address, and it seems related to what you describe. Somehow code loaded at boot.js stage is more aligned? crazy. Unrelated...
This is similar to your code above, but in the Emulator. I get weird output always "4". Also this might sound stupid, but how can it store the float in 1 block(16bits). I print the float with :
Its using 24 bits, 0x46, 0x1c,0x3e. Yet 1 block? How is that possible?
for the normal string backed example, it was 3. ( One block for the Flaot32Array view, One block for the ArrayBuffer view, 1 block for the backing string data ). Is my understanding correct of the above? Posted at 2023-12-24 by d3nd3-o0
I can now confirm that alignment is the thing that affect all cases. I was just lucky before. so
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2023-12-22 by d3nd3-o0
It took me a while to get working, so thought I'd share what worked. Can be useful for off-loading some heavy duty calculations to c, to increase throughput.
Edit: Getting the ref version to work was extremely time-consuming because of some strange crash of reading the variables directly + performing floating math on them. Yet copying the inputs off of the stack seems to solve it. Also it is necessary to write to the pointers in int form ,not float.
Edit: Fixed the crashes by forcing flat string. (cos alignment)
Beta Was this translation helpful? Give feedback.
All reactions