From 52a05290b2cf64213bb4e75974779a437318eca0 Mon Sep 17 00:00:00 2001 From: Kyle De'Vir Date: Sun, 29 May 2016 04:16:28 +1000 Subject: [PATCH] Reverts previous HMM_FastInverseSquareRoot revert :P MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hlkpf3 on Mārtiņš Možeiko's comment: i don't really want to be that guy here.. but it's the internet and i have no free will: the new built-ins are using 'the function', so they are not faster than 'the function' - they're just quicker at it. also, many instruction sets don't offer that functionality yet. and for some it doesn't make any sense. playstations and the nintendo wii, for instance, were using powerpc - no. the nintendo wii u also doesn't have it. the "new 3ds" should in theory be the first nintendo-device with this intrinsic thanks to ARM11 NEON. can someone confirm that? heck, some MPUs still don't even have floating-point units. i have heard of a case where the energy-consumption of a sensor-network for indoor-localisation could be halved by replacing the sqrt they used for Pythagoras with this neat buddy here. --- HandmadeMath.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/HandmadeMath.h b/HandmadeMath.h index 8db5f05..eb72c85 100644 --- a/HandmadeMath.h +++ b/HandmadeMath.h @@ -223,6 +223,7 @@ HMMDEF float HMM_ToRadians(float Degrees); HMMDEF float HMM_Inner(hmm_vec3 A, hmm_vec3 B); HMMDEF float HMM_SquareRoot(float Float); HMMDEF float HMM_LengthSquareRoot(hmm_vec3 A); +HMMDEF float HMM_FastInverseSquareRoot(float Number); HMMDEF float HMM_Length(hmm_vec3 A); HMMDEF float HMM_Power(float Base, int Exponent); HMMDEF float HMM_Clamp(float Min, float Value, float Max); @@ -351,6 +352,24 @@ HMM_LengthSquareRoot(hmm_vec3 A) return (Result); } +HINLINE float +HMM_FastInverseSquareRoot(float Number) +{ + long i; + float x2, y; + const float threehalfs = 1.5f; + + x2 = Number * 0.5f; + y = Number; + i = * ( long * ) &y; // evil floating point bit level hacking + i = 0x5f3759df - ( i >> 1 ); // what the fuck? + y = * ( float * ) &i; + + y = y * ( threehalfs - ( x2 * y * y ) ); + + return ( y ); +} + HINLINE float HMM_Length(hmm_vec3 A) {