#Non-regular sized integers

1 messages · Page 1 of 1 (latest)

pale oyster
#

Hello. I have a few questions regarding types like uN for N different than 8, 16, 32 or 64. I identified two cases.

  1. N is 1, 2, or 4.

Let's take u2 as an example. I guess the alignment is 1 byte? Or can it be 1/4 byte = 2 bits? If I have an array of m elements of type u2, is it going to be stored using 2m bits or 8m bits = m bytes? Is there a special optimization if m is a multiple of 4?

  1. N is not a power of 2.

I honestly don't see the point here. Why would I ever use something like u51 instead of u64? However, I have noticed a few u29 in the standard library. So I guess there is a use case.

thick shell
#

regarding 2: packed structs from some specs can have some weird field sizes
packed struct(u64) { a: u51, b: u3, c: u10 }

pale oyster
#

Ohh I see. Like if i wanted to implement my own version of ieee floating point numbers?
packed struct(u32) { sign: u1, mantissa: u23, exponent: u8 }

woven cypress
#

Not exactly, the same question.... but informative nonetheless

pale oyster
#

From that thread, I understand there is no packing of u2 values in a array. So they are basically u8 in disguise.

finite river
#

There’s an array type in std that packs them more tightly