This commit is contained in:
Mark 2025-02-08 18:22:28 -08:00
parent 4f4675a164
commit e92fae453e

View File

@ -14,23 +14,25 @@ float Q_rsqrt( float number ) {
} }
\end{lstlisting} \end{lstlisting}
This code defines This code defines a function \texttt{Q\_rsqrt} that consumes a float named
a function \texttt{Q\_rsqrt} that consumes a float named
\texttt{number} and approximates its inverse square root (in other words, \texttt{Q\_rsqrt} computes $1/\sqrt{\texttt{number}}$). \texttt{number} and approximates its inverse square root (in other words, \texttt{Q\_rsqrt} computes $1/\sqrt{\texttt{number}}$).
\vspace{2mm} \vspace{2mm}
If we rewrite this using the notation we're familiar with, we get the following: If we rewrite this using notation we're familiar with, we get the following:
\begin{equation*} \begin{equation*}
\texttt{Q\_sqrt}(n_i) = 6240089 - (n_i \div 2) \approx \frac{1}{\sqrt{n_f}} \texttt{Q\_sqrt}(n_f) = 6240089 - (n_i \div 2) \approx \frac{1}{\sqrt{n_f}}
\end{equation*} \end{equation*}
$6240089$ is the decimal value of hex \texttt{0x5f3759df}. \par
It is a magic number hard-coded into the function. \note{
\texttt{0x5f3759df} is $6240089$ in hexadecimal. \par
It is a magic number hard-coded into \texttt{Q\_sqrt}.
}
\vspace{2mm} \vspace{2mm}
Our goal in this section is to understand why this works. \par Our goal in this section is to understand why this works: \par
How are we able to approximate $\frac{1}{\sqrt{x}}$ by only subtracting and dividing by two?? How does Quake approximate $\frac{1}{\sqrt{x}}$ by simply subtracting and dividing by two??
\problem{} \problem{}
Using basic log rules, rewrite $\log_2(1 / \sqrt{x})$ in terms of $\log_2(x)$. Using basic log rules, rewrite $\log_2(1 / \sqrt{x})$ in terms of $\log_2(x)$.