Revision: 19742
Author:   [email protected]
Date:     Mon Mar 10 08:21:18 2014 UTC
Log:      Harmony: implement Math.cbrt in Javascript.

[email protected]

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

Modified:
 /branches/bleeding_edge/src/harmony-math.js
 /branches/bleeding_edge/src/runtime.cc
 /branches/bleeding_edge/src/runtime.h

=======================================
--- /branches/bleeding_edge/src/harmony-math.js Fri Mar  7 08:42:10 2014 UTC
+++ /branches/bleeding_edge/src/harmony-math.js Mon Mar 10 08:21:18 2014 UTC
@@ -175,9 +175,28 @@


 // 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);
+}
+


 // ES6 draft 09-27-13, section 20.2.2.14.
=======================================
--- /branches/bleeding_edge/src/runtime.cc      Sat Mar  8 04:41:06 2014 UTC
+++ /branches/bleeding_edge/src/runtime.cc      Mon Mar 10 08:21:18 2014 UTC
@@ -7692,36 +7692,6 @@
   uint64_t result = (static_cast<uint64_t>(hi) << 32) | lo;
   return isolate->heap()->AllocateHeapNumber(uint64_to_double(result));
 }
-
-
-// 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);
-}


 static const double kPiDividedBy4 = 0.78539816339744830962;
=======================================
--- /branches/bleeding_edge/src/runtime.h       Fri Mar  7 14:58:41 2014 UTC
+++ /branches/bleeding_edge/src/runtime.h       Mon Mar 10 08:21:18 2014 UTC
@@ -175,7 +175,6 @@
   F(Math_asin, 1, 1) \
   F(Math_atan, 1, 1) \
   F(Math_log, 1, 1) \
-  F(Math_cbrt, 1, 1) \
   F(Math_sqrt, 1, 1) \
   F(Math_exp, 1, 1) \
   F(Math_floor, 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/d/optout.

Reply via email to