#Add padding within packed structs?

1 messages · Page 1 of 1 (latest)

scenic willow
#

I want something like:

pub const PaddedAndPacked = struct {
  foo: bool,
  bar: bool,

  // 3 byte padding
  padding: [3]u8 = .{0} ** 3;
};

However, this won't compile since packed structs cannot contain fields of type [_]T.
Whats the best way to pad here?

I know I could do u24 in this case,
but that won't really work for padding values greater than 128 bytes.

safe valve
#

Use extern instead of packed in this case.

#

Since packed structs are just compiled to variable size integers.

#

There's also an awful hack where you can create an integer of the size of the padding to use as a field padding = std.meta.Int(.unsigned, 3 * 8) not a great idea though.

obtuse vessel
scenic willow
short egret
#

which is: "zig with bigger ints are less functional after u128"

#

really after u512 when you use 512 simd capable machines

#

ZIg has arbitrary big numbers up to u65535 (u16 max value)

quaint wadi
#

u128 is, last i checked, the highest you can properly use without llvm exploding

#

after that it fucks up some arithmetic (i don't remember if the compile fails or if it breaks at runtime)

short egret
#

but you cannot multiply and divide

#

u128 is the lst that can multiply and divide unless your pc can handle bigger integers

#

(sometimes)

quaint wadi
#

ahh okay

#

i knew llvm failed to do something past u128

short egret
#

yeah, you can use it for storage and adding numbers

#

after that you might wanna do some algorithms for handling those big numbers till zig can polyfill for those numbers (or llvm can do it)