1. ## bit level hacking?

hey, hows it going?

i ripped this from the quake 3 source:
Code:
```float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;

x2 = number * 0.5F;
y  = number;
i  = * ( long * ) &y;   // evil floating point bit level hacking
i  = 0x5f3759df - ( i >> 1 );       // what the ..........?
y  = * ( float * ) &i;
y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed
return y;
}```
the code returns the reciprocal of sqrt(number).
my question is this: what do they mean by "bit level hacking"? also, why is 0x5f3759df used?

2. >what do they mean by "bit level hacking"?

They're converting a floating-point variable to a long variable. They get the address of y, cast it to pointer to long and then get the object.

3. Nobody seems to know anything about floating point numbers. Everybody knows that they add overhead, but nobody seems to know how they work. You should take the opportunity to find out.

They're converting it to an integer so that they can manipulate the bits individually. This is what they mean by bit-level-hacking. They want to manipulate the float one bit at a time. They will do this to perform an operation that a float doesn't inherantly allow, or to do it faster than any other method will allow.

If you don't know how a float is stored internally, try looking up IEEE floating point number format on the web. You may learn a lot about floats just from looking at it. All the bits in a float play a certain ROLE and to manipulate them requires a lot of care (hence the "evil" reference). When you see the format of a float, it may make sense how they would choose an archaic number like that.

http://www.psc.edu/general/software/...ieee/ieee.html

Bear in mind that this may not be the format for the floats in your source code.

4. Since 0x5f3759df is
such a strange number you can search for

5. LOL

EDIT:
Where the hell did you get the Quake 3 source??

6. Originally posted by GaPe
LOL

EDIT:
Where the hell did you get the Quake 3 source??
from ftp://ftp.idsoftware.com/.
its just the game source, though. they havent released the engine source yet.