Reviewers: jarin,

Message:
This CL depends on https://codereview.chromium.org/178583006/ for introducing
%_DoubleHi and %_ConstructDouble.

Description:
Harmony: implement Math.cbrt in Javascript.

Please review this at https://codereview.chromium.org/183743018/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files (+25, -33 lines):
  M src/harmony-math.js
  M src/runtime.h
  M src/runtime.cc


Index: src/harmony-math.js
diff --git a/src/harmony-math.js b/src/harmony-math.js
index 7856917890b5f3f4c0369a8731c6892a8ef57346..d15178c5782a47bcbca1e4161962247a958a1ef4 100644
--- a/src/harmony-math.js
+++ b/src/harmony-math.js
@@ -174,9 +174,32 @@ function MathClz32(x) {
 }


-//ES6 draft 09-27-13, section 20.2.2.9.
+// ES6 draft 09-27-13, section 20.2.2.9.
+// Cube root approximation, refer to: http://metamerist.com/cbrt/cbrt.htm
+// Using initial approximation adapted from Kahan's cbrt and 4 iterations
+// of Newton's method.
 function MathCbrt(x) {
-  return %Math_cbrt(TO_NUMBER_INLINE(x));
+  if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
+  if (x == 0 || !NUMBER_IS_FINITE(x)) return x;
+  return x >= 0 ? CubeRoot(x) : -CubeRoot(-x);
+}
+
+macro NEWTON_ITERATION_CBRT(x, approx)
+  (1.0 / 3.0) * (x / (approx * approx) + 2 * approx);
+endmacro
+
+function CubeRoot(x) {
+  var approx_hi = MathFloor(%_DoubleHi(x) / 3) + 0x2A9F7893;
+  var approx = %_ConstructDouble(approx_hi, 0);
+  approx = NEWTON_ITERATION_CBRT(x, approx);
+  approx = NEWTON_ITERATION_CBRT(x, approx);
+  approx = NEWTON_ITERATION_CBRT(x, approx);
+  return NEWTON_ITERATION_CBRT(x, approx);
+}
+
+
+function NewtonIterationCbrt(x, approx) {
+  return (1.0 / 3.0) * (x / (approx * approx) + 2 * approx);
 }


Index: src/runtime.cc
diff --git a/src/runtime.cc b/src/runtime.cc
index 24dc60f0bc5b76cbe3963a20e5bff4f958739afc..46021ad9bb6f5c46529846c501ff718b33771589 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -7708,36 +7708,6 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_ConstructDouble) {
 }


-// Cube root approximation, refer to: http://metamerist.com/cbrt/cbrt.htm
-// Using initial approximation adapted from Kahan's cbrt and 4 iterations
-// of Newton's method.
-inline double CubeRootNewtonIteration(double approx, double x) {
-  return (1.0 / 3.0) * (x / (approx * approx) + 2 * approx);
-}
-
-
-inline double CubeRoot(double x) {
-  static const uint64_t magic = V8_2PART_UINT64_C(0x2A9F7893, 00000000);
-  uint64_t xhigh = double_to_uint64(x);
-  double approx = uint64_to_double(xhigh / 3 + magic);
-
-  approx = CubeRootNewtonIteration(approx, x);
-  approx = CubeRootNewtonIteration(approx, x);
-  approx = CubeRootNewtonIteration(approx, x);
-  return CubeRootNewtonIteration(approx, x);
-}
-
-
-RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_cbrt) {
-  SealHandleScope shs(isolate);
-  ASSERT(args.length() == 1);
-  CONVERT_DOUBLE_ARG_CHECKED(x, 0);
-  if (x == 0 || std::isinf(x)) return args[0];
-  double result = (x > 0) ? CubeRoot(x) : -CubeRoot(-x);
-  return isolate->heap()->AllocateHeapNumber(result);
-}
-
-
 RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_log1p) {
   SealHandleScope shs(isolate);
   ASSERT(args.length() == 1);
Index: src/runtime.h
diff --git a/src/runtime.h b/src/runtime.h
index 9fe1b3adeccc658c35017b8dd1a45fe9ea790b64..757b1f7fd4a95ee0cfb9625789267318044138b6 100644
--- a/src/runtime.h
+++ b/src/runtime.h
@@ -178,7 +178,6 @@ namespace internal {
   F(Math_asin, 1, 1) \
   F(Math_atan, 1, 1) \
   F(Math_log, 1, 1) \
-  F(Math_cbrt, 1, 1) \
   F(Math_log1p, 1, 1) \
   F(Math_expm1, 1, 1) \
   F(Math_sqrt, 1, 1) \


--
--
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