Sei sulla pagina 1di 1

// Carmack's Quake III Arena Inverse Square Root function

// Calculate ~ 1/?x
// Useful for normalizing a vector
// angles of incidence, reflection for lighting shading
// magic number is 3/4L(B-s)
// where L is 2^23, B=127, s= 0.0430357
// if output is I_y and input is I_x
// I_y = magic_number - 1/2 I_x
// Newton's Method using only a single iteration allows for a better value
// the code then comes while keeping in mind 1/2 I_x = I_x / 2 = I_x >> 2

float Q_rsqrt( float number )


{
long i;
float x2, y;
const float threehalfs = 1.5F;

x2 = number * 0.5F;
y = number;
{WNY - Turns float y into int i}
i = * ( long * ) &y; // evil floating point bit level
hacking
{WNY - magic 32b number}
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
{WNY - Turns int i into float y}
y = * ( float * ) &i;
{WNY - iteration refers to iteration of Newton's Method - improves accuracy}
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be
removed

return y;
}

Potrebbero piacerti anche