• It could be that getAddressOf(var,true) does not properly do what its described it does?

    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
    https://github.com/espruino/Espruino/blob/5f6ad65e8c8bd51a2c1be17fe68ee162ce4e8c88/src/jsvar.c#L1745

    This is also the case for your array

    >var f=new Float32Array(1)
    =new Float32Array(1)
    >E.getAddressOf(f.buffer,true)
    =536895915
    >peek32(536895915)
    =0
    >f[0]=0.5
    =0.5
    >peek32(536895915).toString(16)
    ="3f000000"
    >f[0]=0.25
    =0.25
    >peek32(536895915).toString(16)
    ="3e800000"
    >trace(f)
    #832[r1,l1] Float32Array (offs 0, len 1)  #828[r1,l0] ArrayBuffer (offs 0, len 4)    #844[r1,l0] String [1 blocks] "\0\0\x80>"
    =undefined
    

    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.

    see also https://stackoverflow.com/questions/63834136/how-to-avoid-unaligned-access-exceptions-with-float-on-cortex-m4

  • 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 test.boot.js.

    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...

    >let f = new Float32Array(1);
    =new Float32Array(1)
    >f[0] = 0.5
    =0.5
    >E.getAddressOf(f.buffer,true)
    =195582
    >peek32(E.getAddressOf(f.buffer,true)).toString(16)
    ="4"
    

    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 :

    >trace(f)
    #4452[r1,l1] Float32Array (offs 0, len 1)  #5210[r1,l0] ArrayBuffer (offs 0, len 4)    #2358[r1,l0] String [1 blocks] "\0>\x1CF"
    =undefined
    >peek32(r).toString(16)
    ="461c3e00"
    >f[0]
    =9999.5
    
    

    Its using 24 bits, 0x46, 0x1c,0x3e. Yet 1 block? How is that possible?
    process.memory().blocksize prints 14


    E.getSizeOf() for the flat string was 4. ( One block for the Float32Array view, One block for the ArrayBuffer view, 2 block for the backing flatstring data )

    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?

About

Avatar for d3nd3-o0 @d3nd3-o0 started