Use XMVECTOR as a class member

294 views Asked by At

I know XMVECTOR has to be 16-byte-aligned and therefore shouldn't be a member of a class (unless I ensure the alignment). However, can I do sth like this?

class A
{
    public:
    XMVECTOR vect;
};

void fun(void)
{
    A a;
    XMVECTOR localVect = a.vect;
    // can I now use localVect correctly?
}
1

There are 1 answers

2
Chuck Walbourn On

The compiler is likely to use movaps to implement the assignment, which would fail due to alignment on most CPUs.

The way to make this not require alignment is to use:

class A { public: XMFLOAT4 vect; } ... XMVECTOR localVec = XMLoadFloat4( &a.vect );

This compiles to a single movups.