Page 1 of 1

[solved] John Carmack sqrt function

Posted: Mon Nov 23, 2009 7:21 pm
by stefbuet
Hey,

I was looking at some stuff and I found a function from quake III sources written by John Carmack which return the square root of a float. It's suposed to be 4 times faster than the standart sqrt function from math.h
I checked in Irrlicht engine and this function is not used.

I made a bench of the John's function, and what surprised me is it's slower than the standart sqrt function... How can this be possible :o

The code :

Code: Select all

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <math.h>

float SquareRootFloat(float number) {
    long i;
    float x, y;
    const float f = 1.5F;
    
    x = number * 0.5F;
    y  = number;
    i  = * ( long * ) &y;
    i  = 0x5f3759df - ( i >> 1 );
    y  = * ( float * ) &i;
    y  = y * ( f - ( x * y * y ) );
    y  = y * ( f - ( x * y * y ) );
    return number * y;
}
int main() {
    
    float a=clock();
    for(unsigned int i=0; i<100000000; i++) {
        SquareRootFloat(4.0);
    }
    std::cout<<"With fast sqrt : "<<(clock()-a)<<std::endl;
    
    a=clock();
    for(unsigned int j=0; j<100000000; j++) {
        sqrt(4.0);
    }
    std::cout<<"With stantart sqrt : "<<(clock()-a)<<std::endl;
    
    
    
    system("PAUSE");
    
}
I don't understand
:?: [/code]

EDIT : I tryed with compiler optimisation to the max.
The carmack function go from 1.7s to 0.78 as standart sqrt function
Lets test for 10^10 iterations...
Result : Carmack & standart spend same time to calculate sqare roots :S

Re: John Carman sqrt function :D

Posted: Mon Nov 23, 2009 7:41 pm
by randomMesh
*cough*

Carmack

*cough*

Posted: Mon Nov 23, 2009 7:52 pm
by stefbuet
omg, sorry, I type it wrongly :cry: :cry:
May he forgive me :o
Carmack bless you :)

Posted: Mon Nov 23, 2009 9:06 pm
by Sylence
Did you test this with a compiler that was modern when Quake 3 was released ?

Posted: Mon Nov 23, 2009 9:07 pm
by Halifax
Well the reason is that he wrote that code back in ~1998 (I think), which means he was writing for different processors than the ones today. His function may perform worse on your processor compared to the standard sqrt, but also may perform faster on other processors.

The reason being the advancement of technology. Back then, a few additions, subtractions, shifts, and logical operators were much faster than floating-point operations. But on the processors of today, that has changed.

By the way, your test isn't exactly the best. Remember that processors have caches, so you may just be testing the speed of the cache. You want to do randomized data so it can't cache it.

Posted: Mon Nov 23, 2009 9:27 pm
by stefbuet
Ok Thanks!

Posted: Mon Nov 23, 2009 9:50 pm
by yamashi
And you use newton's algorithm 2 times, it should be used only once since you use the magic number.

Posted: Tue Nov 24, 2009 3:41 am
by Halifax
Yamashi, the second iteration of newton's method can be used to refine the approximation. It provides better accuracy.