Integral binary logarithm

Benchmark created on


Preparation HTML

<script>
  var log2a = function(x) {
      return Math.floor(Math.log(x) / Math.log(2));
      };
  
  var log2b = function(x) {
      var r = 0;
      while (x >>>= 1) r++;
      return r;
      };
  
  var log2c = function(x) {
      var r = 0;
      if (x >= 65536) {
        x >>>= 16;
        r += 16;
      }
      if (x >= 256) {
        x >>>= 8;
        r += 8;
      }
      if (x >= 16) {
        x >>>= 4;
        r += 4;
      }
      if (x >= 4) {
        x >>>= 2;
        r += 2;
      }
      if (x >= 2) r += 1;
      return r;
      };
  
  var log2d = function(x) {
      var r = 0;
      if (x & 0xFFFF0000) {
        x >>>= 16;
        r |= 16;
      }
      if (x & 0xFF00) {
        x >>>= 8;
        r |= 8;
      }
      if (x & 0xF0) {
        x >>>= 4;
        r |= 4;
      }
      if (x & 0xC) {
        x >>>= 2;
        r |= 2;
      }
      if (x & 0x2) {
        x >>>= 1;
        r |= 1;
      }
      return r;
      };
  
  var log2e = function(x) {
      var r = (x > 0xFFFF) << 4;
      x >>>= r;
      var s = (x > 0xFF) << 3;
      x >>>= s;
      r |= s;
      s = (x > 0xF) << 2;
      x >>>= s;
      r |= s;
      s = (x > 0x3) << 1;
      x >>>= s;
      r |= s;
      r |= (x >> 1);
      return r;
      };
  
  var log2f = function(x) {
      var lgt = [-1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7];
      var r, t, tt;
      if (tt = x >>> 16) r = (t = tt >>> 8) ? 24 + lgt[t] : 16 + lgt[tt];
      else r = (t = x >>> 8) ? 8 + lgt[t] : lgt[x];
      return r;
      };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Math.log
log2a(0), log2a(1), log2a(2), log2a(3), log2a(3423), log2a(4366), log2a(5045), log2a(17205), log2a(45425), log2a(65534), log2a(65535), log2a(65536), log2a(595028), log2a(7198810), log2a(18538624), log2a(35002749), log2a(63529767), log2a(314280517), log2a(2881824439), log2a(4294967295);
ready
Naïve bits counting
log2b(0), log2b(1), log2b(2), log2b(3), log2b(3423), log2b(4366), log2b(5045), log2b(17205), log2b(45425), log2b(65534), log2b(65535), log2b(65536), log2b(595028), log2b(7198810), log2b(18538624), log2b(35002749), log2b(63529767), log2b(314280517), log2b(2881824439), log2b(4294967295);
ready
Compare and branch
log2c(0), log2c(1), log2c(2), log2c(3), log2c(3423), log2c(4366), log2c(5045), log2c(17205), log2c(45425), log2c(65534), log2c(65535), log2c(65536), log2c(595028), log2c(7198810), log2c(18538624), log2c(35002749), log2c(63529767), log2c(314280517), log2c(2881824439), log2c(4294967295);
ready
Better compare and branch
log2d(0), log2d(1), log2d(2), log2d(3), log2d(3423), log2d(4366), log2d(5045), log2d(17205), log2d(45425), log2d(65534), log2d(65535), log2d(65536), log2d(595028), log2d(7198810), log2d(18538624), log2d(35002749), log2d(63529767), log2d(314280517), log2d(2881824439), log2d(4294967295);
ready
Compare without branching
log2e(0), log2e(1), log2e(2), log2e(3), log2e(3423), log2e(4366), log2e(5045), log2e(17205), log2e(45425), log2e(65534), log2e(65535), log2e(65536), log2e(595028), log2e(7198810), log2e(18538624), log2e(35002749), log2e(63529767), log2e(314280517), log2e(2881824439), log2e(4294967295);
ready
Table lookup
log2f(0), log2f(1), log2f(2), log2f(3), log2f(3423), log2f(4366), log2f(5045), log2f(17205), log2f(45425), log2f(65534), log2f(65535), log2f(65536), log2f(595028), log2f(7198810), log2f(18538624), log2f(35002749), log2f(63529767), log2f(314280517), log2f(2881824439), log2f(4294967295);
ready

Revisions

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