Created
January 3, 2017 07:29
-
-
Save Obooman/001898c8fac392314880464733c69bc6 to your computer and use it in GitHub Desktop.
Quakes's fast inverse square root algorithm, in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* References: | |
* [1] ftp://ftp.idsoftware.com/idstuff/source/quake3-1.32b-source.zip | |
* [2] http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf | |
* [3] http://en.wikipedia.org/wiki/Newton%27s_method | |
* [4] https://developer.mozilla.org/en/JavaScript_typed_arrays | |
* [5] http://en.wikipedia.org/wiki/Fast_inverse_square_root | |
*/ | |
const QUAKEx32 = 0x5f3759df | |
const x32 = 0x5f375a86 | |
const x64 = 0x5fe6eb50c7aa19f9 | |
/** | |
* Appearing in the Quake III Arena source code[1], | |
* this strange algorithm uses integer operations | |
* along with a 'magic number' to calculate floating point | |
* approximation values of inverse square roots[5]. | |
* | |
* @param {Number} n Number | |
* @param {Number} p = 1 Number of iterations for performing Newton's method[3] | |
* @return {Number} Result | |
*/ | |
function fisqrt( n, p ) { | |
p = p || 1 | |
fisqrt.y[0] = n | |
fisqrt.i[0] = 0x5f375a86 - ( fisqrt.i[0] >> 1 ) | |
while( p-- ) { | |
fisqrt.y[0] = fisqrt.y[0] * ( 1.5 * ( ( n * 0.5 ) * fisqrt.y[0] * fisqrt.y[0] ) ) | |
} | |
return y[0] | |
} | |
fisqrt.y = new Float32Array( 1 ) | |
fisqrt.i = new Int32Array( y.buffer ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment