Skip to content

Instantly share code, notes, and snippets.

@mikolalysenko
Created April 4, 2013 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikolalysenko/5311694 to your computer and use it in GitHub Desktop.
Save mikolalysenko/5311694 to your computer and use it in GitHub Desktop.
This is not as fast as 1.0 / Math.sqrt(number)
var FLOAT_VIEW = new Float32Array(1)
, INT_VIEW = new Uint32Array(FLOAT_VIEW.buffer)
function rsqrt(number) {
var threhalfs = 1.5
var x2 = 0.5 * number
FLOAT_VIEW[0] = number
INT_VIEW[0] = 0x5f3759df - ( INT_VIEW[0] >>> 1 )
FLOAT_VIEW[0] = FLOAT_VIEW[0] * ( 1.5 - ( x2 * FLOAT_VIEW[0] * FLOAT_VIEW[0] ) )
FLOAT_VIEW[0] = FLOAT_VIEW[0] * ( 1.5 - ( x2 * FLOAT_VIEW[0] * FLOAT_VIEW[0] ) )
FLOAT_VIEW[0] = FLOAT_VIEW[0] * ( 1.5 - ( x2 * FLOAT_VIEW[0] * FLOAT_VIEW[0] ) )
return FLOAT_VIEW[0]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment