Chromium Code Reviews| Index: src/math.js |
| diff --git a/src/math.js b/src/math.js |
| index e1798fa599ac7594d28fbfe96516607a27e26f77..d6e8c0e786876b0517c8a5bcb2dd23b523f525c7 100644 |
| --- a/src/math.js |
| +++ b/src/math.js |
| @@ -217,16 +217,17 @@ var InitTrigonometricFunctions; |
| // Also define the initialization function that populates the lookup table |
| // and then wires up the function definitions. |
| function SetupTrigonometricFunctions() { |
| - // TODO(yangguo): The following table size has been chosen to satisfy |
| - // Sunspider's brittle result verification. Reconsider relevance. |
| - var samples = 4489; |
| - var pi = 3.1415926535897932; |
| - var pi_half = pi / 2; |
| - var inverse_pi_half = 2 / pi; |
| - var two_pi = 2 * pi; |
| - var four_pi = 4 * pi; |
| - var interval = pi_half / samples; |
| - var inverse_interval = samples / pi_half; |
| + var samples = 1800; // Table size. Do not change arbitrarily. |
| + // 2 / pi |
| + var inverse_pi_half = %HeapNumberFromHex("83c8c96d305fe43f"); |
|
Sven Panne
2013/11/20 07:38:35
Why do we need a new %Foo? Our scanner should be a
|
| + // samples / (pi / 2) |
| + var index_convert = %HeapNumberFromHex("3b597e90a9e79140"); |
| + // pi / 2 rounded up |
| + var pi_half = %HeapNumberFromHex("192d4454fb21f93f"); |
| + // We use two parts for pi/2 to emulate a higher precision. |
| + // Note that pi_half > pi_half_1 + pi_half_2 |
| + var pi_half_1 = %HeapNumberFromHex("00000054fb21f93f"); |
| + var pi_half_2 = %HeapNumberFromHex("3326a611460b113e"); |
| var table_sin; |
| var table_cos_interval; |
| @@ -241,8 +242,9 @@ function SetupTrigonometricFunctions() { |
| // 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 Interpolation = function(x, phase) { |
| + var double_index = x * index_convert; |
| + if (phase & 1) double_index = samples - double_index; |
| var index = double_index | 0; |
| var t1 = double_index - index; |
| var t2 = 1 - t1; |
| @@ -251,26 +253,32 @@ function SetupTrigonometricFunctions() { |
| var dy = y2 - y1; |
| return (t2 * y1 + t1 * y2 + |
| t1 * t2 * ((table_cos_interval[index] - dy) * t2 + |
| - (dy - table_cos_interval[index + 1]) * t1)); |
| + (dy - table_cos_interval[index + 1]) * t1)) |
| + * (1 - (phase & 2)) + 0; |
| } |
| var MathSinInterpolation = function(x) { |
| - // This is to make Sunspider's result verification happy. |
| - if (x > four_pi) x -= four_pi; |
| - 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; |
| + var x_over_pi_half = x * inverse_pi_half; |
| + if (%_IsMinusZero(x_over_pi_half)) return x_over_pi_half; |
| + var phase = 0; |
| + while (x < 0 || x > pi_half) { |
| + var multiple = MathFloor(x * inverse_pi_half); |
| + x = x - multiple * pi_half_1 - multiple * pi_half_2; |
| + phase += multiple; |
| + } |
| + return Interpolation(x, multiple); |
| } |
| // 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; |
| + x = MathAbs(x); |
| + var phase = 0; |
| + while (x < 0 || x > pi_half) { |
| + var multiple = MathFloor(x * inverse_pi_half); |
| + x = x - multiple * pi_half_1 - multiple * pi_half_2; |
| + phase += multiple; |
| + } |
| + return Interpolation(x, phase + 1); |
| }; |
| %SetInlineBuiltinFlag(Interpolation); |