From d704c2f6d05ecaffa644213f728ed2d0182eeaee Mon Sep 17 00:00:00 2001 From: inmarket Date: Tue, 24 Sep 2013 16:11:29 +1000 Subject: [PATCH] New inverse square root accelerated math function --- src/gmisc/trig.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/gmisc/trig.c b/src/gmisc/trig.c index 510ee597..00b6365a 100644 --- a/src/gmisc/trig.c +++ b/src/gmisc/trig.c @@ -142,5 +142,23 @@ #endif +#if GMISC_NEED_INVSQRT + // Algorithm based on Quake code + float invsqrt(float n) { + long i; + float x2, y; + const float threehalfs = 1.5F; + + x2 = n * 0.5F; + y = n; + i = * ( long * ) &y; // evil floating point bit level hacking + i = 0x5f3759df - ( i >> 1 ); // what the? + y = * ( float * ) &i; + y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration + //y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration for extra precision, this can be removed + return y; + } +#endif + #endif /* GFX_USE_GMISC */ /** @} */