Click to See Complete Forum and Search --> : some q3 code
abcdefgqwerty
February 23rd, 2007, 03:45 PM
I know this has been looked at before a lot, but I was wondering what a line in this q3 code does. This code is very confusing partly because of that crazy number they invent.
float Q_rsqrt( float number )
{
long i;
float x2, y;
y = number;
i = * ( long * ) &y; //what does this do?
i = 0x5f3759df - ( i >> 1 );
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
return y;
}
What exactly does casting the address of a float do?
creatorul
February 23rd, 2007, 04:42 PM
Well .. i = * ( long * ) &y
&y - gives the address of y variable
( long * ) &y - is casting that float address to an address of type long
* ( long * ) &y - gets the value using our new pointer of type long
TheCPUWizard
February 23rd, 2007, 04:50 PM
It is a bad way of doing the following
union FloatBits
{
float Float;
int Bits;
};
{
float f = 123.456;
FloatBits fb;
fb.Float = f;
int i = fb.Bits;
}
wien
February 24th, 2007, 01:15 PM
A bit OT, but here's a link to an article on the origin of the function in question: Origin of Quake3's Fast InvSqrt() (http://www.beyond3d.com/articles/fastinvsqrt/). Doesn't go too much into detail, but you might find it interesting. It also links to an article on the math behind the function if you're curious. :)
Zaccheus
February 24th, 2007, 03:27 PM
Interesting article.
It is a bad way of doing the following
Isn't it also bad to write to one member, and then read from another member, of the same union?
TheCPUWizard
February 24th, 2007, 03:59 PM
Isn't it also bad to write to one member, and then read from another member, of the same union?
Not if you understand the implications :D :D
There are many analogies I am thinking of, but each one is sure to embarass/offend someone in this international community. :blush:
abcdefgqwerty
February 26th, 2007, 07:40 AM
ah ok thanks. I guess it just confused me
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.