Original Post
One of my many side projects is to try and come up with faster routines than the Direct3D Extension (D3DX) API. Having read in several texts that D3DX is indeed fast, the following SSE code is faster by several null cycles:
inline void FastVectorNormalize (D3DXVECTOR4 &vector)
{
__asm
{
mov eax, vector
movaps xmm0, [eax]
movaps xmm2, xmm0
mulps xmm0, xmm0
movaps xmm1, xmm0
shufps xmm0, xmm0, _MM_SHUFFLE (2, 1, 0, 3)
addps xmm1, xmm0
movaps xmm0, xmm1
shufps xmm1, xmm1, _MM_SHUFFLE (1, 0, 3, 2)
addps xmm0, xmm1
rsqrtps xmm0, xmm0
mulps xmm0, xmm2
movaps [eax], xmm0
};
}
Please feel free to use and adapt this code if your program is normalization intensive. However be warned, the SSE instruction 'rsqrtps' uses internal processor tables to compute reciprocal square roots (hence the speed) and so you will lose some accuracy to about 4 decimal places.