| 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 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 // Binary search. | 167 // Binary search. |
| 168 if ((x & 0xFFFF0000) === 0) { x <<= 16; result += 16; }; | 168 if ((x & 0xFFFF0000) === 0) { x <<= 16; result += 16; }; |
| 169 if ((x & 0xFF000000) === 0) { x <<= 8; result += 8; }; | 169 if ((x & 0xFF000000) === 0) { x <<= 8; result += 8; }; |
| 170 if ((x & 0xF0000000) === 0) { x <<= 4; result += 4; }; | 170 if ((x & 0xF0000000) === 0) { x <<= 4; result += 4; }; |
| 171 if ((x & 0xC0000000) === 0) { x <<= 2; result += 2; }; | 171 if ((x & 0xC0000000) === 0) { x <<= 2; result += 2; }; |
| 172 if ((x & 0x80000000) === 0) { x <<= 1; result += 1; }; | 172 if ((x & 0x80000000) === 0) { x <<= 1; result += 1; }; |
| 173 return result; | 173 return result; |
| 174 } | 174 } |
| 175 | 175 |
| 176 | 176 |
| 177 //ES6 draft 09-27-13, section 20.2.2.9. | 177 // ES6 draft 09-27-13, section 20.2.2.9. |
| 178 // Cube root approximation, refer to: http://metamerist.com/cbrt/cbrt.htm |
| 179 // Using initial approximation adapted from Kahan's cbrt and 4 iterations |
| 180 // of Newton's method. |
| 178 function MathCbrt(x) { | 181 function MathCbrt(x) { |
| 179 return %Math_cbrt(TO_NUMBER_INLINE(x)); | 182 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 183 if (x == 0 || !NUMBER_IS_FINITE(x)) return x; |
| 184 return x >= 0 ? CubeRoot(x) : -CubeRoot(-x); |
| 185 } |
| 186 |
| 187 macro NEWTON_ITERATION_CBRT(x, approx) |
| 188 (1.0 / 3.0) * (x / (approx * approx) + 2 * approx); |
| 189 endmacro |
| 190 |
| 191 function CubeRoot(x) { |
| 192 var approx_hi = MathFloor(%_DoubleHi(x) / 3) + 0x2A9F7893; |
| 193 var approx = %_ConstructDouble(approx_hi, 0); |
| 194 approx = NEWTON_ITERATION_CBRT(x, approx); |
| 195 approx = NEWTON_ITERATION_CBRT(x, approx); |
| 196 approx = NEWTON_ITERATION_CBRT(x, approx); |
| 197 return NEWTON_ITERATION_CBRT(x, approx); |
| 180 } | 198 } |
| 181 | 199 |
| 182 | 200 |
| 183 //ES6 draft 09-27-13, section 20.2.2.14. | 201 |
| 202 // ES6 draft 09-27-13, section 20.2.2.14. |
| 203 // Use Taylor series to approximate. |
| 204 // exp(x) - 1 at 0 == -1 + exp(0) + exp'(0)*x/1! + exp''(0)*x^2/2! + ... |
| 205 // == x/1! + x^2/2! + x^3/3! + ... |
| 206 // The closer x is to 0, the fewer terms are required. |
| 184 function MathExpm1(x) { | 207 function MathExpm1(x) { |
| 185 return %Math_expm1(TO_NUMBER_INLINE(x)); | 208 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 209 var xabs = MathAbs(x); |
| 210 if (xabs < 2E-7) { |
| 211 return x * (1 + x * (1/2)); |
| 212 } else if (xabs < 6E-5) { |
| 213 return x * (1 + x * (1/2 + x * (1/6))); |
| 214 } else if (xabs < 2E-2) { |
| 215 return x * (1 + x * (1/2 + x * (1/6 + |
| 216 x * (1/24 + x * (1/120 + x * (1/720)))))); |
| 217 } else { // Use regular exp if not close enough to 0. |
| 218 return MathExp(x) - 1; |
| 219 } |
| 186 } | 220 } |
| 187 | 221 |
| 188 | 222 |
| 189 //ES6 draft 09-27-13, section 20.2.2.20. | 223 // ES6 draft 09-27-13, section 20.2.2.20. |
| 224 // Use Taylor series to approximate. With y = x + 1; |
| 225 // log(y) at 1 == log(1) + log'(1)(y-1)/1! + log''(1)(y-1)^2/2! + ... |
| 226 // == 0 + x - x^2/2 + x^3/3 ... |
| 227 // The closer x is to 0, the fewer terms are required. |
| 190 function MathLog1p(x) { | 228 function MathLog1p(x) { |
| 191 return %Math_log1p(TO_NUMBER_INLINE(x)); | 229 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 230 var xabs = MathAbs(x); |
| 231 if (xabs < 1E-7) { |
| 232 return x * (1 - x * (1/2)); |
| 233 } else if (xabs < 3E-5) { |
| 234 return x * (1 - x * (1/2 - x * (1/3))); |
| 235 } else if (xabs < 7E-3) { |
| 236 return x * (1 - x * (1/2 - x * (1/3 - x * (1/4 - |
| 237 x * (1/5 - x * (1/6 - x * (1/7))))))); |
| 238 } else { // Use regular log if not close enough to 0. |
| 239 return MathLog(1 + x); |
| 240 } |
| 192 } | 241 } |
| 193 | 242 |
| 194 | 243 |
| 195 function ExtendMath() { | 244 function ExtendMath() { |
| 196 %CheckIsBootstrapping(); | 245 %CheckIsBootstrapping(); |
| 197 | 246 |
| 198 // Set up the non-enumerable functions on the Math object. | 247 // Set up the non-enumerable functions on the Math object. |
| 199 InstallFunctions($Math, DONT_ENUM, $Array( | 248 InstallFunctions($Math, DONT_ENUM, $Array( |
| 200 "sign", MathSign, | 249 "sign", MathSign, |
| 201 "trunc", MathTrunc, | 250 "trunc", MathTrunc, |
| 202 "sinh", MathSinh, | 251 "sinh", MathSinh, |
| 203 "cosh", MathCosh, | 252 "cosh", MathCosh, |
| 204 "tanh", MathTanh, | 253 "tanh", MathTanh, |
| 205 "asinh", MathAsinh, | 254 "asinh", MathAsinh, |
| 206 "acosh", MathAcosh, | 255 "acosh", MathAcosh, |
| 207 "atanh", MathAtanh, | 256 "atanh", MathAtanh, |
| 208 "log10", MathLog10, | 257 "log10", MathLog10, |
| 209 "log2", MathLog2, | 258 "log2", MathLog2, |
| 210 "hypot", MathHypot, | 259 "hypot", MathHypot, |
| 211 "fround", MathFround, | 260 "fround", MathFround, |
| 212 "clz32", MathClz32, | 261 "clz32", MathClz32, |
| 213 "cbrt", MathCbrt, | 262 "cbrt", MathCbrt, |
| 214 "log1p", MathLog1p, | 263 "log1p", MathLog1p, |
| 215 "expm1", MathExpm1 | 264 "expm1", MathExpm1 |
| 216 )); | 265 )); |
| 217 } | 266 } |
| 218 | 267 |
| 219 | 268 |
| 220 ExtendMath(); | 269 ExtendMath(); |
| OLD | NEW |