Fast inverse square root
![](/wiki/images/thumb/5/53/OpenArena-Rocket.jpg/250px-OpenArena-Rocket.jpg)
Fast inverse square root, sometimes referred to as Fast InvSqrt() or by the hexadecimal constant 0x5F3759DF, is an algorithm that estimates [math]\displaystyle{ \frac{1}{\sqrt{x}} }[/math], the reciprocal (or multiplicative inverse) of the square root of a 32-bit floating-point number [math]\displaystyle{ x }[/math] in IEEE 754 floating-point format. The algorithm is best known for its implementation in 1999 in Quake III Arena, a first-person shooter video game heavily based on 3D graphics. With subsequent hardware advancements, especially the x86 SSE instruction rsqrtss
, this algorithm is not generally the best choice for modern computers,[1] though it remains an interesting historical example.[2]
The algorithm accepts a 32-bit floating-point number as the input and stores a halved value for later use. Then, treating the bits representing the floating-point number as a 32-bit integer, a logical shift right by one bit is performed and the result subtracted from the number 0x5F3759DF, which is a floating-point representation of an approximation of [math]\displaystyle{ \sqrt{2^{127}} }[/math].[3] This results in the first approximation of the inverse square root of the input. Treating the bits again as a floating-point number, it runs one iteration of Newton's method, yielding a more precise approximation.
History
William Kahan and K.C. Ng at Berkeley wrote an unpublished paper in May 1986 describing how to calculate the square root using bit-fiddling techniques followed by Newton iterations.[4] In the late 1980s, Cleve Moler at Ardent Computer learned about this technique[5] and passed it along to his coworker Greg Walsh. Greg Walsh devised the now-famous constant and fast inverse square root algorithm. Gary Tarolli was consulting for Kubota, the company funding Ardent at the time, and likely brought the algorithm to 3dfx Interactive circa 1994.[6][7]
Jim Blinn demonstrated a simple approximation of the inverse square root in a 1997 column for IEEE Computer Graphics and Applications.[8] Reverse engineering of other contemporary 3D video games uncovered a variation of the algorithm in Activision's 1997 Interstate '76.[9]
Quake III Arena, a first-person shooter video game, was released in 1999 by id Software and used the algorithm. Brian Hook may have brought the algorithm from 3dfx to id Software.[6] A discussion of the code appeared on the Chinese developer forum CSDN in 2000,[10] and Usenet and the gamedev.net forum spread the code widely in 2002 and 2003.[11] Speculation arose as to who wrote the algorithm and how the constant was derived; some guessed John Carmack.[7] Quake III's full source code was released at QuakeCon 2005, but provided no answers. The authorship question was answered in 2006 when Greg Walsh contacted Beyond3D as their speculation gained popularity on Slashdot.[6]
In 2007 the algorithm was implemented in some dedicated hardware vertex shaders using field-programmable gate arrays (FPGA).[12][13]
Motivation
The inverse square root of a floating point number is used in digital signal processing to normalize a vector, scaling it to length 1 to produce a unit vector.[14] For example, computer graphics programs use inverse square roots to compute angles of incidence and reflection for lighting and shading. 3D graphics programs must perform millions of these calculations every second to simulate lighting. When the code was developed in the early 1990s, most floating point processing power lagged the speed of integer processing.[7] This was troublesome for 3D graphics programs before the advent of specialized hardware to handle transform and lighting. Computation of square roots usually depends upon many division operations, which for floating point numbers are computationally expensive. The fast inverse square generates a good approximation with only one division step.
The length of the vector is determined by calculating its Euclidean norm: the square root of the sum of squares of the vector components. When each component of the vector is divided by that length, the new vector will be a unit vector pointing in the same direction. In a 3D graphics program, all vectors are in three-dimensional space, so [math]\displaystyle{ \boldsymbol v }[/math] would be a vector [math]\displaystyle{ (v_1, v_2, v_3) }[/math].
- [math]\displaystyle{ \|\boldsymbol{v}\| = \sqrt{v_1^2+v_2^2+v_3^2} }[/math]
is the Euclidean norm of the vector.
- [math]\displaystyle{ \boldsymbol{\hat{v}} = \frac{\boldsymbol{v}}{\left\|\boldsymbol{v}\right\|} }[/math]
is the normalized (unit) vector. Substituting [math]\displaystyle{ \|\boldsymbol v\| }[/math], the equation can also be written as:
- [math]\displaystyle{ \boldsymbol{\hat{v}} = \frac{\boldsymbol{v}}\sqrt{v_1^2+v_2^2+v_3^2} }[/math]
which relates the unit vector to the inverse square root of the distance components. The inverse square root can be used to compute [math]\displaystyle{ \boldsymbol{\hat{v}} }[/math] because this equation is equivalent to
- [math]\displaystyle{ \boldsymbol{\hat{v}} = \boldsymbol{v}\, \frac{1}\sqrt{v_1^2+v_2^2+v_3^2} }[/math]
where the fraction term is the inverse square root of [math]\displaystyle{ v_1^2+v_2^2+v_3^2 }[/math].
At the time, floating-point division was generally expensive compared to multiplication; the fast inverse square root algorithm bypassed the division step, giving it its performance advantage.
Overview of the code
The following code is the fast inverse square root implementation from Quake III Arena, stripped of C preprocessor directives, but including the exact original comment text:[15]
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 ); // what the fuck? 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; }
At the time, the general method to compute the inverse square root was to calculate an approximation for [math]\displaystyle{ \frac{1}{\sqrt{x}} }[/math], then revise that approximation via another method until it came within an acceptable error range of the actual result. Common software methods in the early 1990s drew approximations from a lookup table.[16] The key of the fast inverse square root was to directly compute an approximation by utilizing the structure of floating-point numbers, proving faster than table lookups. The algorithm was approximately four times faster than computing the square root with another method and calculating the reciprocal via floating-point division.[17] The algorithm was designed with the IEEE 754-1985 32-bit floating-point specification in mind, but investigation from Chris Lomont showed that it could be implemented in other floating-point specifications.[18]
The advantages in speed offered by the fast inverse square root trick came from treating the 32-bit floating-point word[note 1] as an integer, then subtracting it from a "magic" constant, 0x5F3759DF.[7][19][20][21] This integer subtraction and bit shift results in a bit pattern which, when re-defined as a floating-point number, is a rough approximation for the inverse square root of the number. One iteration of Newton's method is performed to gain some accuracy, and the code is finished. The algorithm generates reasonably accurate results using a unique first approximation for Newton's method; however, it is much slower and less accurate than using the SSE instruction rsqrtss
on x86 processors also released in 1999.[1][22]
Worked example
As an example, the number [math]\displaystyle{ x=0.15625 }[/math] can be used to calculate [math]\displaystyle{ \frac{1}{\sqrt{x}} \approx 2.52982 }[/math]. The first steps of the algorithm are illustrated below:
0011_1110_0010_0000_0000_0000_0000_0000 Bit pattern of both x and i 0001_1111_0001_0000_0000_0000_0000_0000 Shift right one position: (i >> 1) 0101_1111_0011_0111_0101_1001_1101_1111 The magic number 0x5F3759DF 0100_0000_0010_0111_0101_1001_1101_1111 The result of 0x5F3759DF - (i >> 1)
Interpreting as IEEE 32-bit representation:
0_01111100_01000000000000000000000 1.25 × 2−3 0_00111110_00100000000000000000000 1.125 × 2−65 0_10111110_01101110101100111011111 1.432430... × 263 0_10000000_01001110101100111011111 1.307430... × 21
Reinterpreting this last bit pattern as a floating point number gives the approximation [math]\displaystyle{ y=2.61486 }[/math], which has an error of about 3.4%. After one single iteration of Newton's method, the final result is [math]\displaystyle{ y=2.52549 }[/math], an error of only 0.17%.
Avoiding undefined behavior
According to the C standard, reinterpreting a floating point value as an integer by casting then dereferencing the pointer to it is not valid (undefined behavior). Another way would be to place the floating point value in an anonymous union containing an additional 32-bit unsigned integer member, and accesses to that integer provides a bit level view of the contents of the floating point value. However, type punning through a union is also undefined behavior in C++.
# include <stdint.h> // uint32_t float q_rsqrt(float number) { union { float f; uint32_t i; } conv = { .f = number }; conv.i = 0x5f3759df - (conv.i >> 1); conv.f *= 1.5F - (number * 0.5F * conv.f * conv.f); return conv.f; }
In modern C++, the usual method for implementing this function's casts is through C++20's std::bit_cast
. This also allows the function to work in constexpr
context:
# include <bit> # include <limits> # include <cstdint> constexpr float Q_rsqrt(float number) noexcept { static_assert(std::numeric_limits<float>::is_iec559); // (enable only on IEEE 754) float const y = std::bit_cast<float>( 0x5f3759df - (std::bit_cast<std::uint32_t>(number) >> 1)); return y * (1.5f - (number * 0.5f * y * y)); }
Algorithm
The algorithm computes [math]\displaystyle{ \frac{1}{\sqrt{x}} }[/math] by performing the following steps:
- Alias the argument [math]\displaystyle{ x }[/math] to an integer as a way to compute an approximation of the binary logarithm [math]\displaystyle{ \log_{2}(x) }[/math]
- Use this approximation to compute an approximation of [math]\displaystyle{ \log_{2}\left(\frac{1}{\sqrt{x}}\right) = -\frac{1}{2} \log_{2}(x) }[/math]
- Alias back to a float, as a way to compute an approximation of the base-2 exponential
- Refine the approximation using a single iteration of Newton's method.
Floating-point representation
Since this algorithm relies heavily on the bit-level representation of single-precision floating-point numbers, a short overview of this representation is provided here. To encode a non-zero real number [math]\displaystyle{ x }[/math] as a single precision float, the first step is to write [math]\displaystyle{ x }[/math] as a normalized binary number:[23]
- [math]\displaystyle{ \begin{align} x &= \pm 1.b_1b_2b_3\ldots \times 2^{e_x} \end{align} }[/math]
where the exponent [math]\displaystyle{ e_x }[/math] is an integer, and [math]\displaystyle{ 1.b_1b_2b_3\ldots }[/math] is the binary representation of the significand. Since the single bit before the point in the significand is always 1, it does not need be stored. The equation can be rewritten as:
- [math]\displaystyle{ \begin{align} x &= \pm 2^{e_x} (1 + m_x) \end{align} }[/math]
where [math]\displaystyle{ m_x }[/math] means [math]\displaystyle{ 0.b_1b_2b_3\ldots }[/math], so [math]\displaystyle{ m_x \in [0, 1) }[/math]. From this form, three unsigned integers are computed:[24]
- [math]\displaystyle{ S_x }[/math], the "sign bit", is [math]\displaystyle{ 0 }[/math] if [math]\displaystyle{ x }[/math] is positive and [math]\displaystyle{ 1 }[/math] negative or zero (1 bit)
- [math]\displaystyle{ E_x = e_x + B }[/math] is the "biased exponent", where [math]\displaystyle{ B = 127 }[/math] is the "exponent bias"[note 2] (8 bits)
- [math]\displaystyle{ M_x = m_x \times L }[/math], where [math]\displaystyle{ L = 2^{23} }[/math][note 3] (23 bits)
These fields are then packed, left to right, into a 32-bit container.[25]
As an example, consider again the number [math]\displaystyle{ x = 0.15625 = 0.00101_2 }[/math]. Normalizing [math]\displaystyle{ x }[/math] yields:
- [math]\displaystyle{ x = +2^{-3}(1 + 0.25) }[/math]
and thus, the three unsigned integer fields are:
- [math]\displaystyle{ S = 0 }[/math]
- [math]\displaystyle{ E = -3 + 127 = 124 = 0111\ 1100_2 }[/math]
- [math]\displaystyle{ M = 0.25 \times 2^{23} = 2\ 097\ 152 = 0010\ 0000\ 0000\ 0000\ 0000\ 0000_2 }[/math]
these fields are packed as shown in the figure below:
Aliasing to an integer as an approximate logarithm
If [math]\displaystyle{ \frac{1}{\sqrt{x}} }[/math] were to be calculated without a computer or a calculator, a table of logarithms would be useful, together with the identity [math]\displaystyle{ \log_b\left(\frac{1}{\sqrt{x}}\right) = \log_b\left(x^{-\frac{1}{2}}\right) = -\frac{1}{2} \log_b(x) }[/math], which is valid for every base [math]\displaystyle{ b }[/math]. The fast inverse square root is based on this identity, and on the fact that aliasing a float32 to an integer gives a rough approximation of its logarithm. Here is how:
If [math]\displaystyle{ x }[/math] is a positive normal number:
- [math]\displaystyle{ x = 2^{e_x} (1 + m_x) }[/math]
then
- [math]\displaystyle{ \log_2(x) = e_x + \log_2(1 + m_x) }[/math]
and since [math]\displaystyle{ m_x \in [0, 1) }[/math], the logarithm on the right-hand side can be approximated by[26]
- [math]\displaystyle{ \log_2(1 + m_x) \approx m_x + \sigma }[/math]
where [math]\displaystyle{ \sigma }[/math] is a free parameter used to tune the approximation. For example, [math]\displaystyle{ \sigma = 0 }[/math] yields exact results at both ends of the interval, while [math]\displaystyle{ \sigma = \frac{1}{2} - \frac{1+\ln(\ln(2))}{2\ln(2)} \approx 0.0430357 }[/math] yields the optimal approximation (the best in the sense of the uniform norm of the error). However, this value is not used by the algorithm as it does not take subsequent steps into account.
Thus there is the approximation
- [math]\displaystyle{ \log_2(x) \approx e_x + m_x + \sigma. }[/math]
Interpreting the floating-point bit-pattern of [math]\displaystyle{ x }[/math] as an integer [math]\displaystyle{ I_x }[/math] yields[note 4]
- [math]\displaystyle{ \begin{align} I_x &= E_x L + M_x\\ &= L (e_x + B + m_x)\\ &= L (e_x + m_x + \sigma + B - \sigma)\\ &\approx L \log_2(x) + L (B - \sigma). \end{align} }[/math]
It then appears that [math]\displaystyle{ I_x }[/math] is a scaled and shifted piecewise-linear approximation of [math]\displaystyle{ \log_2(x) }[/math], as illustrated in the figure on the right. In other words, [math]\displaystyle{ \log_2(x) }[/math] is approximated by
- [math]\displaystyle{ \log_2(x) \approx \frac{I_x}{L} - (B - \sigma). }[/math]
First approximation of the result
The calculation of [math]\displaystyle{ y=\frac{1}{\sqrt{x}} }[/math] is based on the identity
- [math]\displaystyle{ \log_2(y) = - \tfrac{1}{2}\log_2(x) }[/math]
Using the approximation of the logarithm above, applied to both [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math], the above equation gives:
- [math]\displaystyle{ \frac{I_y}{L} - (B - \sigma) \approx - \frac{1}{2}\left(\frac{I_x}{L} - (B - \sigma)\right) }[/math]
Thus, an approximation of [math]\displaystyle{ I_y }[/math] is:
- [math]\displaystyle{ I_y \approx \tfrac{3}{2} L (B - \sigma) - \tfrac{1}{2} I_x }[/math]
which is written in the code as
i = 0x5f3759df - ( i >> 1 );
The first term above is the magic number
- [math]\displaystyle{ \tfrac{3}{2} L (B - \sigma) = \mathtt{0x5F3759DF} }[/math]
from which it can be inferred that [math]\displaystyle{ \sigma \approx 0.0450466 }[/math]. The second term, [math]\displaystyle{ \frac{1}{2}I_x }[/math], is calculated by shifting the bits of [math]\displaystyle{ I_x }[/math] one position to the right.[27]
Newton's method
With [math]\displaystyle{ y }[/math] as the inverse square root, [math]\displaystyle{ f(y)=\frac{1}{y^2}-x=0 }[/math]. The approximation yielded by the earlier steps can be refined by using a root-finding method, a method that finds the zero of a function. The algorithm uses Newton's method: if there is an approximation, [math]\displaystyle{ y_n }[/math] for [math]\displaystyle{ y }[/math], then a better approximation [math]\displaystyle{ y_{n+1} }[/math] can be calculated by taking [math]\displaystyle{ y_n - \frac{f(y_n)}{f'(y_n)} }[/math], where [math]\displaystyle{ f'(y_n) }[/math] is the derivative of [math]\displaystyle{ f(y) }[/math] at [math]\displaystyle{ y_n }[/math].[28] For the above [math]\displaystyle{ f(y) }[/math],
- [math]\displaystyle{ y_{n+1} = \frac{y_{n}\left(3-xy_n^2\right)}{2} }[/math]
where [math]\displaystyle{ f(y)= \frac{1}{y^2} - x }[/math] and [math]\displaystyle{ f'(y) = -\frac{2}{y^3} }[/math].
Treating [math]\displaystyle{ y }[/math] as a floating-point number, y = y*(threehalfs - x/2*y*y);
is equivalent to
- [math]\displaystyle{ y_{n+1} = y_{n}\left (\frac32-\frac{x}{2}y_n^2\right ) = \frac{y_{n}\left(3-xy_n^2\right)}{2}. }[/math]
By repeating this step, using the output of the function ([math]\displaystyle{ y_{n+1} }[/math]) as the input of the next iteration, the algorithm causes [math]\displaystyle{ y }[/math] to converge to the inverse square root.[29] For the purposes of the Quake III engine, only one iteration was used. A second iteration remained in the code but was commented out.[21]
Accuracy
As noted above, the approximation is very accurate. The single graph on the right plots the error of the function (that is, the error of the approximation after it has been improved by running one iteration of Newton's method), for inputs starting at 0.01, where the standard library gives 10.0 as a result, and InvSqrt() gives 9.982522, making the relative difference 0.0017478, or 0.175% of the true value, 10. The absolute error only drops from then on, and the relative error stays within the same bounds across all orders of magnitude.
Subsequent improvements
Magic number
It is not known precisely how the exact value for the magic number was determined. Chris Lomont developed a function to minimize approximation error by choosing the magic number [math]\displaystyle{ R }[/math] over a range. He first computed the optimal constant for the linear approximation step as 0x5F37642F, close to 0x5F3759DF, but this new constant gave slightly less accuracy after one iteration of Newton's method.[30] Lomont then searched for a constant optimal even after one and two Newton iterations and found 0x5F375A86, which is more accurate than the original at every iteration stage.[30] He concluded by asking whether the exact value of the original constant was chosen through derivation or trial and error.[31] Lomont said that the magic number for 64-bit IEEE754 size type double is 0x5FE6EC85E7DE30DA, but it was later shown by Matthew Robertson to be exactly 0x5FE6EB50C7B537A9.[32]
Jan Kadlec reduced the relative error by a further factor of 2.7 by adjusting the constants in the single Newton's method iteration as well,[33] arriving after an exhaustive search at
conv.i = 0x5F1FFFF9 - ( conv.i >> 1 ); conv.f *= 0.703952253f * ( 2.38924456f - x * conv.f * conv.f ); return conv.f;
A complete mathematical analysis for determining the magic number is now available for single-precision floating-point numbers.[34][35]
Zero finding
Intermediate to the use of one vs. two iterations of Newton's method in terms of speed and accuracy is a single iteration of Halley's method. In this case, Halley's method is equivalent to applying Newton's method with the starting formula [math]\displaystyle{ f(y) = \frac{1}{y^{1/2}} - xy^{3/2} = 0 }[/math]. The update step is then
- [math]\displaystyle{ y_{n+1} = y_{n} - \frac{f(y_n)}{f'(y_n)} = y_n \left(\frac{3 + xy_n^2}{1 + 3xy_n^2}\right), }[/math]
where the implementation should calculate [math]\displaystyle{ xy_n^2 }[/math] only once, via a temporary variable.
Obsolescence
Subsequent additions by hardware manufacturers have made this algorithm redundant for the most part. For example, on x86, Intel introduced the SSE instruction rsqrtss
in 1999. In a 2009 benchmark on the Intel Core 2, this instruction took 0.85ns per float compared to 3.54ns for the fast inverse square root algorithm, and had less error.[1]
Some low-cost embedded systems do not have specialized square root instructions. However, manufacturers of these systems usually provide trigonometric and other math libraries, based on algorithms such as CORDIC. Most applications in this area are not performance-sensitive and a specialized fast inverse square root algorithm is not needed.[citation needed]
See also
- Methods of computing square roots § Approximations that depend on the floating point representation
- Magic number
Notes
- ↑ Use of the type
long
reduces the portability of this code on modern systems. For the code to execute properly,sizeof(long)
must be 4 bytes, otherwise negative outputs may result. Under many modern 64-bit systems,sizeof(long)
is 8 bytes. The more portable replacement isint32_t
. - ↑ [math]\displaystyle{ E_x }[/math] should be in the range [math]\displaystyle{ [1, 254] }[/math] for [math]\displaystyle{ x }[/math] to be representable as a normal number.
- ↑ The only real numbers that can be represented exactly as floating point are those for which [math]\displaystyle{ M_x }[/math] is an integer. Other numbers can only be represented approximately by rounding them to the nearest exactly representable number.
- ↑ Since [math]\displaystyle{ x }[/math] is positive, [math]\displaystyle{ S_x = 0 }[/math].
References
- ↑ 1.0 1.1 1.2 Ruskin, Elan (2009-10-16). "Timing square root". Some Assembly Required. http://assemblyrequired.crashworks.org/timing-square-root/.
- ↑ feilipu. "z88dk is a collection of software development tools that targets the 8080 and z80 computers.". https://github.com/z88dk/z88dk/tree/master/libsrc/_DEVELOPMENT/math/float/math32#sqrt-and-invsqrt.
- ↑ Munafo, Robert. "Notable Properties of Specific Numbers". https://mrob.com/pub/math/numbers-16.html#le009_16.
- ↑ "sqrt implementation in fdlibm - See W. Kahan and K.C. Ng's discussion in comments in lower half of this code". http://www.netlib.org/fdlibm/e_sqrt.c.
- ↑ Moler, Cleve (19 June 2012). "Symplectic Spacewar". MATLAB. http://blogs.mathworks.com/cleve/2012/06/19/symplectic-spacewar/#comment-13.
- ↑ 6.0 6.1 6.2 Sommefeldt, Rys (2006-12-19). "Origin of Quake3's Fast InvSqrt() - Part Two". Beyond3D. http://www.beyond3d.com/content/articles/15/.
- ↑ 7.0 7.1 7.2 7.3 Sommefeldt, Rys (2006-11-29). "Origin of Quake3's Fast InvSqrt()". Beyond3D. http://www.beyond3d.com/content/articles/8/.
- ↑ Blinn 1997, pp. 80–84.
- ↑ Peelar, Shane (1 June 2021). "Fast reciprocal square root... in 1997?!". https://inbetweennames.net/blog/2021-05-06-i76rsqrt/.
- ↑ "Discussion on CSDN". http://bbs.csdn.net/topics/41888.
- ↑ Lomont 2003, p. 1-2.
- ↑ Zafar, Saad; Adapa, Raviteja (January 2014). "Hardware architecture design and mapping of 'Fast Inverse Square Root' algorithm". 2014 International Conference on Advances in Electrical Engineering (ICAEE). pp. 1–4. doi:10.1109/ICAEE.2014.6838433. ISBN 978-1-4799-3543-7. https://ieeexplore.ieee.org/document/6838433.
- ↑ Middendorf 2007, pp. 155–164.
- ↑ Blinn 2003, p. 130.
- ↑ "quake3-1.32b/code/game/q_math.c". Quake III Arena. id Software. https://github.com/id-Software/Quake-III-Arena/blob/master/code/game/q_math.c#L552.
- ↑ Eberly 2001, p. 504.
- ↑ Lomont 2003, p. 1.
- ↑ Lomont 2003.
- ↑ Lomont 2003, p. 3.
- ↑ McEniry 2007, p. 2, 16.
- ↑ 21.0 21.1 Eberly 2001, p. 2.
- ↑ Fog, Agner. "Lists of instruction latencies, throughputs and micro-operation breakdowns for Intel, AMD and VIA CPUs". http://www.agner.org/optimize/instruction_tables.pdf.
- ↑ Goldberg 1991, p. 7.
- ↑ Goldberg 1991, pp. 15–20.
- ↑ Goldberg 1991, p. 16.
- ↑ McEniry 2007, p. 3.
- ↑ Hennessey & Patterson 1998, p. 305.
- ↑ Hardy 1908, p. 323.
- ↑ McEniry 2007, p. 6.
- ↑ 30.0 30.1 Lomont 2003, p. 10.
- ↑ Lomont 2003, pp. 10–11.
- ↑ Matthew Robertson (2012-04-24). "A Brief History of InvSqrt". UNBSJ. https://mrober.io/papers/rsqrt.pdf.
- ↑ Kadlec, Jan (2010). "Řrřlog::Improving the fast inverse square root". http://rrrola.wz.cz/inv_sqrt.html.
- ↑ Moroz et al. 2018.
- ↑ Muller, Jean-Michel (December 2020). "Elementary Functions and Approximate Computing". Proceedings of the IEEE 108 (12): 2146. doi:10.1109/JPROC.2020.2991885. ISSN 0018-9219. https://ieeexplore.ieee.org/document/9106347.
Bibliography
- Blinn, Jim (July 1997). "Floating Point Tricks". IEEE Computer Graphics & Applications 17 (4): 80. doi:10.1109/38.595279.
- Blinn, Jim (2003). Jim Blinn's Corner: Notation, notation notation. Morgan Kaufmann. ISBN 1-55860-860-5.
- Eberly, David (2001). 3D Game Engine Design. Morgan Kaufmann. ISBN 978-1-55860-593-0. https://archive.org/details/isbn_9781558605930.
- Goldberg, David (1991). "What every computer scientist should know about floating-point arithmetic". ACM Computing Surveys 23 (1): 5–48. doi:10.1145/103162.103163.
- Hardy, Godfrey (1908). A Course of Pure Mathematics. Cambridge, UK: Cambridge University Press. ISBN 0-521-72055-9. http://www.gutenberg.org/files/38769.
- Hennessey, John; Patterson, David A. (1998). Computer Organization and Design (2nd ed.). San Francisco, CA: Morgan Kaufmann Publishers. ISBN 978-1-55860-491-9. https://archive.org/details/computerorganiz000henn.
- Lomont, Chris (February 2003). "Fast Inverse Square Root". http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf.
- McEniry, Charles (August 2007). "The Mathematics Behind the Fast Inverse Square Root Function Code". http://www.daxia.com/bibis/upload/406Fast_Inverse_Square_Root.pdf.
- Middendorf, Lars; Mühlbauer, Felix; Umlauf, George; Bodba, Christophe (June 1, 2007). "Embedded Vertex Shader in FPGA". in Rettberg, Achin. IFIP TC10 Working Conference:International Embedded Systems Symposium (IESS). et al.. Irvine, California: Springer. doi:10.1007/978-0-387-72258-0_14. ISBN 978-0-387-72257-3. https://link.springer.com/content/pdf/10.1007/978-0-387-72258-0_14.pdf.
- Striegel, Jason (2008-12-04). "Quake's fast inverse square root". Hackszine. O'Reilly Media. http://www.hackszine.com/blog/archive/2008/12/quakes_fast_inverse_square_roo.html.
- IEEE Computer Society (1985), 754-1985 - IEEE Standard for Binary Floating-Point Arithmetic, Institute of Electrical and Electronics Engineers, https://standards.ieee.org/findstds/standard/754-1985.html
- Moroz, Leonid V.; Walczyk, Cezary J.; Hrynchyshyn, Andriy; Holimath, Vijay; Cieslinski, Jan L. (January 2018). "Fast calculation of inverse square root with the use of magic constant analytical approach". Applied Mathematics and Computation (Elsevier Science Inc.) 316 (C): 245–255. doi:10.1016/j.amc.2017.08.025.
Further reading
- Kushner, David (August 2002). "The wizardry of Id". IEEE Spectrum 39 (8): 42–47. doi:10.1109/MSPEC.2002.1021943.
External links
- 0x5f3759df, further investigations into accuracy and generalizability of the algorithm by Christian Plesner Hansen
- Origin of Quake3's Fast InvSqrt()
- Quake III Arena source code (archived in Software Heritage)
- Implementation of InvSqrt in DESMOS
- "Fast Inverse Square Root — A Quake III Algorithm" (YouTube)
![]() | Original source: https://en.wikipedia.org/wiki/Fast inverse square root.
Read more |