Memory layout in Zig with formulas
raymondtana.github.io
Memory layout in Zig with formulas
1–10 of 30 posts
Re: Memory layout in Zig with formulas
#2Wrote this blog post to summarize what I think are the right ways to understand alignment and size for various data types in Zig, just through experimentation.
Let me know any and all feedback!
Re: Memory layout in Zig with formulas
#3I've been learning Zig, and needed a refresher on memory layout (@sizeOf and @alignOf). Wrote this blog post to summarize what I think are the right ways to understand alignment and size for various data types in Zig, just through experimentation. Let me know any and all feedback!
Re: Memory layout in Zig with formulas
#4I've been learning Zig, and needed a refresher on memory layout (@sizeOf and @alignOf). Wrote this blog post to summarize what I think are the right ways to understand alignment and size for various data types in Zig, just through experimentation. Let me know any and all feedback!
i could be wrong but i believe the zig compiler reserves the right to lay things out differently depending on compilation mode? especially debug. unless it's extern or packed, in which case the layout will be defined.
if you need a well defined layout, use `extern`. if your struct makes sense to represent as an integer, use `packed`. I think it is often ill advisable to use `packed` otherwise.
you can explore this yourself on the Type info returned from @TypeInfo(T):
https://ziglang.org/documentation/master/std/#std.builtin.Ty...
https://ziglang.org/documentation/master/std/#std.builtin.Ty...
https://ziglang.org/documentation/master/std/#std.builtin.Ty...
Re: Memory layout in Zig with formulas
#5Earlier quoted context omitted.
i could be wrong but i believe the zig compiler reserves the right to lay things out differently depending on compilation mode? especially debug. unless it's extern or packed, in which case the layout will be defined.
`extern` and `packed` container types have well defined layouts. a regular `struct` is an "auto" layout - and the compiler can and will rearrange whenever it wants. if you need a well defined layout, use `extern`. if your struct makes sense to represent as an integer, use `packed`. I think it is often ill advisable to use `packed` otherwise. you can explore this yourself on the Type info returned from @TypeInfo(T): h…
> An extern struct has in-memory layout matching the C ABI for the target.
Zig is really good at speaking the C ABI of the target, but the upshot seems to be that it appears there is no stable Zig-native ABI.
If I'm correct, I wonder if there are plans to settle on a stable ABI at some point in the future. I do know that in other languages the lack of a stable ABI is brought up as a downside, and although I've been burned by C++ ABI stability too many times to agree, I can understand why people would want one.
Re: Memory layout in Zig with formulas
#6Re: Memory layout in Zig with formulas
#7Earlier quoted context omitted.
`extern` and `packed` container types have well defined layouts. a regular `struct` is an "auto" layout - and the compiler can and will rearrange whenever it wants. if you need a well defined layout, use `extern`. if your struct makes sense to represent as an integer, use `packed`. I think it is often ill advisable to use `packed` otherwise. you can explore this yourself on the Type info returned from @TypeInfo(T): h…
To wit: https://ziglang.org/documentation/master/#extern-struct > An extern struct has in-memory layout matching the C ABI for the target. Zig is really good at speaking the C ABI of the target, but the upshot seems to be that it appears there is no stable Zig-native ABI. If I'm correct, I wonder if there are plans to settle on a stable ABI at some point in the future. I do know that in other languages the lack of a…
Heres the kind of code it generates https://zigbin.io/6dba68
It can also generate javascript, heres doom running on browser: https://cloudef.pw/sorvi/#doom.wasm
Re: Memory layout in Zig with formulas
#8Earlier quoted context omitted.
`extern` and `packed` container types have well defined layouts. a regular `struct` is an "auto" layout - and the compiler can and will rearrange whenever it wants. if you need a well defined layout, use `extern`. if your struct makes sense to represent as an integer, use `packed`. I think it is often ill advisable to use `packed` otherwise. you can explore this yourself on the Type info returned from @TypeInfo(T): h…
To wit: https://ziglang.org/documentation/master/#extern-struct > An extern struct has in-memory layout matching the C ABI for the target. Zig is really good at speaking the C ABI of the target, but the upshot seems to be that it appears there is no stable Zig-native ABI. If I'm correct, I wonder if there are plans to settle on a stable ABI at some point in the future. I do know that in other languages the lack of a…
Re: Memory layout in Zig with formulas
#9 struct Dang : bits 64 // 64 bits wide, int total
{
foo : bits 5 @ 0; // 5 bits wide at bit offset 0
bar : bits 5 @ 0;
baz : bits 16 @ 4; // 16 bits wide at bit offset 4
tom : bits 11 @ 32;
};Re: Memory layout in Zig with formulas
#10I know this is a bit cursed; but, I always wanted a bitfield-on-steroids construct: struct Dang : bits 64 // 64 bits wide, int total { foo : bits 5 @ 0; // 5 bits wide at bit offset 0 bar : bits 5 @ 0; baz : bits 16 @ 4; // 16 bits wide at bit offset 4 tom : bits 11 @ 32; };