0
\$\begingroup\$

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? } 

Edit: My question isn't "Can I use XMVECTOR as a member variable?". It's "Can I copy an XMVECTOR instance that's not guaranteed to be 16-byte aligned into one that is and then use the latter one without any problems?".

\$\endgroup\$
3
  • \$\begingroup\$ Where do you get the idea that 16-byte-aligned things shouldn't be members of a class? This is fine - it just forces the class as a whole to be 16-byte-aligned as well. \$\endgroup\$ Commented Jan 6, 2014 at 20:39
  • 1
    \$\begingroup\$ @NathanReed this is true as far as he allocating his struct/class on the stack, on the heap AFAIK you need to force it. \$\endgroup\$ Commented Jan 6, 2014 at 20:50
  • \$\begingroup\$ As I said: "shouldn't be a member of a class (unless I ensure the alignment)". \$\endgroup\$ Commented Jan 6, 2014 at 21:07

2 Answers 2

1
\$\begingroup\$

Answering the question after the edit:

If XMVECTOR was allocated on the stack then the compiler will automatically force it to be 16-byte aligned.

The only case that you have an unaligned XMVECTOR is when it's allocated on the heap, but should't be used at all, because it will result in a seg-fault.

On the other hand if you are accessing the XMVECTOR as a memory chunk using its address, in other words without doing any computation it won't be copied to the SSE registers. You can copy it just like any other POD. The problem isn't accessing the memory, the problem is about using it, so copying it is OK as far as you don't do any math operations that use SSE registers.

\$\endgroup\$
7
  • \$\begingroup\$ Seems like you didn't get what I was asking about at all. \$\endgroup\$ Commented Jan 6, 2014 at 21:11
  • \$\begingroup\$ @NPS well, the title and the content are confusing. They are fundamentally different. \$\endgroup\$ Commented Jan 6, 2014 at 21:14
  • \$\begingroup\$ You are absolutely right, even I was surprised when I looked at the title after reading your comment. Well, hope the new title is better. \$\endgroup\$ Commented Jan 7, 2014 at 10:55
  • \$\begingroup\$ As for my question - so my code should copy XMVECTOR's content correctly and then working on a copy (that is aligned) should be fine as well? \$\endgroup\$ Commented Jan 7, 2014 at 10:58
  • \$\begingroup\$ @NPS Yes. Given that you don't try to access it or make any computations that use the SSE registers. so if you make memcpy( dst, src, sizeof(XMVECTOR)) because this operation uses the normal registers and no SSE is involved. \$\endgroup\$ Commented Jan 7, 2014 at 11:21
1
\$\begingroup\$

If you are only building for x64 native, this code is perfectly fine since it is always 16-byte aligned.

class A { public: XMVECTOR vect; }; void fun(void) { A* a = new A; XMVECTOR localVect = a.vect; } 

To make it portable to x86 or ARM (32-bit), you have one of three choices:

  1. Create an overloaded new for the class A that uses __aligned_malloc(,16);
  2. Create a global new overload that does this (which is not recommended)
  3. Just use XMFLOAT4 or it's kin and explicitly use XMLoadFloat4/XMStoreFloat4

The following code results in the identical compiled code as the block above except that one movaps is now a movups:

class A { public: XMFLOAT4 vect; }; void fun(void) { A* a = new A; XMVECTOR localVect = XMLoadFloat4( &a.vect ); } 

As such, there's no point in messing around with aligned allocations unless it's trivial to do for some other reason (such as DirectX Tool Kit's use of the pImpl idiom which means we might as well align the internal class allocations to make it convenient).

If your complaint is just the extra work it takes to use XMVECTOR correctly with respect to the alignment, you could take a look at using SimpleMath from the DirectX Tool Kit.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.