I currently abuse a union, but I’m wondering if I’m missing something easier.
Thanks.
const PixmapEntryType = extern union {
u: u32,
a: [4]u8,
v: @Vector(4, u8),
};
I currently abuse a union, but I’m wondering if I’m missing something easier.
Thanks.
const PixmapEntryType = extern union {
u: u32,
a: [4]u8,
v: @Vector(4, u8),
};
What about this are you finding difficult?
Apparently only the @Vector(4, u8) → u32 assignment requires a bunch of machinery–probably reasonable given that it would depend on endianness.
Interestingly, once I poked at it, an assignment from @Vector(4, u8) to [4]u8 works just fine without any casts.
const std = @import("std");
test "foo" {
const v: @Vector(4, u8) = @splat(1);
const a: [4]u8 = v;
const u_from_v: u32 = @bitCast(v);
const u_from_a: u32 = @bitCast(a);
std.debug.assert(u_from_v == u_from_a);
}
Ah, right, @bitCast. That’s what I was looking for.
Thanks.
Be careful to not use @bitCast on @Vector’s with integers other than u8,u16,u32,u64. Currently it behaves differently on llvm and x86_64 backends Miscompilation when bitcasting numbers to vector in x86-64 backend · Issue #23851 · ziglang/zig · GitHub