Ember

intrinsics

Nov 21st, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. inline __m128 SSE_SIMDTestNormalizeFast( float * RESTRICT vOut, float * RESTRICT vIn  )
  2. {
  3.         // load as a SIMD vector
  4.         const __m128 vec = _mm_loadu_ps(vIn);
  5.         // compute a dot product by computing the square, and
  6.         // then rotating the vector and adding, so that the
  7.         // dot ends up in the low term (used by the scalar ops)
  8.         __m128 dot = _mm_mul_ps( vec, vec );
  9.         // rotate x under y and add together  
  10.         __m128 rotated = _mm_shuffle_ps( dot, dot, _MM_SHUFFLE( 0,3,2,1 ) ); // YZWX ( shuffle macro is high to low word )
  11.         dot = _mm_add_ss( dot, rotated ); // x^2 + y^2 in the low word
  12.         rotated = _mm_shuffle_ps( rotated, rotated, _MM_SHUFFLE( 0,3,2,1 ) ); // ZWXY
  13.         dot = _mm_add_ss( dot, rotated ); // x^2 + y^2 + z^2 in the low word
  14.  
  15.         __m128 recipsqrt = SSERSqrtNR( dot ); // contains reciprocal square root in low term
  16.         recipsqrt = _mm_shuffle_ps( recipsqrt, recipsqrt, _MM_SHUFFLE( 0, 0, 0, 0 ) ); // broadcast low term to all words
  17.  
  18.         // multiply 1/sqrt(dotproduct) against all vector components, and write back
  19.         const __m128 normalized = _mm_mul_ps( vec, recipsqrt );
  20.         _mm_storeu_ps(vOut, normalized);
  21.         return _mm_mul_ss( dot , recipsqrt );
  22. }
Advertisement
Add Comment
Please, Sign In to add comment