From a166aeaa7484f769e8a103302e2a4d91f22e1171 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 12 Feb 2025 17:28:36 -0800 Subject: [PATCH] Added introduction --- src/Advanced/Fast Inverse Root/main.typ | 13 +++--- .../Fast Inverse Root/parts/00 intro.typ | 45 +++++++++++++++++++ .../parts/{00 int.typ => 01 int.typ} | 0 .../parts/{01 float.typ => 02 float.typ} | 0 .../parts/{02 approx.typ => 03 approx.typ} | 0 .../parts/{03 quake.typ => 04 quake.typ} | 10 ++--- .../parts/{04 bonus.typ => 05 bonus.typ} | 0 7 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 src/Advanced/Fast Inverse Root/parts/00 intro.typ rename src/Advanced/Fast Inverse Root/parts/{00 int.typ => 01 int.typ} (100%) rename src/Advanced/Fast Inverse Root/parts/{01 float.typ => 02 float.typ} (100%) rename src/Advanced/Fast Inverse Root/parts/{02 approx.typ => 03 approx.typ} (100%) rename src/Advanced/Fast Inverse Root/parts/{03 quake.typ => 04 quake.typ} (97%) rename src/Advanced/Fast Inverse Root/parts/{04 bonus.typ => 05 bonus.typ} (100%) diff --git a/src/Advanced/Fast Inverse Root/main.typ b/src/Advanced/Fast Inverse Root/main.typ index 71043b5..e36d184 100644 --- a/src/Advanced/Fast Inverse Root/main.typ +++ b/src/Advanced/Fast Inverse Root/main.typ @@ -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" diff --git a/src/Advanced/Fast Inverse Root/parts/00 intro.typ b/src/Advanced/Fast Inverse Root/parts/00 intro.typ new file mode 100644 index 0000000..e75dbbd --- /dev/null +++ b/src/Advanced/Fast Inverse Root/parts/00 intro.typ @@ -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. diff --git a/src/Advanced/Fast Inverse Root/parts/00 int.typ b/src/Advanced/Fast Inverse Root/parts/01 int.typ similarity index 100% rename from src/Advanced/Fast Inverse Root/parts/00 int.typ rename to src/Advanced/Fast Inverse Root/parts/01 int.typ diff --git a/src/Advanced/Fast Inverse Root/parts/01 float.typ b/src/Advanced/Fast Inverse Root/parts/02 float.typ similarity index 100% rename from src/Advanced/Fast Inverse Root/parts/01 float.typ rename to src/Advanced/Fast Inverse Root/parts/02 float.typ diff --git a/src/Advanced/Fast Inverse Root/parts/02 approx.typ b/src/Advanced/Fast Inverse Root/parts/03 approx.typ similarity index 100% rename from src/Advanced/Fast Inverse Root/parts/02 approx.typ rename to src/Advanced/Fast Inverse Root/parts/03 approx.typ diff --git a/src/Advanced/Fast Inverse Root/parts/03 quake.typ b/src/Advanced/Fast Inverse Root/parts/04 quake.typ similarity index 97% rename from src/Advanced/Fast Inverse Root/parts/03 quake.typ rename to src/Advanced/Fast Inverse Root/parts/04 quake.typ index 3d92755..3ba0474 100644 --- a/src/Advanced/Fast Inverse Root/parts/03 quake.typ +++ b/src/Advanced/Fast Inverse Root/parts/04 quake.typ @@ -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) diff --git a/src/Advanced/Fast Inverse Root/parts/04 bonus.typ b/src/Advanced/Fast Inverse Root/parts/05 bonus.typ similarity index 100% rename from src/Advanced/Fast Inverse Root/parts/04 bonus.typ rename to src/Advanced/Fast Inverse Root/parts/05 bonus.typ