Revision: 19435
Author:   [email protected]
Date:     Tue Feb 18 10:49:35 2014 UTC
Log:      Harmony: implement Math.clz32

[email protected], [email protected]
BUG=v8:2938
LOG=N

Review URL: https://codereview.chromium.org/169783002
http://code.google.com/p/v8/source/detail?r=19435

Added:
 /branches/bleeding_edge/test/mjsunit/harmony/math-clz32.js
Modified:
 /branches/bleeding_edge/src/harmony-math.js

=======================================
--- /dev/null
+++ /branches/bleeding_edge/test/mjsunit/harmony/math-clz32.js Tue Feb 18 10:49:35 2014 UTC
@@ -0,0 +1,28 @@
+// 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.
+
+// Flags: --harmony-maths
+
+[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;
+}
+
+var max = Math.pow(2, 40);
+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; } }));
+}
=======================================
--- /branches/bleeding_edge/src/harmony-math.js Tue Feb 18 10:43:06 2014 UTC
+++ /branches/bleeding_edge/src/harmony-math.js Tue Feb 18 10:49:35 2014 UTC
@@ -158,6 +158,20 @@
 function MathFround(x) {
   return %Math_fround(TO_NUMBER_INLINE(x));
 }
+
+
+function MathClz32(x) {
+  x = ToUint32(TO_NUMBER_INLINE(x));
+  if (x == 0) return 32;
+  var result = 0;
+  // Binary search.
+  if ((x & 0xFFFF0000) === 0) { x <<= 16; result += 16; };
+  if ((x & 0xFF000000) === 0) { x <<=  8; result +=  8; };
+  if ((x & 0xF0000000) === 0) { x <<=  4; result +=  4; };
+  if ((x & 0xC0000000) === 0) { x <<=  2; result +=  2; };
+  if ((x & 0x80000000) === 0) { x <<=  1; result +=  1; };
+  return result;
+}


 function ExtendMath() {
@@ -176,8 +190,10 @@
     "log10", MathLog10,
     "log2", MathLog2,
     "hypot", MathHypot,
-    "fround", MathFround
+    "fround", MathFround,
+    "clz32", MathClz32
   ));
 }
+

 ExtendMath();

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to