math-clz32.js 888 Bytes
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flags: --allow-natives-syntax
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

[NaN, Infinity, -Infinity, 0, -0, "abc", "Infinity", "-Infinity", {}].forEach(
  function(x) {
    assertEquals(32, Math.clz32(x));
  }
);

function testclz(x) {
  for (var i = 0; i < 33; i++) {
    if (x & 0x80000000) return i;
    x <<= 1;
  }
  return 32;
}

21 22 23 24 25 26 27 28 29 30

function f(e) {
  var max = Math.pow(2, e);
  for (var x = 0; x < max; x = x * 1.01 + 1) {
    assertEquals(testclz(x), Math.clz32(x));
    assertEquals(testclz(-x), Math.clz32(-x));
    assertEquals(testclz(x), Math.clz32({ valueOf: function() { return x; } }));
    assertEquals(testclz(-x),
                 Math.clz32({ toString: function() { return -x; } }));
  }
31
}
32 33 34 35 36

f(5);
f(5);
%OptimizeFunctionOnNextCall(f);
f(40);