I'm using SSE/AVX and I need to store aligned data. how ever, my data can be of different types. so I use a union like this
union Data
{
bool Bools[128];
int32 Ints32[128];
int64 Ints64[128];
// ... other data types
} data;
Can I do the following?
union alignas(16) Data
{
alignas(4) bool Bools[128];
alignas(4) int32 Ints32[128];
alignas(8) int64 Ints64[128];
alignas(16) Bar Bars[128]; // Bar is 16 bytes
} data;
so I expect Ints32
and Bool
elements to be aligned as 4 bytes, yet Int64
elements are aligned as 8 bytes.
because of Bar
first element of each array (or basically &data
) should also be aligned to 16 bytes. but elements of each array should be aligned as stated. so is my union correct?