Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(899)

Unified Diff: src/harmony-math.js

Issue 196133017: Experimental parser: merge r19949 (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/harmony-array.js ('k') | src/heap.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/harmony-math.js
diff --git a/src/harmony-math.js b/src/harmony-math.js
index 7856917890b5f3f4c0369a8731c6892a8ef57346..298fa58cb2ab1d13acf1e7bf56f6a407cd93eef1 100644
--- a/src/harmony-math.js
+++ b/src/harmony-math.js
@@ -174,21 +174,70 @@ 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);
}
-//ES6 draft 09-27-13, section 20.2.2.14.
+
+// ES6 draft 09-27-13, section 20.2.2.14.
+// Use Taylor series to approximate.
+// exp(x) - 1 at 0 == -1 + exp(0) + exp'(0)*x/1! + exp''(0)*x^2/2! + ...
+// == x/1! + x^2/2! + x^3/3! + ...
+// The closer x is to 0, the fewer terms are required.
function MathExpm1(x) {
- return %Math_expm1(TO_NUMBER_INLINE(x));
+ if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
+ var xabs = MathAbs(x);
+ if (xabs < 2E-7) {
+ return x * (1 + x * (1/2));
+ } else if (xabs < 6E-5) {
+ return x * (1 + x * (1/2 + x * (1/6)));
+ } else if (xabs < 2E-2) {
+ return x * (1 + x * (1/2 + x * (1/6 +
+ x * (1/24 + x * (1/120 + x * (1/720))))));
+ } else { // Use regular exp if not close enough to 0.
+ return MathExp(x) - 1;
+ }
}
-//ES6 draft 09-27-13, section 20.2.2.20.
+// ES6 draft 09-27-13, section 20.2.2.20.
+// Use Taylor series to approximate. With y = x + 1;
+// log(y) at 1 == log(1) + log'(1)(y-1)/1! + log''(1)(y-1)^2/2! + ...
+// == 0 + x - x^2/2 + x^3/3 ...
+// The closer x is to 0, the fewer terms are required.
function MathLog1p(x) {
- return %Math_log1p(TO_NUMBER_INLINE(x));
+ if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
+ var xabs = MathAbs(x);
+ if (xabs < 1E-7) {
+ return x * (1 - x * (1/2));
+ } else if (xabs < 3E-5) {
+ return x * (1 - x * (1/2 - x * (1/3)));
+ } else if (xabs < 7E-3) {
+ return x * (1 - x * (1/2 - x * (1/3 - x * (1/4 -
+ x * (1/5 - x * (1/6 - x * (1/7)))))));
+ } else { // Use regular log if not close enough to 0.
+ return MathLog(1 + x);
+ }
}
« no previous file with comments | « src/harmony-array.js ('k') | src/heap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698