0
\$\begingroup\$

The documentation for XMVector4Dot lists the return value as a vector. It also states that the dot product is replicated into each component. Given that the dot product is going to be a single value, how does this offer an advantage over just returning float?

The only way I can see to get the value is to do something such as:

        XMVECTOR w = { -XMVectorGetX(XMVector4Dot(x, eye)), -XMVectorGetX(XMVector4Dot(y, eye)), -XMVectorGetX(XMVector4Dot(z, eye)), 1 }; 

Which seems unnecessary. Is there another reason I'm overlooking?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

These APIs are designed to leverage the SIMD registers and instruction sets of your CPU, which are vector registers (each one holds four floats). Getting data into and out of these registers is sometimes "slow" (relatively speaking): the best way to utilize them is usually to get data into them and then keep it there as long as possible while you operate on it.

If the dot product operation were to return a single float instead, it would need to pay that transfer cost every time you called it, even if you're just going to immediately transfer that result back into another SIMD register to perform more operations on it. So it's less pessimistic on the part of the API to assume you'll keep doing more SIMD math and thus give you a result that still facilitates that. That puts the decision about when to pay the transfer cost in your hands instead of theirs, and you presumably know better than they do about when the best time to eat that transfer cost is for your application.

Further, by replicating the results out to every component of the vector, you can perform further multiplications against that return in parallel with further SIMD operations.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .