galinette
galinette

Reputation: 9292

x86_64 SSE alignment : differences between GCC and Clang

I have a large codebase using SSE intrinsics extensively, that has been developped under GCC for the x86_64 platform only. There are a lot of __m128 and float[4] allocated on the stack, which are always aligned to 16-byte when compiling with GCC on x86_64.

We are testing clang, and it crashes on misaligned SSE loads and stores, on the stack variables.

It seems I can fix all errors one by one by us __attribute__ ((aligned(16)))

Is there any way to force clang to align all variables to 16 bytes globally? I can't find any in the documentation.

Upvotes: 2

Views: 972

Answers (1)

Paul R
Paul R

Reputation: 213060

__m128 local variables should be 16 byte aligned with any compiler and should not need any additional work. float[4] only has 4 byte alignment so you will need to add suitable directives for these. Most people use a macro for this, e.g.

#ifdef _MSC_VER
  // MSVC...
  #define ALIGN(n) declspec(align(n))
#else
  // the civilised world...
  #define ALIGN(n) __attribute__ ((aligned(n)))
#endif

and then declare variables as e.g.:

ALIGN(16) float[4] my_floats;

Upvotes: 4

Related Questions