Finding the inverse square root of a number with i = 0x5f3759df - (i>>1);

| | TrackBacks (0)

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

About this Entry

This page contains a single entry by klsh published on February 8, 2008 3:14 PM.

Jawr rapid development of resources for Javascript and CSS was the previous entry in this blog.

SSH tunneled connection to a MySQL server is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.