Alignment assumptions


The attached code segfaults with clang 3.8 and 4.0:

$ clang++ -g -O3 -mavx main.cpp A.cpp ; ./a.out
Segmentation fault

The problem is in the following line:

     memset((__m256i*)dest, 0, length);

Removing the cast to __m256i* solves the problem. It seems that clang assumes that __m256i* is always aligned correctly, which is not the case in the code. Is that a bug in clang, or is that a mistake on my side? GCC does not cause a segfault.

Best regards,

A.h (168 Bytes)

main.cpp (55 Bytes)

A.cpp (145 Bytes)

Intel documents these types as follows:

“The compiler aligns the __m256, __m256d, and __m256i local and global data to 32-byte boundaries on the stack”

That’s awkwardly worded, but these types have 32B alignment; the result of casting a pointer that is not suitably aligned is undefined (i.e this is a bug in your code).

– Steve