Author |
Message |
Andy
|
Posted: Fri Dec 01, 2006 2:43 pm Post subject: magic function |
|
|
c++: |
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
here and here |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Fri Dec 01, 2006 3:05 pm Post subject: (No subject) |
|
|
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.
Quote:
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 ![Sad Sad](http://compsci.ca/v3/images/smiles/icon_sad.gif) |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Fri Dec 01, 2006 3:50 pm Post subject: (No subject) |
|
|
the first "here" does explain it |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Fri Dec 01, 2006 4:11 pm Post subject: (No subject) |
|
|
It's demonstrating the hole in C++'s type system. ![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Fri Dec 01, 2006 6:08 pm Post subject: (No subject) |
|
|
hahaa, i see it as a feature :p |
|
|
|
|
![](images/spacer.gif) |
ericfourfour
|
Posted: Fri Dec 01, 2006 6:24 pm Post subject: (No subject) |
|
|
Isn't a
wtd wrote: hole
used
Andy wrote: as a feature
an exploit? |
|
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Fri Dec 01, 2006 6:46 pm Post subject: (No subject) |
|
|
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. |
|
|
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: Sat Dec 02, 2006 9:42 pm Post subject: (No subject) |
|
|
wtd wrote: It's demonstrating the hole in C++'s type system. ;)
Well, the code does this: undefined behaviour. Not much of a hole... |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Dec 02, 2006 11:48 pm Post subject: (No subject) |
|
|
The hole being that ultimately values in C and C++ are untyped. |
|
|
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Sun Dec 03, 2006 9:32 am Post subject: (No subject) |
|
|
what exactly does the function do? I read through both articles, and, I'll admit, I'm confused. |
|
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Sun Dec 03, 2006 4:11 pm Post subject: (No subject) |
|
|
did you really? because the title of the first article clearly states the usage of the function |
|
|
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: Sun Dec 03, 2006 4:22 pm Post subject: (No subject) |
|
|
Yay for reposting things from slashdot
(or possibly finding and posting from digg... or before both; but it's old news either way) |
|
|
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Sun Dec 03, 2006 4:39 pm Post subject: (No subject) |
|
|
Yes I get the "Fast Inverse Square Root" title, but what exactly does that mean? |
|
|
|
|
![](images/spacer.gif) |
zylum
![](http://compsci.ca/v3/uploads/user_avatars/1689129126468091c334ee0.gif)
|
Posted: Sun Dec 03, 2006 10:36 pm Post subject: (No subject) |
|
|
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. |
|
|
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: Sun Dec 03, 2006 10:37 pm Post subject: (No subject) |
|
|
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. |
|
|
|
|
![](images/spacer.gif) |
|