| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 52 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 53 // Idempotent for NaN, +/-0 and +/-Infinity. | 53 // Idempotent for NaN, +/-0 and +/-Infinity. |
| 54 if (x === 0 || !NUMBER_IS_FINITE(x)) return x; | 54 if (x === 0 || !NUMBER_IS_FINITE(x)) return x; |
| 55 return (MathExp(x) - MathExp(-x)) / 2; | 55 return (MathExp(x) - MathExp(-x)) / 2; |
| 56 } | 56 } |
| 57 | 57 |
| 58 | 58 |
| 59 // ES6 draft 09-27-13, section 20.2.2.12. | 59 // ES6 draft 09-27-13, section 20.2.2.12. |
| 60 function MathCosh(x) { | 60 function MathCosh(x) { |
| 61 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 61 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 62 // Idempotent for NaN and +/-Infinity. | 62 if (!NUMBER_IS_FINITE(x)) return MathAbs(x); |
| 63 if (!NUMBER_IS_FINITE(x)) return x; | |
| 64 return (MathExp(x) + MathExp(-x)) / 2; | 63 return (MathExp(x) + MathExp(-x)) / 2; |
| 65 } | 64 } |
| 66 | 65 |
| 67 | 66 |
| 68 // ES6 draft 09-27-13, section 20.2.2.33. | 67 // ES6 draft 09-27-13, section 20.2.2.33. |
| 69 function MathTanh(x) { | 68 function MathTanh(x) { |
| 70 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 69 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 71 // Idempotent for +/-0. | 70 // Idempotent for +/-0. |
| 72 if (x === 0) return x; | 71 if (x === 0) return x; |
| 73 // Returns +/-1 for +/-Infinity. | 72 // Returns +/-1 for +/-Infinity. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 103 function MathAtanh(x) { | 102 function MathAtanh(x) { |
| 104 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 103 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 105 // Idempotent for +/-0. | 104 // Idempotent for +/-0. |
| 106 if (x === 0) return x; | 105 if (x === 0) return x; |
| 107 // Returns NaN for NaN and +/- Infinity. | 106 // Returns NaN for NaN and +/- Infinity. |
| 108 if (!NUMBER_IS_FINITE(x)) return NAN; | 107 if (!NUMBER_IS_FINITE(x)) return NAN; |
| 109 return 0.5 * MathLog((1 + x) / (1 - x)); | 108 return 0.5 * MathLog((1 + x) / (1 - x)); |
| 110 } | 109 } |
| 111 | 110 |
| 112 | 111 |
| 113 //ES6 draft 09-27-13, section 20.2.2.21. | 112 // ES6 draft 09-27-13, section 20.2.2.21. |
| 114 function MathLog10(x) { | 113 function MathLog10(x) { |
| 115 return MathLog(x) * 0.434294481903251828; // log10(x) = log(x)/log(10). | 114 return MathLog(x) * 0.434294481903251828; // log10(x) = log(x)/log(10). |
| 116 } | 115 } |
| 117 | 116 |
| 118 | 117 |
| 119 //ES6 draft 09-27-13, section 20.2.2.22. | 118 // ES6 draft 09-27-13, section 20.2.2.22. |
| 120 function MathLog2(x) { | 119 function MathLog2(x) { |
| 121 return MathLog(x) * 1.442695040888963407; // log2(x) = log(x)/log(2). | 120 return MathLog(x) * 1.442695040888963407; // log2(x) = log(x)/log(2). |
| 122 } | 121 } |
| 123 | 122 |
| 124 | 123 |
| 125 //ES6 draft 09-27-13, section 20.2.2.17. | 124 // ES6 draft 09-27-13, section 20.2.2.17. |
| 126 function MathHypot(x, y) { // Function length is 2. | 125 function MathHypot(x, y) { // Function length is 2. |
| 127 // We may want to introduce fast paths for two arguments and when | 126 // We may want to introduce fast paths for two arguments and when |
| 128 // normalization to avoid overflow is not necessary. For now, we | 127 // normalization to avoid overflow is not necessary. For now, we |
| 129 // simply assume the general case. | 128 // simply assume the general case. |
| 130 var length = %_ArgumentsLength(); | 129 var length = %_ArgumentsLength(); |
| 131 var args = new InternalArray(length); | 130 var args = new InternalArray(length); |
| 132 var max = 0; | 131 var max = 0; |
| 133 for (var i = 0; i < length; i++) { | 132 for (var i = 0; i < length; i++) { |
| 134 var n = %_Arguments(i); | 133 var n = %_Arguments(i); |
| 135 if (!IS_NUMBER(n)) n = NonNumberToNumber(n); | 134 if (!IS_NUMBER(n)) n = NonNumberToNumber(n); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 148 var n = args[i] / max; | 147 var n = args[i] / max; |
| 149 var summand = n * n - compensation; | 148 var summand = n * n - compensation; |
| 150 var preliminary = sum + summand; | 149 var preliminary = sum + summand; |
| 151 compensation = (preliminary - sum) - summand; | 150 compensation = (preliminary - sum) - summand; |
| 152 sum = preliminary; | 151 sum = preliminary; |
| 153 } | 152 } |
| 154 return MathSqrt(sum) * max; | 153 return MathSqrt(sum) * max; |
| 155 } | 154 } |
| 156 | 155 |
| 157 | 156 |
| 157 // ES6 draft 09-27-13, section 20.2.2.16. |
| 158 function MathFround(x) { |
| 159 return %Math_fround(TO_NUMBER_INLINE(x)); |
| 160 } |
| 161 |
| 162 |
| 163 function MathClz32(x) { |
| 164 x = ToUint32(TO_NUMBER_INLINE(x)); |
| 165 if (x == 0) return 32; |
| 166 var result = 0; |
| 167 // Binary search. |
| 168 if ((x & 0xFFFF0000) === 0) { x <<= 16; result += 16; }; |
| 169 if ((x & 0xFF000000) === 0) { x <<= 8; result += 8; }; |
| 170 if ((x & 0xF0000000) === 0) { x <<= 4; result += 4; }; |
| 171 if ((x & 0xC0000000) === 0) { x <<= 2; result += 2; }; |
| 172 if ((x & 0x80000000) === 0) { x <<= 1; result += 1; }; |
| 173 return result; |
| 174 } |
| 175 |
| 176 |
| 177 //ES6 draft 09-27-13, section 20.2.2.9. |
| 178 function MathCbrt(x) { |
| 179 return %Math_cbrt(TO_NUMBER_INLINE(x)); |
| 180 } |
| 181 |
| 182 |
| 183 //ES6 draft 09-27-13, section 20.2.2.14. |
| 184 function MathExpm1(x) { |
| 185 return %Math_expm1(TO_NUMBER_INLINE(x)); |
| 186 } |
| 187 |
| 188 |
| 189 //ES6 draft 09-27-13, section 20.2.2.20. |
| 190 function MathLog1p(x) { |
| 191 return %Math_log1p(TO_NUMBER_INLINE(x)); |
| 192 } |
| 193 |
| 194 |
| 158 function ExtendMath() { | 195 function ExtendMath() { |
| 159 %CheckIsBootstrapping(); | 196 %CheckIsBootstrapping(); |
| 160 | 197 |
| 161 // Set up the non-enumerable functions on the Math object. | 198 // Set up the non-enumerable functions on the Math object. |
| 162 InstallFunctions($Math, DONT_ENUM, $Array( | 199 InstallFunctions($Math, DONT_ENUM, $Array( |
| 163 "sign", MathSign, | 200 "sign", MathSign, |
| 164 "trunc", MathTrunc, | 201 "trunc", MathTrunc, |
| 165 "sinh", MathSinh, | 202 "sinh", MathSinh, |
| 166 "cosh", MathCosh, | 203 "cosh", MathCosh, |
| 167 "tanh", MathTanh, | 204 "tanh", MathTanh, |
| 168 "asinh", MathAsinh, | 205 "asinh", MathAsinh, |
| 169 "acosh", MathAcosh, | 206 "acosh", MathAcosh, |
| 170 "atanh", MathAtanh, | 207 "atanh", MathAtanh, |
| 171 "log10", MathLog10, | 208 "log10", MathLog10, |
| 172 "log2", MathLog2, | 209 "log2", MathLog2, |
| 173 "hypot", MathHypot | 210 "hypot", MathHypot, |
| 211 "fround", MathFround, |
| 212 "clz32", MathClz32, |
| 213 "cbrt", MathCbrt, |
| 214 "log1p", MathLog1p, |
| 215 "expm1", MathExpm1 |
| 174 )); | 216 )); |
| 175 } | 217 } |
| 176 | 218 |
| 219 |
| 177 ExtendMath(); | 220 ExtendMath(); |
| OLD | NEW |