Skip to content

Commit d535089

Browse files
committed
q_shared: single iteration in Q_sqrt
1 parent 79598bf commit d535089

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/engine/qcommon/q_shared.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -342,19 +342,22 @@ extern const quat_t quatIdentity;
342342
// https://en.wikipedia.org/wiki/Fast_inverse_square_root#/media/File:2nd-iter.png
343343
inline float Q_rsqrt( float number )
344344
{
345-
float x = 0.5f * number;
346-
float y;
347-
348345
// compute approximate inverse square root
349346
#if defined(DAEMON_USE_ARCH_INTRINSICS_i686_sse)
347+
float y;
350348
// SSE rsqrt relative error bound: 3.7 * 10^-4
351349
_mm_store_ss( &y, _mm_rsqrt_ss( _mm_load_ss( &number ) ) );
352350
#else
353-
y = Util::bit_cast<float>( 0x5f3759df - ( Util::bit_cast<uint32_t>( number ) >> 1 ) );
354-
y *= ( 1.5f - ( x * y * y ) ); // initial iteration
351+
float x = 0.5f * number;
352+
float y = Util::bit_cast<float>( 0x5f3759df - ( Util::bit_cast<uint32_t>( number ) >> 1 ) );
353+
// initial iteration
355354
// relative error bound after the initial iteration: 1.8 * 10^-3
355+
y *= ( 1.5f - ( x * y * y ) );
356+
#if 0
357+
// second iteration for higher precision
358+
y *= ( 1.5f - ( x * y * y ) );
359+
#endif
356360
#endif
357-
y *= ( 1.5f - ( x * y * y ) ); // second iteration for higher precision
358361
return y;
359362
}
360363

0 commit comments

Comments
 (0)