Skip to content

Comments

Fix floats on Wasm#169

Merged
jgonet merged 1 commit intoswmfrom
jgonet/fix-floats
Dec 17, 2025
Merged

Fix floats on Wasm#169
jgonet merged 1 commit intoswmfrom
jgonet/fix-floats

Conversation

@jgonet
Copy link
Collaborator

@jgonet jgonet commented Dec 17, 2025

On wasm32, terms are aligned by 4 bytes. memory_heap_allocation returns a pointer for n terms. We used float_term_t to create an union between a term and float. However, floats are aligned to 8 bytes which made casting between union and term pointers unsafe.

We replaced union usage to memcpy which allows unaligned memory access. The true solution would be to return correctly aligned memory from memory_heap_alloc but fix for that would need a lot more testing.

On wasm32, terms are aligned by 4 bytes. `memory_heap_allocation` returns a pointer for `n` terms.
We used float_term_t to create an union between a term and float. However, floats are aligned to 8 bytes which made
casting between union and term pointers unsafe.

We replaced union usage to `memcpy` which allows unaligned memory access. The true solution would be to return
correctly aligned memory from memory_heap_alloc but fix for that would need a lot more testing.

Signed-off-by: Jakub Gonet <jakub.gonet@swmansion.com>
@jgonet jgonet merged commit 5ebc4cf into swm Dec 17, 2025
2 checks passed
@jgonet jgonet deleted the jgonet/fix-floats branch December 17, 2025 15:48
@jgonet
Copy link
Collaborator Author

jgonet commented Dec 17, 2025

Upstream here: atomvm/AtomVM#2041

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants