3

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?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
markzzz
  • 47,390
  • 120
  • 299
  • 507

2 Answers2

2

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.

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • so is it correct to use `alignas(4) std::array mSamples`? – markzzz Sep 03 '21 at 07:48
  • @markzzz `float` is in most cases already naturally aligned on 4 bytes, so `alignas(4)` here would make no difference. If you want to perform SIMD operations over this array, then you should use `alignas(16)` (or 32 when targeting AVX2 or later). – rustyx Sep 03 '21 at 10:48
  • thats the question: why alignas(16) will differs from alignas(4) if they are consecutive? – markzzz Sep 03 '21 at 11:20
1

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++.

eerorika
  • 232,697
  • 12
  • 197
  • 326