SSE2 in Visual Studio

So for some reason I thought I should be using SIMD in my student projects because it’s something that I should learn about, and I might as well do that when I’m not bothering other people with it.

It turns out that Visual Studio will hate you for it. It will hate you for using objects that have a specific alignment in memory.

The reason I’m writing this post is because I just had to write my most ingenious (not) workaround yet for getting a SSE2 class to work with Visual Studio:

class Vector4
{
    union {
        __m128 xyzw;
        struct { float x, y, z, w; };
    };
    /* ... */
#ifdef DEBUG
// for some reason visual studio sometimes copies a Vector4 to a non-aligned position
// in debug mode. which is illegal and crashes the game. it never uses that Vector4
// though, so just changing the copy constructor to not use SIMD is enough
inline Vector4(const Vector4 & other) : x(other.x), y(other.y), z(other.z), w(other.w) {}
#else
inline Vector4(const Vector4 & other) : xyzw(other.xyzw) {}
#endif

Read the rest of this entry »