Finding the inverse square root of a number with i = 0x5f3759df - (i>>1);
Nice bit of writing on some code archaeology. Not definite in it's answer, it provides some illumination of the thinking behind complex problem solving.
float InvSqrt (float x){
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i>>1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;
}
Finding the inverse square root of a number has many applications in 3D graphics, not least of all the normalisation of 3D vectors. Without something like the nrm instruction in a modern fragment processor where you can get normalisation of an fp16 3-channel vector for free on certain NVIDIA hardware if you're (or the compiler is!) careful, or if you need to do it outside of a shader program for whatever reason, inverse square root is your friend.
0 TrackBacks
Listed below are links to blogs that reference this entry: Finding the inverse square root of a number with i = 0x5f3759df - (i>>1);.
TrackBack URL for this entry: http://kennethhunt.com/mt/mt-tb.cgi/1874