Comment by ozgrakkurt

1 day ago

Thanks for the context, but what I am criticising is this part:

> it became allowed to use @bitCast to reinterpret a [3]u8 as a u24

This cant't make sense unless u24 is defined to be 24bits in the first place. It is just silly to allow something like this. It would make so much more sense to me if they started disallowing this or just even print a deprecation notice for it for one release version.

> Useful for larger integers than int16_t while saving 25% over a 32-bit value

You can't even do []u24 in zig as far as I can remember and understand anyway so this is only happening in a packed struct context.

C doesn't mandate padding but C compilers allow having pointers and arrays of irregular _BitInt types as far as I can understand.

In this [1] document, in Abi considerations section, it writes that it is defined to have next-power-of-two layout size.

Also here (for RISCV) [2] it seems like it is defined with next-power-of-two layout.

Also the document here (for x86_64) defines it similarly [3]

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2709.pdf

[2] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/...

[3] https://gitlab.com/x86-psABIs/x86-64-ABI/-/tree/master?ref_t...

> This cant't make sense unless u24 is defined to be 24bits in the first place

It's worth remembering that zig is a ~hll that should be platform agnostic. suppose someone built a byte-chip with a 24 bit word. the "new" zig way of doing things will be more portable and slot right in, and support 32- and 16-bit datatypes just fine.

I sort of agree ... bit casting from an N width integer into an array of ... woah ... that's too far. It's bitcast not byte-cast which has an implied reinterpretation on a same or smaller word size in the cpu.

Once you see that the fact somebody has a u24 in their code is between them and the compiler alone.

As others probably noted byte casting (keeping the same endianess) is what unions are for.