
-----------------------------------
Andy
Fri Dec 01, 2006 2:43 pm

magic function
-----------------------------------

float foo (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;
}

without cheatin, can anyone figure out what this code does?

for those who giveup, goto

[url=http://www.geometrictools.com/Documentation/FastInverseSqrt.pdf]here and [url=http://www.beyond3d.com/articles/fastinvsqrt/]here

-----------------------------------
Tony
Fri Dec 01, 2006 3:05 pm


-----------------------------------
so it's a 10 year old piece of (amazing!) code, and an equivallently long article attempting to hunt down for the code's author.


took a long time to figure out how and why this works, and I can't
remember the details anymore.


And it still doesn't explain the magic 0x5f3759df constant  :(

-----------------------------------
Andy
Fri Dec 01, 2006 3:50 pm


-----------------------------------
the first "here" does explain it

-----------------------------------
wtd
Fri Dec 01, 2006 4:11 pm


-----------------------------------
It's demonstrating the hole in C++'s type system.  ;)

-----------------------------------
Andy
Fri Dec 01, 2006 6:08 pm


-----------------------------------
hahaa, i see it as a feature :p

-----------------------------------
ericfourfour
Fri Dec 01, 2006 6:24 pm


-----------------------------------
Isn't a
hole
used
as a feature
an exploit?

-----------------------------------
Andy
Fri Dec 01, 2006 6:46 pm


-----------------------------------
yeah it's a hack, only works on 32bit systems.

apparently that piece of code is rumored to be written by Gary Tarolli, from NVidia.. I'm going to email him and ask him about it.

-----------------------------------
OneOffDriveByPoster
Sat Dec 02, 2006 9:42 pm


-----------------------------------
It's demonstrating the hole in C++'s type system.  ;)
Well, the code does this:  undefined behaviour.  Not much of a hole...

-----------------------------------
wtd
Sat Dec 02, 2006 11:48 pm


-----------------------------------
The hole being that ultimately values in C and C++ are untyped.

-----------------------------------
Clayton
Sun Dec 03, 2006 9:32 am


-----------------------------------
what exactly does the function do? I read through both articles, and, I'll admit, I'm confused.

-----------------------------------
Andy
Sun Dec 03, 2006 4:11 pm


-----------------------------------
did you really? because the title of the first article clearly states the usage of the function

-----------------------------------
md
Sun Dec 03, 2006 4:22 pm


-----------------------------------
Yay for reposting things from slashdot ;-)

(or possibly finding and posting from digg... or before both; but it's old news either way)

-----------------------------------
Clayton
Sun Dec 03, 2006 4:39 pm


-----------------------------------
Yes I get the "Fast Inverse Square Root" title, but what exactly does that mean?

-----------------------------------
zylum
Sun Dec 03, 2006 10:36 pm


-----------------------------------
the square root function can be slow. the function presented finds the inverse square root very efficiently in (as you can see) an unconventional way. as you may know, the square root funtion is useful for finding the distance between 2 points among other useful calculations.

-----------------------------------
md
Sun Dec 03, 2006 10:37 pm


-----------------------------------
It calculates the inverse of the floating point number passed of course! Not only that, but it does it rather quickly using the particularities of the 32bit x86 processor, C and some math. Alas modern GPUs can do the same amazingly fast (much faster...) and it's not very portable.

-----------------------------------
Tony
Thu Dec 21, 2006 2:35 pm


-----------------------------------
for those interested, here's a [url=http://www.beyond3d.com/articles/fastinvsqrt2/]follow-up article
