Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to use alignas(16) for an array[8] of m128?

This is the code I usually write:

alignas(16) __m128 myBuffer[8];

But maybe (since the object-array is 128*8 bit = 128 byte) I should write:

alignas(128) __m128 myBuffer[8];

Or, "since the first 16 byte are aligned" in the first example, the rest will be automatically aligned in memory?

like image 976
markzzz Avatar asked Nov 25 '25 07:11

markzzz


2 Answers

128-bit SIMD data types must be aligned on 16 bytes. The compiler already knows that, so it's not needed to align __m128 manually. See for example, MSVC docs on __m128:

Variables of type __m128 are automatically aligned on 16-byte boundaries.

Also, arrays are contiguous. Each array element is naturally aligned the same as the first one. There's no need to multiply the alignment for arrays.

So you need not bother with alignas at all:

__m128 myBuffer[8];

This will just work.

like image 79
rustyx Avatar answered Nov 26 '25 21:11

rustyx


is correct to use alignas(16) for an array[8] of m128?

It's not technically wrong, but according to documentation __m128 is aligned to 16 bytes, so it's unnecessary to use alignas.

But maybe ... I should write: alignas(128)

If your goal is to align to 128 bytes, then you can achieve it like that.

Or "since the first 16 byte are aligned" in the first example, the rest will be automatically aligned in memory?

This question confuses me. If the integer is aligned, then the first byte is aligned. Rest of the bytes are offset from the first byte. In case the size is larger than alignment, some of the subsequent bytes would be aligned at offsets equal to the alignment.


Note that there is no __m128 in standard C++.

like image 29
eerorika Avatar answered Nov 26 '25 21:11

eerorika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!