Add fast-inverse SSE1 sqrt() from Quake 3 Arena

Add the Quake 3 Arena fast-inverse `sqrt()` using SSE1 intrinsics to
third_party.

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
This commit is contained in:
Squall Leonhart 2025-01-12 15:37:51 +00:00 committed by Rafael Kitover
parent 44aa859e6d
commit d36f80b5e6
No known key found for this signature in database
GPG Key ID: 08AB596679D86240
1 changed files with 12 additions and 0 deletions

12
third_party/quake3-sqrt/quake3-sqrt.h vendored Normal file
View File

@ -0,0 +1,12 @@
// From Quake 3 Arena.
#include <immintrin.h>
inline float quake3_sqrt(float x) {
__m128 y = _mm_set_ss(x);
__m128 approx = _mm_rsqrt_ss(y);
__m128 half_x = _mm_mul_ss(y, _mm_set_ss(0.5f));
__m128 three_half = _mm_set_ss(1.5f);
__m128 refined = _mm_mul_ss(approx, _mm_sub_ss(three_half, _mm_mul_ss(half_x, _mm_mul_ss(approx, approx))));
return _mm_cvtss_f32(_mm_mul_ss(refined, y));
}