Added introduction
This commit is contained in:
parent
1c3db4b18d
commit
a166aeaa74
@ -13,16 +13,19 @@
|
||||
by: "Mark",
|
||||
)
|
||||
|
||||
#include "parts/00 int.typ"
|
||||
#include "parts/00 intro.typ"
|
||||
#pagebreak()
|
||||
|
||||
#include "parts/01 float.typ"
|
||||
#include "parts/01 int.typ"
|
||||
#pagebreak()
|
||||
|
||||
#include "parts/02 approx.typ"
|
||||
#include "parts/02 float.typ"
|
||||
#pagebreak()
|
||||
|
||||
#include "parts/03 quake.typ"
|
||||
#include "parts/03 approx.typ"
|
||||
#pagebreak()
|
||||
|
||||
#include "parts/04 bonus.typ"
|
||||
#include "parts/04 quake.typ"
|
||||
#pagebreak()
|
||||
|
||||
#include "parts/05 bonus.typ"
|
||||
|
45
src/Advanced/Fast Inverse Root/parts/00 intro.typ
Normal file
45
src/Advanced/Fast Inverse Root/parts/00 intro.typ
Normal file
@ -0,0 +1,45 @@
|
||||
#import "@local/handout:0.1.0": *
|
||||
|
||||
= Introduction
|
||||
|
||||
In 2005, ID Software published the source code of _Quake III Arena_, a popular game released in 1999. \
|
||||
This caused quite a stir: ID Software was responsible for many games popular among old-school engineers (most notably _Doom_, which has a place in programmer humor even today).
|
||||
|
||||
#v(2mm)
|
||||
|
||||
Naturally, this community immediately began dissecting _Quake_'s source. \
|
||||
One particularly interesting function is reproduced below, with original comments: \
|
||||
|
||||
#v(3mm)
|
||||
|
||||
```c
|
||||
float Q_rsqrt( float number ) {
|
||||
long i;
|
||||
float x2, y;
|
||||
const float threehalfs = 1.5F;
|
||||
|
||||
x2 = number * 0.5F;
|
||||
y = number;
|
||||
i = * ( long * ) &y; // evil floating point bit level hacking
|
||||
i = 0x5f3759df - ( i >> 1 ); // [redacted]
|
||||
y = * ( float * ) &i;
|
||||
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
|
||||
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
|
||||
|
||||
return y;
|
||||
}
|
||||
```
|
||||
|
||||
#v(3mm)
|
||||
|
||||
This code defines a function `Q_sqrt`, which was used as a fast approximation of the inverse square root in graphics routines. (in other words, `Q_sqrt` efficiently approximates $1 div sqrt(x)$)
|
||||
|
||||
#v(3mm)
|
||||
|
||||
The key word here is "fast": _Quake_ ran on very limited hardware, and traditional approximation techinques (like Taylor series)#footnote[In fact, Taylor series aren't used today, and for the same reason: there are better ways.] were too computationally expensive to be viable.
|
||||
|
||||
#v(3mm)
|
||||
|
||||
Our goal today is to understand how `Q_sqrt` works. \
|
||||
To do that, we'll first need to understand how computers represent numbers. \
|
||||
We'll start with simple binary---turn the page.
|
@ -2,10 +2,9 @@
|
||||
|
||||
= The Fast Inverse Square Root
|
||||
|
||||
A simplified version of the _Quake_ routine we are studying is reproduced below.
|
||||
|
||||
The following code is present in _Quake III Arena_ (1999):
|
||||
|
||||
#v(5mm)
|
||||
#v(2mm)
|
||||
|
||||
```c
|
||||
float Q_rsqrt( float number ) {
|
||||
@ -15,7 +14,7 @@ float Q_rsqrt( float number ) {
|
||||
}
|
||||
```
|
||||
|
||||
#v(5mm)
|
||||
#v(2mm)
|
||||
|
||||
This code defines a function `Q_rsqrt` that consumes a float `number` and approximates its inverse square root.
|
||||
If we rewrite this using notation we're familiar with, we get the following:
|
||||
@ -165,8 +164,7 @@ though it is fairly close to the ideal $epsilon$.
|
||||
|
||||
#remark()
|
||||
And now, we're done! \
|
||||
We've shown that `Q_sqrt(x)` approximates $1/sqrt(x)$ fairly well, \
|
||||
thanks to the approximation $log(1+a) = a + epsilon$.
|
||||
We've shown that `Q_sqrt(x)` approximates $1/sqrt(x)$ fairly well. \
|
||||
|
||||
#v(2mm)
|
||||
|
Loading…
x
Reference in New Issue
Block a user