| Index: src/math.js
|
| diff --git a/src/math.js b/src/math.js
|
| index efab63a186d4f8b924894dad9c1f0be59ef9846a..385b3c2a221832bbe1d15cc268ed7ae2b6ac74e0 100644
|
| --- a/src/math.js
|
| +++ b/src/math.js
|
| @@ -79,7 +79,7 @@ function MathCeil(x) {
|
|
|
| // ECMA 262 - 15.8.2.7
|
| function MathCos(x) {
|
| - return %_MathCos(TO_NUMBER_INLINE(x));
|
| + return MathCosImpl(x);
|
| }
|
|
|
| // ECMA 262 - 15.8.2.8
|
| @@ -117,9 +117,8 @@ function MathMax(arg1, arg2) { // length == 2
|
| if (arg2 > arg1) return arg2;
|
| if (arg1 > arg2) return arg1;
|
| if (arg1 == arg2) {
|
| - // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be
|
| - // a Smi or a heap number.
|
| - return (arg1 == 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg2 : arg1;
|
| + // Make sure -0 is considered less than +0.
|
| + return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg2 : arg1;
|
| }
|
| // All comparisons failed, one of the arguments must be NaN.
|
| return NAN;
|
| @@ -128,10 +127,8 @@ function MathMax(arg1, arg2) { // length == 2
|
| for (var i = 0; i < length; i++) {
|
| var n = %_Arguments(i);
|
| if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
|
| - // Make sure +0 is considered greater than -0. -0 is never a Smi, +0 can be
|
| - // a Smi or heap number.
|
| - if (NUMBER_IS_NAN(n) || n > r ||
|
| - (r == 0 && n == 0 && !%_IsSmi(r) && 1 / r < 0)) {
|
| + // Make sure +0 is considered greater than -0.
|
| + if (NUMBER_IS_NAN(n) || n > r || (r === 0 && n === 0 && %_IsMinusZero(r))) {
|
| r = n;
|
| }
|
| }
|
| @@ -147,9 +144,8 @@ function MathMin(arg1, arg2) { // length == 2
|
| if (arg2 > arg1) return arg1;
|
| if (arg1 > arg2) return arg2;
|
| if (arg1 == arg2) {
|
| - // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be
|
| - // a Smi or a heap number.
|
| - return (arg1 == 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg1 : arg2;
|
| + // Make sure -0 is considered less than +0.
|
| + return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg1 : arg2;
|
| }
|
| // All comparisons failed, one of the arguments must be NaN.
|
| return NAN;
|
| @@ -158,10 +154,8 @@ function MathMin(arg1, arg2) { // length == 2
|
| for (var i = 0; i < length; i++) {
|
| var n = %_Arguments(i);
|
| if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
|
| - // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be a
|
| - // Smi or a heap number.
|
| - if (NUMBER_IS_NAN(n) || n < r ||
|
| - (r == 0 && n == 0 && !%_IsSmi(n) && 1 / n < 0)) {
|
| + // Make sure -0 is considered less than +0.
|
| + if (NUMBER_IS_NAN(n) || n < r || (r === 0 && n === 0 && %_IsMinusZero(n))) {
|
| r = n;
|
| }
|
| }
|
| @@ -185,7 +179,7 @@ function MathRound(x) {
|
|
|
| // ECMA 262 - 15.8.2.16
|
| function MathSin(x) {
|
| - return %_MathSin(TO_NUMBER_INLINE(x));
|
| + return MathSinImpl(x);
|
| }
|
|
|
| // ECMA 262 - 15.8.2.17
|
| @@ -195,7 +189,7 @@ function MathSqrt(x) {
|
|
|
| // ECMA 262 - 15.8.2.18
|
| function MathTan(x) {
|
| - return %_MathTan(TO_NUMBER_INLINE(x));
|
| + return MathSinImpl(x) / MathCosImpl(x);
|
| }
|
|
|
| // Non-standard extension.
|
| @@ -204,6 +198,92 @@ function MathImul(x, y) {
|
| }
|
|
|
|
|
| +var MathSinImpl = function(x) {
|
| + InitTrigonometricFunctions();
|
| + return MathSinImpl(x);
|
| +}
|
| +
|
| +
|
| +var MathCosImpl = function(x) {
|
| + InitTrigonometricFunctions();
|
| + return MathCosImpl(x);
|
| +}
|
| +
|
| +
|
| +var InitTrigonometricFunctions;
|
| +
|
| +
|
| +// Define constants and interpolation functions.
|
| +// Also define the initialization function that populates the lookup table
|
| +// and then wires up the function definitions.
|
| +function SetupTrigonometricFunctions() {
|
| + var samples = 1800; // Table size.
|
| + var pi = 3.1415926535897932;
|
| + var pi_half = pi / 2;
|
| + var inverse_pi_half = 1 / pi_half;
|
| + var two_pi = pi * 2;
|
| + var interval = pi_half / samples;
|
| + var inverse_interval = samples / pi_half;
|
| + var table_sin;
|
| + var table_cos_interval;
|
| +
|
| + // This implements sine using the following algorithm.
|
| + // 1) Multiplication takes care of to-number conversion.
|
| + // 2) Reduce x to the first quadrant [0, pi/2].
|
| + // Conveniently enough, in case of +/-Infinity, we get NaN.
|
| + // 3) Replace x by (pi/2-x) if x was in the 2nd or 4th quadrant.
|
| + // 4) Do a table lookup for the closest samples to the left and right of x.
|
| + // 5) Find the derivatives at those sampling points by table lookup:
|
| + // dsin(x)/dx = cos(x) = sin(pi/2-x) for x in [0, pi/2].
|
| + // 6) Use cubic spline interpolation to approximate sin(x).
|
| + // 7) Negate the result if x was in the 3rd or 4th quadrant.
|
| + // 8) Get rid of -0 by adding 0.
|
| + var Interpolation = function(x) {
|
| + var double_index = x * inverse_interval;
|
| + var index = double_index | 0;
|
| + var t1 = double_index - index;
|
| + var t2 = 1 - t1;
|
| + var y1 = table_sin[index];
|
| + var y2 = table_sin[index + 1];
|
| + var dy = y2 - y1;
|
| + return (t2 * y1 + t1 * y2 +
|
| + t1 * t2 * ((table_cos_interval[index] - dy) * t2 +
|
| + (dy - table_cos_interval[index + 1]) * t1));
|
| + }
|
| +
|
| + var MathSinInterpolation = function(x) {
|
| + var multiple = MathFloor(x * inverse_pi_half);
|
| + if (%_IsMinusZero(multiple)) return multiple;
|
| + x = (multiple & 1) * pi_half +
|
| + (1 - ((multiple & 1) << 1)) * (x - multiple * pi_half);
|
| + return Interpolation(x) * (1 - (multiple & 2)) + 0;
|
| + }
|
| +
|
| + // Cosine is sine with a phase offset of pi/2.
|
| + var MathCosInterpolation = function(x) {
|
| + var multiple = MathFloor(x * inverse_pi_half);
|
| + var phase = multiple + 1;
|
| + x = (phase & 1) * pi_half +
|
| + (1 - ((phase & 1) << 1)) * (x - multiple * pi_half);
|
| + return Interpolation(x) * (1 - (phase & 2)) + 0;
|
| + };
|
| +
|
| + %SetInlineBuiltinFlag(Interpolation);
|
| + %SetInlineBuiltinFlag(MathSinInterpolation);
|
| + %SetInlineBuiltinFlag(MathCosInterpolation);
|
| +
|
| + InitTrigonometricFunctions = function() {
|
| + table_sin = new global.Float64Array(samples + 2);
|
| + table_cos_interval = new global.Float64Array(samples + 2);
|
| + %PopulateTrigonometricTable(table_sin, table_cos_interval, samples);
|
| + MathSinImpl = MathSinInterpolation;
|
| + MathCosImpl = MathCosInterpolation;
|
| + }
|
| +}
|
| +
|
| +SetupTrigonometricFunctions();
|
| +
|
| +
|
| // -------------------------------------------------------------------
|
|
|
| function SetUpMath() {
|
| @@ -276,6 +356,10 @@ function SetUpMath() {
|
| "min", MathMin,
|
| "imul", MathImul
|
| ));
|
| +
|
| + %SetInlineBuiltinFlag(MathSin);
|
| + %SetInlineBuiltinFlag(MathCos);
|
| + %SetInlineBuiltinFlag(MathTan);
|
| }
|
|
|
| SetUpMath();
|
|
|