Inverse Square Root (v4)

Revision 4 of this benchmark created on


Description

A Flash and C trick ported to JS array buffers. Doesn't look like a gain yet.

There were serious problems in previous revisions, the Quake Inverse SQRT code wasnt returning the correct values. This should correct it.

Preparation HTML

<script>
  var Math_sqrt = Math.sqrt;
  
  var x2 = new Float32Array(1);
  var y = new Float32Array(1);
  
  function InvSqrt(number) {
   x2[0] = number * 0.5;
   y[0] = number;
   var threehalfs = 1.5;
  
   var i = new Int32Array(y.buffer);
  
   i[0] = 0x5f3759df - (i[0] >> 1);
  
   y = new Float32Array(i.buffer);
  
   y[0] = y[0] * (threehalfs - (x2[0] * y[0] * y[0]));
  
   return y[0];
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Classic
var result;

for (var i = 0; i < 1000; i++) {
 result = 1.0 / Math_sqrt(i);
}
ready
Quake inverse sqrt
var result;

for (var i = 0; i < 1000; i++) {
 result = InvSqrt(i);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.