Fast approximation of x^0.5

Posted: July 12, 2011 in Programming
Tags: ,

Here is fast method to compute the inverse of the square root of a number, implemented in the famous Quake3 engine.

float InvSqrt(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;
}

What’s behind it? Newton-Raphson method and bitwise manipulation of floating point numbers. Here goes a little explanation.

Back then, floating-point computation was a resource-intensive operation. However, with graphics cards that peaks at the TFLOPS level, computational power has increased by orders of magnitude, and you will rarely need or find this kind of tricks nowadays.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s