Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/libAtomVM/term.h
Original file line number Diff line number Diff line change
Expand Up @@ -2396,16 +2396,18 @@ static inline term term_from_float(avm_float_t f, Heap *heap)
term *boxed_value = memory_heap_alloc(heap, FLOAT_SIZE);
boxed_value[0] = ((FLOAT_SIZE - 1) << 6) | TERM_BOXED_FLOAT;

float_term_t *boxed_float = (float_term_t *) (boxed_value + 1);
boxed_float->f = f;

// For wasm32, alignof(avm_float_t) = 8 and alignof(term) = 4
Copy link
Collaborator

@bettio bettio Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is somehow slightly misleading, this is not just an issue for wasm32.
It should be rephrased as:
// alignof(avm_float_t) might be != alignof(term)

// `memory_heap_alloc` returns memory aligned to term size.
memcpy(boxed_value + 1, &f, sizeof(avm_float_t));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, but sadly there is a know ESP32 GCC bug that I opened 2 years ago: https://gcc.gnu.org/pipermail/gcc-bugs/2023-October/839353.html

Can you keep also the old code and use it with such a similar macro?

#if defined(__XTENSA__)
// See https://gcc.gnu.org/pipermail/gcc-bugs/2023-October/839353.html
[...]
#else
// your new code here
#endif

return ((term) boxed_value) | TERM_PRIMARY_BOXED;
}

static inline avm_float_t term_to_float(term t)
{
const float_term_t *boxed_float = (float_term_t *) (term_to_const_term_ptr(t) + 1);
return boxed_float->f;
avm_float_t result;
// see `term_from_float`
memcpy(&result, term_to_const_term_ptr(t) + 1, sizeof(avm_float_t));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the previous comment.

return result;
}

static inline bool term_is_number(term t)
Expand Down
Loading