0

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?
}
NPS
  • 6,003
  • 11
  • 53
  • 90

1 Answers1

0

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.

Chuck Walbourn
  • 38,259
  • 2
  • 58
  • 81
  • I know about `XMFLOAT4`, I've read the doc but I was wondering if it were necessary. – NPS Jun 27 '14 at 20:54
  • It's necessary to be portable with ARM or x86 (32-bit) platforms. if you were only interested in x64 (64-bit) platforms, you can safely just use XMVECTOR and XMMATRIX directly in most cases--there are things that can change the default alignment of a class that would cause problems. – Chuck Walbourn Jun 28 '14 at 06:07