Cult code via [WayBack] Fast inverse square root – Wikipedia part of [WayBack] Quake-III-Arena/blob/master/code/game/q_math.c:
float Q_rsqrt( 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 ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}
It is a really fast way to approximate the square root for 32-bit IEEE754 calculations having origins around 1986:
- [WayBack] Symplectic Spacewar » Cleve’s Corner: Cleve Moler on Mathematics and Computing:
Cleve Moler replied on June 27th, 2012 9:35 pm UTC :
Jotaf — Thanks very much for your comment, and for reminding me about the fast inverse square root hack. I didn’t realize that the trick had attained a kind of cult status in the graphics community. The trick uses bit-fiddling integer operations on a floating point number to get a good starting approximation for Newton’s iteration. The Wikipedia article that you link to describes the trick in great detail, and also links to an article by Rys Sommefeldt about its origins. Sommefeldt goes back to the late ’80s and to me and my colleague Greg Walsh at Ardent Computer. I actually learned about trick from code written by Velvel Kahan and K.C. Ng at Berkeley around 1986. Here is a link to their description, in comments at the end of the fdlibm code for sqrt. http://www.netlib.org/fdlibm/e_sqrt.c . — Cleve
- [WayBack] http://www.netlib.org/fdlibm/e_sqrt.c
By now there is also a constant for 64-bit IEEE754 calculations 0x5fe6ec85e7de30da
by [WayBack] 2003 research from Chris Lomont who also found a better 32-bit constant 0x5f375a86
.
Note you need to be careful with boundary values like zero and infinity. This holds for approximations in general: [WayBack] performance – Why is SSE scalar sqrt(x) slower than rsqrt(x) * x? – Stack Overflow
–jeroen
Like this:
Like Loading...