I have 3 unsigned ints with range [0, 255]. I want to store these 3 numbers to a compact storage and since this operation happens too often I want to know how I can improve it.
Initially I tried this:
struct Foo {
uint8_t x;
uint8_t y;
uint8_t z;
};
Foo arr[] = ...;
void pushBack(unsigned x, unsigned y, unsigned z) {
arr[count].x = x;
arr[count].y = y;
arr[count++].z = z;
}
The results were not good so I tried this instead:
struct Foo {
union {
struct {
uint8_t x;
uint8_t y;
uint8_t z;
uint8_t pad_;
} v;
uint32_t u32;
};
};
Foo arr[] = ...;
void pushBack(unsigned x, unsigned y, unsigned z) {
arr[count++].u32 = (z << 16) | (y << 8) | x;
}
And the results improved quite a bit.
I wonder if there is a way to improve this even more with SSE and/or AVX instructions.