Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 magic function
Index -> General Programming
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Andy




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Andy




PostPosted: Fri Dec 01, 2006 3:50 pm   Post subject: (No subject)

the first "here" does explain it
wtd




PostPosted: Fri Dec 01, 2006 4:11 pm   Post subject: (No subject)

It's demonstrating the hole in C++'s type system. Wink
Andy




PostPosted: Fri Dec 01, 2006 6:08 pm   Post subject: (No subject)

hahaa, i see it as a feature :p
ericfourfour




PostPosted: 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?
Andy




PostPosted: 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.
OneOffDriveByPoster




PostPosted: 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...
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Dec 02, 2006 11:48 pm   Post subject: (No subject)

The hole being that ultimately values in C and C++ are untyped.
Clayton




PostPosted: 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.
Andy




PostPosted: 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
md




PostPosted: Sun Dec 03, 2006 4:22 pm   Post subject: (No subject)

Yay for reposting things from slashdot Wink

(or possibly finding and posting from digg... or before both; but it's old news either way)
Clayton




PostPosted: 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?
zylum




PostPosted: 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.
md




PostPosted: 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.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: