| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 (function(global, utils) { | 5 (function(global, utils) { |
| 6 "use strict"; | 6 "use strict"; |
| 7 | 7 |
| 8 %CheckIsBootstrapping(); | 8 %CheckIsBootstrapping(); |
| 9 | 9 |
| 10 // ------------------------------------------------------------------- | 10 // ------------------------------------------------------------------- |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 nextRandomIndex = randomNumbers.length; | 84 nextRandomIndex = randomNumbers.length; |
| 85 } | 85 } |
| 86 return %_DoubleLo(randomNumbers[--nextRandomIndex]) & 0x3FFFFFFF; | 86 return %_DoubleLo(randomNumbers[--nextRandomIndex]) & 0x3FFFFFFF; |
| 87 } | 87 } |
| 88 | 88 |
| 89 // ECMA 262 - 15.8.2.15 | 89 // ECMA 262 - 15.8.2.15 |
| 90 function MathRound(x) { | 90 function MathRound(x) { |
| 91 return %RoundNumber(TO_NUMBER(x)); | 91 return %RoundNumber(TO_NUMBER(x)); |
| 92 } | 92 } |
| 93 | 93 |
| 94 // ECMA 262 - 15.8.2.17 | |
| 95 function MathSqrtJS(x) { | |
| 96 return %_MathSqrt(+x); | |
| 97 } | |
| 98 | |
| 99 // ES6 draft 09-27-13, section 20.2.2.28. | 94 // ES6 draft 09-27-13, section 20.2.2.28. |
| 100 function MathSign(x) { | 95 function MathSign(x) { |
| 101 x = +x; | 96 x = +x; |
| 102 if (x > 0) return 1; | 97 if (x > 0) return 1; |
| 103 if (x < 0) return -1; | 98 if (x < 0) return -1; |
| 104 // -0, 0 or NaN. | 99 // -0, 0 or NaN. |
| 105 return x; | 100 return x; |
| 106 } | 101 } |
| 107 | 102 |
| 108 // ES6 draft 09-27-13, section 20.2.2.34. | 103 // ES6 draft 09-27-13, section 20.2.2.34. |
| 109 function MathTrunc(x) { | 104 function MathTrunc(x) { |
| 110 x = +x; | 105 x = +x; |
| 111 if (x > 0) return %_MathFloor(x); | 106 if (x > 0) return %_MathFloor(x); |
| 112 if (x < 0) return -%_MathFloor(-x); | 107 if (x < 0) return -%_MathFloor(-x); |
| 113 // -0, 0 or NaN. | 108 // -0, 0 or NaN. |
| 114 return x; | 109 return x; |
| 115 } | 110 } |
| 116 | 111 |
| 117 // ES6 draft 09-27-13, section 20.2.2.5. | 112 // ES6 draft 09-27-13, section 20.2.2.5. |
| 118 function MathAsinh(x) { | 113 function MathAsinh(x) { |
| 119 x = TO_NUMBER(x); | 114 x = TO_NUMBER(x); |
| 120 // Idempotent for NaN, +/-0 and +/-Infinity. | 115 // Idempotent for NaN, +/-0 and +/-Infinity. |
| 121 if (x === 0 || !NUMBER_IS_FINITE(x)) return x; | 116 if (x === 0 || !NUMBER_IS_FINITE(x)) return x; |
| 122 if (x > 0) return MathLog(x + %_MathSqrt(x * x + 1)); | 117 if (x > 0) return MathLog(x + %math_sqrt(x * x + 1)); |
| 123 // This is to prevent numerical errors caused by large negative x. | 118 // This is to prevent numerical errors caused by large negative x. |
| 124 return -MathLog(-x + %_MathSqrt(x * x + 1)); | 119 return -MathLog(-x + %math_sqrt(x * x + 1)); |
| 125 } | 120 } |
| 126 | 121 |
| 127 // ES6 draft 09-27-13, section 20.2.2.3. | 122 // ES6 draft 09-27-13, section 20.2.2.3. |
| 128 function MathAcosh(x) { | 123 function MathAcosh(x) { |
| 129 x = TO_NUMBER(x); | 124 x = TO_NUMBER(x); |
| 130 if (x < 1) return NaN; | 125 if (x < 1) return NaN; |
| 131 // Idempotent for NaN and +Infinity. | 126 // Idempotent for NaN and +Infinity. |
| 132 if (!NUMBER_IS_FINITE(x)) return x; | 127 if (!NUMBER_IS_FINITE(x)) return x; |
| 133 return MathLog(x + %_MathSqrt(x + 1) * %_MathSqrt(x - 1)); | 128 return MathLog(x + %math_sqrt(x + 1) * %math_sqrt(x - 1)); |
| 134 } | 129 } |
| 135 | 130 |
| 136 // ES6 draft 09-27-13, section 20.2.2.7. | 131 // ES6 draft 09-27-13, section 20.2.2.7. |
| 137 function MathAtanh(x) { | 132 function MathAtanh(x) { |
| 138 x = TO_NUMBER(x); | 133 x = TO_NUMBER(x); |
| 139 // Idempotent for +/-0. | 134 // Idempotent for +/-0. |
| 140 if (x === 0) return x; | 135 if (x === 0) return x; |
| 141 // Returns NaN for NaN and +/- Infinity. | 136 // Returns NaN for NaN and +/- Infinity. |
| 142 if (!NUMBER_IS_FINITE(x)) return NaN; | 137 if (!NUMBER_IS_FINITE(x)) return NaN; |
| 143 return 0.5 * MathLog((1 + x) / (1 - x)); | 138 return 0.5 * MathLog((1 + x) / (1 - x)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 162 if (max === 0) max = 1; | 157 if (max === 0) max = 1; |
| 163 var sum = 0; | 158 var sum = 0; |
| 164 var compensation = 0; | 159 var compensation = 0; |
| 165 for (var i = 0; i < length; i++) { | 160 for (var i = 0; i < length; i++) { |
| 166 var n = arguments[i] / max; | 161 var n = arguments[i] / max; |
| 167 var summand = n * n - compensation; | 162 var summand = n * n - compensation; |
| 168 var preliminary = sum + summand; | 163 var preliminary = sum + summand; |
| 169 compensation = (preliminary - sum) - summand; | 164 compensation = (preliminary - sum) - summand; |
| 170 sum = preliminary; | 165 sum = preliminary; |
| 171 } | 166 } |
| 172 return %_MathSqrt(sum) * max; | 167 return %math_sqrt(sum) * max; |
| 173 } | 168 } |
| 174 | 169 |
| 175 // ES6 draft 07-18-14, section 20.2.2.11 | 170 // ES6 draft 07-18-14, section 20.2.2.11 |
| 176 function MathClz32JS(x) { | 171 function MathClz32JS(x) { |
| 177 return %_MathClz32(x >>> 0); | 172 return %_MathClz32(x >>> 0); |
| 178 } | 173 } |
| 179 | 174 |
| 180 // ES6 draft 09-27-13, section 20.2.2.9. | 175 // ES6 draft 09-27-13, section 20.2.2.9. |
| 181 // Cube root approximation, refer to: http://metamerist.com/cbrt/cbrt.htm | 176 // Cube root approximation, refer to: http://metamerist.com/cbrt/cbrt.htm |
| 182 // Using initial approximation adapted from Kahan's cbrt and 4 iterations | 177 // Using initial approximation adapted from Kahan's cbrt and 4 iterations |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 // Set up non-enumerable functions of the Math object and | 222 // Set up non-enumerable functions of the Math object and |
| 228 // set their names. | 223 // set their names. |
| 229 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ | 224 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ |
| 230 "random", MathRandom, | 225 "random", MathRandom, |
| 231 "abs", MathAbs, | 226 "abs", MathAbs, |
| 232 "ceil", MathCeil, | 227 "ceil", MathCeil, |
| 233 "exp", MathExp, | 228 "exp", MathExp, |
| 234 "floor", MathFloorJS, | 229 "floor", MathFloorJS, |
| 235 "log", MathLog, | 230 "log", MathLog, |
| 236 "round", MathRound, | 231 "round", MathRound, |
| 237 "sqrt", MathSqrtJS, | |
| 238 "atan2", MathAtan2JS, | 232 "atan2", MathAtan2JS, |
| 239 "pow", MathPowJS, | 233 "pow", MathPowJS, |
| 240 "sign", MathSign, | 234 "sign", MathSign, |
| 241 "trunc", MathTrunc, | 235 "trunc", MathTrunc, |
| 242 "asinh", MathAsinh, | 236 "asinh", MathAsinh, |
| 243 "acosh", MathAcosh, | 237 "acosh", MathAcosh, |
| 244 "atanh", MathAtanh, | 238 "atanh", MathAtanh, |
| 245 "hypot", MathHypot, | 239 "hypot", MathHypot, |
| 246 "clz32", MathClz32JS, | 240 "clz32", MathClz32JS, |
| 247 "cbrt", MathCbrt | 241 "cbrt", MathCbrt |
| 248 ]); | 242 ]); |
| 249 | 243 |
| 250 %SetForceInlineFlag(MathAbs); | 244 %SetForceInlineFlag(MathAbs); |
| 251 %SetForceInlineFlag(MathAtan2JS); | 245 %SetForceInlineFlag(MathAtan2JS); |
| 252 %SetForceInlineFlag(MathCeil); | 246 %SetForceInlineFlag(MathCeil); |
| 253 %SetForceInlineFlag(MathClz32JS); | 247 %SetForceInlineFlag(MathClz32JS); |
| 254 %SetForceInlineFlag(MathFloorJS); | 248 %SetForceInlineFlag(MathFloorJS); |
| 255 %SetForceInlineFlag(MathRandom); | 249 %SetForceInlineFlag(MathRandom); |
| 256 %SetForceInlineFlag(MathSign); | 250 %SetForceInlineFlag(MathSign); |
| 257 %SetForceInlineFlag(MathSqrtJS); | |
| 258 %SetForceInlineFlag(MathTrunc); | 251 %SetForceInlineFlag(MathTrunc); |
| 259 | 252 |
| 260 // ------------------------------------------------------------------- | 253 // ------------------------------------------------------------------- |
| 261 // Exports | 254 // Exports |
| 262 | 255 |
| 263 utils.Export(function(to) { | 256 utils.Export(function(to) { |
| 264 to.MathAbs = MathAbs; | 257 to.MathAbs = MathAbs; |
| 265 to.MathExp = MathExp; | 258 to.MathExp = MathExp; |
| 266 to.MathFloor = MathFloorJS; | 259 to.MathFloor = MathFloorJS; |
| 267 to.IntRandom = MathRandomRaw; | 260 to.IntRandom = MathRandomRaw; |
| 268 }); | 261 }); |
| 269 | 262 |
| 270 }) | 263 }) |
| OLD | NEW |