Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 210 } | 210 } |
| 211 | 211 |
| 212 | 212 |
| 213 var InitTrigonometricFunctions; | 213 var InitTrigonometricFunctions; |
| 214 | 214 |
| 215 | 215 |
| 216 // Define constants and interpolation functions. | 216 // Define constants and interpolation functions. |
| 217 // Also define the initialization function that populates the lookup table | 217 // Also define the initialization function that populates the lookup table |
| 218 // and then wires up the function definitions. | 218 // and then wires up the function definitions. |
| 219 function SetupTrigonometricFunctions() { | 219 function SetupTrigonometricFunctions() { |
| 220 var samples = 1800; // Table size. | 220 var samples = 1620; // Table size. Do not change arbitrarily. |
| 221 var pi = 3.1415926535897932; | 221 var inverse_pi_half = 0.63661977236758134; |
| 222 var pi_half = pi / 2; | |
| 223 var inverse_pi_half = 2 / pi; | |
| 224 var two_pi = 2 * pi; | |
| 225 var four_pi = 4 * pi; | |
| 226 var interval = pi_half / samples; | |
| 227 var inverse_interval = samples / pi_half; | |
| 228 var table_sin; | 222 var table_sin; |
| 229 var table_cos_interval; | 223 var table_cos_interval; |
| 230 | 224 |
| 231 // This implements sine using the following algorithm. | 225 // This implements sine using the following algorithm. |
| 232 // 1) Multiplication takes care of to-number conversion. | 226 // 1) Multiplication takes care of to-number conversion. |
| 233 // 2) Reduce x to the first quadrant [0, pi/2]. | 227 // 2) Reduce x to the first quadrant [0, pi/2]. |
| 234 // Conveniently enough, in case of +/-Infinity, we get NaN. | 228 // Conveniently enough, in case of +/-Infinity, we get NaN. |
| 235 // 3) Replace x by (pi/2-x) if x was in the 2nd or 4th quadrant. | 229 // 3) Replace x by (pi/2-x) if x was in the 2nd or 4th quadrant. |
| 236 // 4) Do a table lookup for the closest samples to the left and right of x. | 230 // 4) Do a table lookup for the closest samples to the left and right of x. |
| 237 // 5) Find the derivatives at those sampling points by table lookup: | 231 // 5) Find the derivatives at those sampling points by table lookup: |
| 238 // dsin(x)/dx = cos(x) = sin(pi/2-x) for x in [0, pi/2]. | 232 // dsin(x)/dx = cos(x) = sin(pi/2-x) for x in [0, pi/2]. |
| 239 // 6) Use cubic spline interpolation to approximate sin(x). | 233 // 6) Use cubic spline interpolation to approximate sin(x). |
| 240 // 7) Negate the result if x was in the 3rd or 4th quadrant. | 234 // 7) Negate the result if x was in the 3rd or 4th quadrant. |
| 241 // 8) Get rid of -0 by adding 0. | 235 // 8) Get rid of -0 by adding 0. |
| 242 var Interpolation = function(x) { | 236 var Interpolation = function(x) { |
| 243 var double_index = x * inverse_interval; | 237 var double_index = x * samples; |
| 244 var index = double_index | 0; | 238 var index = double_index | 0; |
| 245 var t1 = double_index - index; | 239 var t1 = double_index - index; |
| 246 var t2 = 1 - t1; | 240 var t2 = 1 - t1; |
| 247 var y1 = table_sin[index]; | 241 var y1 = table_sin[index]; |
| 248 var y2 = table_sin[index + 1]; | 242 var y2 = table_sin[index + 1]; |
| 249 var dy = y2 - y1; | 243 var dy = y2 - y1; |
| 250 return (t2 * y1 + t1 * y2 + | 244 return (t2 * y1 + t1 * y2 + |
| 251 t1 * t2 * ((table_cos_interval[index] - dy) * t2 + | 245 t1 * t2 * ((table_cos_interval[index] - dy) * t2 + |
| 252 (dy - table_cos_interval[index + 1]) * t1)); | 246 (dy - table_cos_interval[index + 1]) * t1)); |
| 253 } | 247 } |
| 254 | 248 |
| 255 var MathSinInterpolation = function(x) { | 249 var MathSinInterpolation = function(x) { |
| 256 // This is to make Sunspider's result verification happy. | 250 x = x * inverse_pi_half; |
| 257 if (x > four_pi) x -= four_pi; | 251 var multiple = MathFloor(x); |
| 258 var multiple = MathFloor(x * inverse_pi_half); | 252 if (%_IsMinusZero(multiple)) return -0; |
|
Jakob Kummerow
2013/11/18 13:53:46
Why this change? "return multiple" might be (ever
| |
| 259 if (%_IsMinusZero(multiple)) return multiple; | 253 x = (multiple & 1) + (1 - ((multiple & 1) << 1)) * (x - multiple); |
| 260 x = (multiple & 1) * pi_half + | |
| 261 (1 - ((multiple & 1) << 1)) * (x - multiple * pi_half); | |
| 262 return Interpolation(x) * (1 - (multiple & 2)) + 0; | 254 return Interpolation(x) * (1 - (multiple & 2)) + 0; |
| 263 } | 255 } |
| 264 | 256 |
| 265 // Cosine is sine with a phase offset of pi/2. | 257 // Cosine is sine with a phase offset of pi/2. |
| 266 var MathCosInterpolation = function(x) { | 258 var MathCosInterpolation = function(x) { |
| 267 var multiple = MathFloor(x * inverse_pi_half); | 259 x = x * inverse_pi_half; |
| 260 var multiple = MathFloor(x); | |
| 268 var phase = multiple + 1; | 261 var phase = multiple + 1; |
| 269 x = (phase & 1) * pi_half + | 262 x = (phase & 1) + (1 - ((phase & 1) << 1)) * (x - multiple); |
| 270 (1 - ((phase & 1) << 1)) * (x - multiple * pi_half); | |
| 271 return Interpolation(x) * (1 - (phase & 2)) + 0; | 263 return Interpolation(x) * (1 - (phase & 2)) + 0; |
| 272 }; | 264 }; |
| 273 | 265 |
| 274 %SetInlineBuiltinFlag(Interpolation); | 266 %SetInlineBuiltinFlag(Interpolation); |
| 275 %SetInlineBuiltinFlag(MathSinInterpolation); | 267 %SetInlineBuiltinFlag(MathSinInterpolation); |
| 276 %SetInlineBuiltinFlag(MathCosInterpolation); | 268 %SetInlineBuiltinFlag(MathCosInterpolation); |
| 277 | 269 |
| 278 InitTrigonometricFunctions = function() { | 270 InitTrigonometricFunctions = function() { |
| 279 table_sin = new global.Float64Array(samples + 2); | 271 table_sin = new global.Float64Array(samples + 2); |
| 280 table_cos_interval = new global.Float64Array(samples + 2); | 272 table_cos_interval = new global.Float64Array(samples + 2); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 359 "min", MathMin, | 351 "min", MathMin, |
| 360 "imul", MathImul | 352 "imul", MathImul |
| 361 )); | 353 )); |
| 362 | 354 |
| 363 %SetInlineBuiltinFlag(MathSin); | 355 %SetInlineBuiltinFlag(MathSin); |
| 364 %SetInlineBuiltinFlag(MathCos); | 356 %SetInlineBuiltinFlag(MathCos); |
| 365 %SetInlineBuiltinFlag(MathTan); | 357 %SetInlineBuiltinFlag(MathTan); |
| 366 } | 358 } |
| 367 | 359 |
| 368 SetUpMath(); | 360 SetUpMath(); |
| OLD | NEW |