Slashdot Log In
Origin of Quake3's Fast InvSqrt()
Posted by
Zonk
on Fri Dec 01, 2006 03:20 PM
from the i-know-you-were-dying-inside-without-this dept.
from the i-know-you-were-dying-inside-without-this dept.
geo writes "Beyond3D.com's Ryszard Sommefeldt dons his seersucker hunting jacket and meerschaum pipe to take on his secret identity as graphics code sleuth extraordinaire. In today's thrilling installment, the origins of one of the more famous snippets of graphics code in recent years is under the microscope — Quake3's Fast InvSqrt(), which has been known to cause strong geeks to go wobbly in the knees while contemplating its simple beauty and power."
This discussion has been archived.
No new comments can be posted.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
Full
Abbreviated
Hidden
Loading... please wait.
A famous quote (Score:5, Funny)
Re:A famous quote (Score:5, Informative)
Parent
Re:A famous quote (Score:5, Funny)
Parent
Re:A famous quote (Score:5, Informative)
Parent
Re:A famous quote (Score:5, Funny)
Interesting smiley... is that a dead man with a fraction in his mouth and a prominent Adam's Apple, wearing a bow tie and a dress and standing on a toy race car?
What's your point, man?
Parent
Re:A famous quote (Score:5, Informative)
This is great for a 3D rendering application, but in a game speed is critical. This pair of calculations involves a square root and a divide. Both of thse are at least an order of magnitude slower than multiplications and additions.
So what this function does is provide a value you can multiply each component by to get a unit vector.
Well, there's the what and why parts. As for the , I have no idea. I think it uses magic.
Parent
Re:A famous quote (Score:5, Informative)
Carmack quite graciously denied the code was his and helped direct the author closer to the true source.
Parent
I know who wrote it (Score:5, Funny)
Obviously SCO's intellectual property! (Score:5, Funny)
This paper seems to have the info (Score:5, Informative)
Re:This paper seems to have the info (Score:5, Funny)
Parent
Mirrordot the airticle cut-and-paste (Score:5, Informative)
Note!
This article is a republishing of something I had up on my personal website a year or so ago before I joined Beyond3D, which is itself the culmination of an investigation started in April 2004. So if timeframes appear a little wonky, it's entirely on purpose! One for the geeks, enjoy.
Origin of Quake3's Fast InvSqrt()
To most folks the following bit of C code, found in a few places in the recently released Quake3 source code, won't mean much. To the Beyond3D crowd it might ring a bell or two. It might even make some sense.
InvSqrt()
Finding the inverse square root of a number has many applications in 3D graphics, not least of all the normalisation of 3D vectors. Without something like the nrm instruction in a modern fragment processor where you can get normalisation of an fp16 3-channel vector for free on certain NVIDIA hardware if you're (or the compiler is!) careful, or if you need to do it outside of a shader program for whatever reason, inverse square root is your friend. Most of you will know that you can calculate a square root using Newton-Raphson iteration and essentially that's what the code above does, but with a twist.
How the code works
The magic of the code, even if you can't follow it, stands out as the i = 0x5f3759df - (i>>1); line. Simplified, Newton-Raphson is an approximation that starts off with a guess and refines it with iteration. Taking advantage of the nature of 32-bit x86 processors, i, an integer, is initially set to the value of the floating point number you want to take the inverse square of, using an integer cast. i is then set to 0x5f3759df, minus itself shifted one bit to the right. The right shift drops the least significant bit of i, essentially halving it.
Using the integer cast of the seeded value, i is reused and the initial guess for Newton is calculated using the magic seed value minus a free divide by 2 courtesy of the CPU.
But why that constant to start the guessing game? Chris Lomont wrote a paper analysing it while at Purdue in 2003. He'd seen the code on the gamedev.net forums and that's probably also where DemoCoder saw it before commenting in the first NV40 Doom3 thread on B3D. Chris's analysis for his paper explains it for those interested in the base math behind the implementation. Suffice to say the constant used to start the Newton iteration is a very clever one. The paper's summary wonders who wrote it and whether they got there by guessing or derivation.
So who did write it? John Carmack?
While discussing NV40's render path in the Doom3 engine as mentioned previously, the code was brought up and attributed to John Carmack; and he's the obvious choice since it appears in the source for one of his engines. Michael Abrash was mooted as a possible author too. Michael stands up here as x86 assembly optimiser extraordinaire, author of the legendary Zen of Assembly Language and Zen of Graphics Programming tomes, and employee of id during Quake's development where he worked alongside Carmack on optimising Quake's software renderer for the CPUs around at the time.
Asking John whether it was him or Michael returned a "not quite".
-----Original Message-----
From: John Carmack
Sent: 26 April 2004 19:51
Subject: Re: Origin of fast approximated inverse square root
At 06:38 PM 4/26/2004 +0100, you wrote:
>Hi John,
>
>There's a discussion on Beyond3D.com's forums about who the author of
>the following is:
>
>float InvSqrt (float x){
> float xhalf = 0.5f*x;
> int i = *(int*)
> i = 0x5f3759df - (i>>1);
> x = *(float*)
> x = x*(1.5f - xhalf*x*x);
> return x;
>}
>
>Is that something we can attribute to you? Analysis shows it to be
>extremely clever in its method and supposedly from the Q3 source.
>Most people say it's your work, a few say it's Michael Abrash's. Do
>you know who's responsible, possibly with a history of sorts?
Not me,
It might be damn smart.. (Score:5, Insightful)
Seriously, try looking away from the genius who obviously wrote it.
- There is no single comment which would make reading and understanding what happens here much easier!
- Introduction of a magic number with no explanation whatsoever
- Magic pointer arithmetics without demystification
- Portability? Abuse of a single processor architecture, without warning that this would not work on non-x86
I know it is good code. But it is simply bad code!Re:And so why do we care? (Score:5, Funny)
I was a little worried when Slashdot posted the Britney Spears beaver pictures, but they now have their credibility back as the home of "News for Nerds".
Parent
Re:And so why do we care? (Score:5, Funny)
Parent
Re:And so why do we care? (Score:5, Informative)
Parent
Re:And so why do we care? (Score:5, Funny)
Parent
Re:And so why do we care? (Score:5, Funny)
--
Evan
Parent
Re:What's with use of Pointers? (Score:5, Informative)
int i = (int)x;
Then C will simply convert the float value into an integer value (throwing away fractional part). But this isn't what we want. We want to operate on the bits of an IEEE floating point value directly, and integers are the best way to do that.
So first, we lie to the compiler by telling it we have a pointer to an int:
(int *) &f
And then we deference the pointer to get it into an operable int:
i = *(int *) &f
Note what's important here is to keep the compiler from modifying any part of the original 32-bit value.
Schwab
Parent
Re:What's with use of Pointers? (Score:5, Informative)
*(int*) &x treats the bits as an integer, with no behind the scenes conversion to an actual int value.
Parent
Re:What's with use of Pointers? (Score:5, Informative)
If you do this:
int i = (int)3.0f;
You get i=3, like what you'd get from the floor() function.
If you do this:
float f = 3.0f;
int i = *(int*)
Then i contains a bit-for-bit copy of the IEEE floating-point representation of 3.0.
It's because C knows how to cast a float to an int by applying the floor function. However, if you do it the second way, you aren't casting a float to an int, you are casting a pointer-to-float to a pointer-to-int and then dereferencing it.
By the way, I just wanted to say... this is one of the most interesting things I've read on Slashdot in a while. Wow. That function is just amazing. I only wish I understood how it worked. I know nothing about what a "Newton-Raphson iteration" is.
Parent
Re:What's with use of Pointers? (Score:5, Informative)
You start with some INITIAL GUESS (the real beauty of this algorithm) X(0), then apply:
X(n+1) = X(n) - f(X(n)) / f'(X(n))
where
X(n+1) is the NEXT guess after the value you 'know',
X(n) is that most recent value you know,
f(X(n)) is the function evaluated at X(n) and
f'(X(n)) is the first derivative of f(x) evaluated at X(n).
It's not foolproof and a BOTH whether it converges at al AND how FAST it converges depends on the initial guess, X(0)
The "Secant Method" is an improvement that makes it a little 'smarter,' at the expense of more computation (this is often a positive trade-off on numerical modeling codes, since the 'smarter' algorithm does tend to converge faster). There are other improvements as well, such as the Los Alamos Linear Feedback Solver (a slightly modified secant method that converges about 10-17% faster, at least for some types of problems) that I use in my own codes.
Obligatory wikipediea followup: Newton's Method [wikipedia.org]
Parent
Re:What's with use of Pointers? (Score:5, Informative)
I'll take a swing at this one. It's because the author doesn't want the value of x, but the integer representation of the value at x's memory address.
If x is 3.14159, (2) will result in i==3, whereas (1) will result in whatever the 4-byte IEEE-754 representation of 3.14159 is (0x40490FD0, if Google is correct). By using (1), the author is able to use integer bitwise opeartions (>>) to perform "free" floating point operations. When i is sent back into floating point form via:
x = *(float*)
x now contains the value of the integer operation:
i = 0x5f3759df - (i >> 1);
which was presumably faster than an identical floating point operation. It's a nifty little solution, especially with regard to the selection of the magic number.
Parent
Re:It was fast (Score:5, Insightful)
Parent
Re:It Was Obviously... (Score:5, Funny)
int MakeYouMyBitch7 () {
int my_bitch = MakeYouMyBitch() * MakeYouMyBitch2();
return MakeYouMyBitch36(my_bitch);
}
Just terrible.
Parent