|
-
February 23rd, 2007, 04:45 PM
#1
some q3 code
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.
Code:
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?
-
February 23rd, 2007, 05:42 PM
#2
Re: some q3 code
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
Bogdan
If someone helped you then please Rate his post and mark the thread as Resolved
Please improve your messages appearance by using tags [ code] Place your code here [ /code]
-
February 23rd, 2007, 05:50 PM
#3
Re: some q3 code
It is a bad way of doing the following
Code:
union FloatBits
{
float Float;
int Bits;
};
{
float f = 123.456;
FloatBits fb;
fb.Float = f;
int i = fb.Bits;
}
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 24th, 2007, 02:15 PM
#4
Re: some q3 code
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(). 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.
Insert entertaining phrase here
-
February 24th, 2007, 04:27 PM
#5
Re: some q3 code
Interesting article.
 Originally Posted by TheCPUWizard
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?
-
February 24th, 2007, 04:59 PM
#6
Re: some q3 code
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
There are many analogies I am thinking of, but each one is sure to embarass/offend someone in this international community.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 26th, 2007, 08:40 AM
#7
Re: some q3 code
ah ok thanks. I guess it just confused me
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|