You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
core: add alignof<T>(), the alignment of a type, beside sizeof<T>() #4575
Vx has sizeof<T>() but no alignof<T>(). Code that needs the alignment of a type must measure it.
PR #4565 adds std::alloc::heap_align<T>() for Vec, Box and the other containers. A <4 x f64> needs an address that is a multiple of 32, and malloc gives 16. heap_align measures the alignment of T with a helper struct:
struct AlignProbe<T> { byte : u8, value : T, }
// value starts at the first multiple of the alignment of T after byte.
let align = sizeof<AlignProbe<T>>() - sizeof<T>();
This works for every type that sizeof knows, but it is indirect. core::mem has no align_of for the same reason (see the list of missing functions in stdlib/core/mem.vx). docs/implementation_plans/heap_memory_and_c_functions.md lists "alignof<T>() for Vec" as open.
Request
Add an alignof<T>() intrinsic beside sizeof<T>(), in both code generators. It gives the alignment from the data layout, as sizeof gives the size. Then:
heap_align<T>() uses it instead of AlignProbe.
core::mem gets align_of<T>().
A test checks alignof for a number, a <4 x f64>, a struct with a vector field and a struct of bytes, with both code generators.
Activity