[solved] John Carmack sqrt function

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
stefbuet
Competition winner
Posts: 495
Joined: Sun Dec 09, 2007 4:13 pm
Location: france

[solved] John Carmack sqrt function

Post 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
Last edited by stefbuet on Mon Nov 23, 2009 9:28 pm, edited 3 times in total.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: John Carman sqrt function :D

Post by randomMesh »

*cough*

Carmack

*cough*
"Whoops..."
stefbuet
Competition winner
Posts: 495
Joined: Sun Dec 09, 2007 4:13 pm
Location: france

Post by stefbuet »

omg, sorry, I type it wrongly :cry: :cry:
May he forgive me :o
Carmack bless you :)
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

Did you test this with a compiler that was modern when Quake 3 was released ?
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post 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.
TheQuestion = 2B || !2B
stefbuet
Competition winner
Posts: 495
Joined: Sun Dec 09, 2007 4:13 pm
Location: france

Post by stefbuet »

Ok Thanks!
yamashi
Posts: 82
Joined: Sat Jan 03, 2009 4:53 am

Post by yamashi »

And you use newton's algorithm 2 times, it should be used only once since you use the magic number.
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Yamashi, the second iteration of newton's method can be used to refine the approximation. It provides better accuracy.
TheQuestion = 2B || !2B
Post Reply