Monday, March 23, 2009

Diving Into SSE - Alignment Issues

I noticed some odd behaviour, or at least I thought it was, when using SSE intrinsics in my vst plugin tests:

Using some of my classes would crash the host, while others would not, and looking at the code of the classes gave little insight to what was going on - until I noticed that the classes that would cause the crashes were all dynamically allocated, using standard new.

SSE data needs to be aligned on 16 byte boundaries, OR ELSE!

My current fix to this is to inherit from this class:

#include <xmmintrin.h>

class SSEAlign
 void *operator new (unsigned int size)
 { return _mm_malloc(size, 16); }

 void operator delete (void *p)
 { _mm_free(p); }
But see this thread as to why that may not be the best way to do it. (Link is dead.)
For me, it works for now.