Why does XMVector4Dot return a vector with the dot product copied to it's components?

356 Views Asked by At

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?

1

There are 1 best solutions below

1
galop1n On

This is beneficial with SIMD implementation. CPUs have SIMD instructions (single instruction multiple data).

These instructions work on a different set of registers and going back and forth with regular scalar registers has a significant cost. By using the same vector type for the result of your dot product, it helped keeping the code vector-ised.

Also, it is pretty common practice to splat a scalar value into all the component of a vector and then multiple 4 data with it. You can look at how Array Of Struct versus Structure Of Array ( AOS vs SOA ) influence the design of a SIMD algorithm implementation.