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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 function MathAtan2(y, x) { | 71 function MathAtan2(y, x) { |
| 72 return %Math_atan2(TO_NUMBER_INLINE(y), TO_NUMBER_INLINE(x)); | 72 return %Math_atan2(TO_NUMBER_INLINE(y), TO_NUMBER_INLINE(x)); |
| 73 } | 73 } |
| 74 | 74 |
| 75 // ECMA 262 - 15.8.2.6 | 75 // ECMA 262 - 15.8.2.6 |
| 76 function MathCeil(x) { | 76 function MathCeil(x) { |
| 77 return %Math_ceil(TO_NUMBER_INLINE(x)); | 77 return %Math_ceil(TO_NUMBER_INLINE(x)); |
| 78 } | 78 } |
| 79 | 79 |
| 80 // ECMA 262 - 15.8.2.7 | 80 // ECMA 262 - 15.8.2.7 |
| 81 function MathCos(x) { | 81 var MathCos; |
| 82 return MathCosImpl(x); | |
| 83 } | |
| 84 | 82 |
| 85 // ECMA 262 - 15.8.2.8 | 83 // ECMA 262 - 15.8.2.8 |
| 86 function MathExp(x) { | 84 function MathExp(x) { |
| 87 return %Math_exp(TO_NUMBER_INLINE(x)); | 85 return %Math_exp(TO_NUMBER_INLINE(x)); |
| 88 } | 86 } |
| 89 | 87 |
| 90 // ECMA 262 - 15.8.2.9 | 88 // ECMA 262 - 15.8.2.9 |
| 91 function MathFloor(x) { | 89 function MathFloor(x) { |
| 92 x = TO_NUMBER_INLINE(x); | 90 x = TO_NUMBER_INLINE(x); |
| 93 // It's more common to call this with a positive number that's out | 91 // It's more common to call this with a positive number that's out |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 171 function MathRandom() { | 169 function MathRandom() { |
| 172 return %_RandomHeapNumber(); | 170 return %_RandomHeapNumber(); |
| 173 } | 171 } |
| 174 | 172 |
| 175 // ECMA 262 - 15.8.2.15 | 173 // ECMA 262 - 15.8.2.15 |
| 176 function MathRound(x) { | 174 function MathRound(x) { |
| 177 return %RoundNumber(TO_NUMBER_INLINE(x)); | 175 return %RoundNumber(TO_NUMBER_INLINE(x)); |
| 178 } | 176 } |
| 179 | 177 |
| 180 // ECMA 262 - 15.8.2.16 | 178 // ECMA 262 - 15.8.2.16 |
| 181 function MathSin(x) { | 179 var MathSin; |
| 182 return MathSinImpl(x); | |
| 183 } | |
| 184 | 180 |
| 185 // ECMA 262 - 15.8.2.17 | 181 // ECMA 262 - 15.8.2.17 |
| 186 function MathSqrt(x) { | 182 function MathSqrt(x) { |
| 187 return %_MathSqrt(TO_NUMBER_INLINE(x)); | 183 return %_MathSqrt(TO_NUMBER_INLINE(x)); |
| 188 } | 184 } |
| 189 | 185 |
| 190 // ECMA 262 - 15.8.2.18 | 186 // ECMA 262 - 15.8.2.18 |
| 191 function MathTan(x) { | 187 function MathTan(x) { |
| 192 return MathSinImpl(x) / MathCosImpl(x); | 188 return MathSin(x) / MathCos(x); |
| 193 } | 189 } |
| 194 | 190 |
| 195 // Non-standard extension. | 191 // Non-standard extension. |
| 196 function MathImul(x, y) { | 192 function MathImul(x, y) { |
| 197 return %NumberImul(TO_NUMBER_INLINE(x), TO_NUMBER_INLINE(y)); | 193 return %NumberImul(TO_NUMBER_INLINE(x), TO_NUMBER_INLINE(y)); |
| 198 } | 194 } |
| 199 | 195 |
| 200 | 196 |
| 201 var MathSinImpl = function(x) { | |
| 202 InitTrigonometricFunctions(); | |
| 203 return MathSinImpl(x); | |
| 204 } | |
| 205 | |
| 206 | |
| 207 var MathCosImpl = function(x) { | |
| 208 InitTrigonometricFunctions(); | |
| 209 return MathCosImpl(x); | |
| 210 } | |
| 211 | |
| 212 | |
| 213 var InitTrigonometricFunctions; | 197 var InitTrigonometricFunctions; |
| 214 | 198 |
| 215 | 199 |
| 216 // Define constants and interpolation functions. | 200 // Define constants and interpolation functions. |
| 217 // Also define the initialization function that populates the lookup table | 201 // Also define the initialization function that populates the lookup table |
| 218 // and then wires up the function definitions. | 202 // and then wires up the function definitions. |
| 219 function SetupTrigonometricFunctions() { | 203 function SetupTrigonometricFunctions() { |
|
Sven Panne
2013/11/21 08:09:27
Hmmm, all this lazy-evaluation-by-hand magic is hi
| |
| 220 var samples = 1800; // Table size. Do not change arbitrarily. | 204 var samples = 1800; // Table size. Do not change arbitrarily. |
| 221 var inverse_pi_half = 0.636619772367581343; // 2 / pi | 205 var inverse_pi_half = 0.636619772367581343; // 2 / pi |
| 222 var inverse_pi_half_s_26 = 9.48637384723993156e-9; // 2 / pi / (2^26) | 206 var inverse_pi_half_s_26 = 9.48637384723993156e-9; // 2 / pi / (2^26) |
| 223 var s_26 = 1 << 26; | 207 var s_26 = 1 << 26; |
| 224 var two_step_threshold = 1 << 27; | 208 var two_step_threshold = 1 << 27; |
| 225 var index_convert = 1145.915590261646418; // samples / (pi / 2) | 209 var index_convert = 1145.915590261646418; // samples / (pi / 2) |
| 226 // pi / 2 rounded up | 210 // pi / 2 rounded up |
| 227 var pi_half = 1.570796326794896780; // 0x192d4454fb21f93f | 211 var pi_half = 1.570796326794896780; // 0x192d4454fb21f93f |
| 228 // We use two parts for pi/2 to emulate a higher precision. | 212 // We use two parts for pi/2 to emulate a higher precision. |
| 229 // pi_half_1 only has 26 significant bits for mantissa. | 213 // pi_half_1 only has 26 significant bits for mantissa. |
| 230 // Note that pi_half > pi_half_1 + pi_half_2 | 214 // Note that pi_half > pi_half_1 + pi_half_2 |
| 231 var pi_half_1 = 1.570796325802803040; // 0x00000054fb21f93f | 215 var pi_half_1 = 1.570796325802803040; // 0x00000054fb21f93f |
| 232 var pi_half_2 = 9.920935796805404252e-10; // 0x3326a611460b113e | 216 var pi_half_2 = 9.920935796805404252e-10; // 0x3326a611460b113e |
| 233 var table_sin; | 217 |
| 234 var table_cos_interval; | 218 var table_sin = new InternalDoubleArray(samples + 2); |
| 219 var table_cos_interval = new InternalDoubleArray(samples + 2); | |
| 220 %PopulateTrigonometricTable(table_sin, table_cos_interval, samples); | |
| 221 | |
| 235 | 222 |
| 236 // This implements sine using the following algorithm. | 223 // This implements sine using the following algorithm. |
| 237 // 1) Multiplication takes care of to-number conversion. | 224 // 1) Multiplication takes care of to-number conversion. |
| 238 // 2) Reduce x to the first quadrant [0, pi/2]. | 225 // 2) Reduce x to the first quadrant [0, pi/2]. |
| 239 // Conveniently enough, in case of +/-Infinity, we get NaN. | 226 // Conveniently enough, in case of +/-Infinity, we get NaN. |
| 240 // Note that we try to use only 26 instead of 52 significant bits for | 227 // Note that we try to use only 26 instead of 52 significant bits for |
| 241 // mantissa to avoid rounding errors when multiplying. For very large | 228 // mantissa to avoid rounding errors when multiplying. For very large |
| 242 // input we therefore have additional steps. | 229 // input we therefore have additional steps. |
| 243 // 3) Replace x by (pi/2-x) if x was in the 2nd or 4th quadrant. | 230 // 3) Replace x by (pi/2-x) if x was in the 2nd or 4th quadrant. |
| 244 // 4) Do a table lookup for the closest samples to the left and right of x. | 231 // 4) Do a table lookup for the closest samples to the left and right of x. |
| 245 // 5) Find the derivatives at those sampling points by table lookup: | 232 // 5) Find the derivatives at those sampling points by table lookup: |
| 246 // dsin(x)/dx = cos(x) = sin(pi/2-x) for x in [0, pi/2]. | 233 // dsin(x)/dx = cos(x) = sin(pi/2-x) for x in [0, pi/2]. |
| 247 // 6) Use cubic spline interpolation to approximate sin(x). | 234 // 6) Use cubic spline interpolation to approximate sin(x). |
| 248 // 7) Negate the result if x was in the 3rd or 4th quadrant. | 235 // 7) Negate the result if x was in the 3rd or 4th quadrant. |
| 249 // 8) Get rid of -0 by adding 0. | 236 // 8) Get rid of -0 by adding 0. |
| 250 var Interpolation = function(x, phase) { | 237 var Interpolation = function(x, phase) { |
| 251 if (x < 0 || x > pi_half) { | 238 if (x < 0 || x > pi_half) { |
| 252 var multiple; | 239 var multiple; |
| 253 while (x < -two_step_threshold || x > two_step_threshold) { | 240 while (x < -two_step_threshold || x > two_step_threshold) { |
| 254 // Let's assume this loop does not terminate. | 241 // Let's assume this loop does not terminate. |
|
Sven Panne
2013/11/21 08:09:27
Hmmm, I am still not convinced that the loop will
| |
| 255 // All numbers x in each loop forms a set S. | 242 // All numbers x in each loop forms a set S. |
| 256 // (1) abs(x) > 2^27 for all x in S. | 243 // (1) abs(x) > 2^27 for all x in S. |
| 257 // (2) abs(multiple) != 0 since (2^27 * inverse_pi_half_s26) > 1 | 244 // (2) abs(multiple) != 0 since (2^27 * inverse_pi_half_s26) > 1 |
| 258 // (3) multiple is rounded down in 2^26 steps, so the rounding error is | 245 // (3) multiple is rounded down in 2^26 steps, so the rounding error is |
| 259 // at most max(ulp, 2^26). | 246 // at most max(ulp, 2^26). |
| 260 // (4) so for x > 2^27, we subtract at most (1+pi/4)x and at least | 247 // (4) so for x > 2^27, we subtract at most (1+pi/4)x and at least |
| 261 // (1-pi/4)x | 248 // (1-pi/4)x |
| 262 // (5) The subtraction results in x' so that abs(x') <= abs(x)*pi/4. | 249 // (5) The subtraction results in x' so that abs(x') <= abs(x)*pi/4. |
| 263 // Note that this difference cannot be simply rounded off. | 250 // Note that this difference cannot be simply rounded off. |
| 264 // Set S cannot exist since (5) violates (1). Loop must terminate. | 251 // Set S cannot exist since (5) violates (1). Loop must terminate. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 276 var t2 = 1 - t1; | 263 var t2 = 1 - t1; |
| 277 var y1 = table_sin[index]; | 264 var y1 = table_sin[index]; |
| 278 var y2 = table_sin[index + 1]; | 265 var y2 = table_sin[index + 1]; |
| 279 var dy = y2 - y1; | 266 var dy = y2 - y1; |
| 280 return (t2 * y1 + t1 * y2 + | 267 return (t2 * y1 + t1 * y2 + |
| 281 t1 * t2 * ((table_cos_interval[index] - dy) * t2 + | 268 t1 * t2 * ((table_cos_interval[index] - dy) * t2 + |
| 282 (dy - table_cos_interval[index + 1]) * t1)) | 269 (dy - table_cos_interval[index + 1]) * t1)) |
| 283 * (1 - (phase & 2)) + 0; | 270 * (1 - (phase & 2)) + 0; |
| 284 } | 271 } |
| 285 | 272 |
| 286 var MathSinInterpolation = function(x) { | 273 MathSin = function(x) { |
| 287 x = x * 1; // Convert to number and deal with -0. | 274 x = x * 1; // Convert to number and deal with -0. |
| 288 if (%_IsMinusZero(x)) return x; | 275 if (%_IsMinusZero(x)) return x; |
| 289 return Interpolation(x, 0); | 276 return Interpolation(x, 0); |
| 290 } | 277 } |
| 291 | 278 |
| 292 // Cosine is sine with a phase offset. | 279 // Cosine is sine with a phase offset. |
| 293 var MathCosInterpolation = function(x) { | 280 MathCos = function(x) { |
| 294 x = MathAbs(x); // Convert to number and get rid of -0. | 281 x = MathAbs(x); // Convert to number and get rid of -0. |
| 295 return Interpolation(x, 1); | 282 return Interpolation(x, 1); |
| 296 }; | 283 }; |
| 297 | 284 |
| 298 %SetInlineBuiltinFlag(Interpolation); | 285 %SetInlineBuiltinFlag(Interpolation); |
| 299 %SetInlineBuiltinFlag(MathSinInterpolation); | |
| 300 %SetInlineBuiltinFlag(MathCosInterpolation); | |
| 301 | |
| 302 InitTrigonometricFunctions = function() { | |
| 303 table_sin = new global.Float64Array(samples + 2); | |
| 304 table_cos_interval = new global.Float64Array(samples + 2); | |
| 305 %PopulateTrigonometricTable(table_sin, table_cos_interval, samples); | |
| 306 MathSinImpl = MathSinInterpolation; | |
| 307 MathCosImpl = MathCosInterpolation; | |
| 308 } | |
| 309 } | 286 } |
| 310 | 287 |
| 311 SetupTrigonometricFunctions(); | 288 SetupTrigonometricFunctions(); |
| 312 | 289 |
| 313 | 290 |
| 314 // ------------------------------------------------------------------- | 291 // ------------------------------------------------------------------- |
| 315 | 292 |
| 316 function SetUpMath() { | 293 function SetUpMath() { |
| 317 %CheckIsBootstrapping(); | 294 %CheckIsBootstrapping(); |
| 318 | 295 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 383 "min", MathMin, | 360 "min", MathMin, |
| 384 "imul", MathImul | 361 "imul", MathImul |
| 385 )); | 362 )); |
| 386 | 363 |
| 387 %SetInlineBuiltinFlag(MathSin); | 364 %SetInlineBuiltinFlag(MathSin); |
| 388 %SetInlineBuiltinFlag(MathCos); | 365 %SetInlineBuiltinFlag(MathCos); |
| 389 %SetInlineBuiltinFlag(MathTan); | 366 %SetInlineBuiltinFlag(MathTan); |
| 390 } | 367 } |
| 391 | 368 |
| 392 SetUpMath(); | 369 SetUpMath(); |
| OLD | NEW |