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 |
| 201 function NewtonIterationCbrt(x, approx) { |
| 202 return (1.0 / 3.0) * (x / (approx * approx) + 2 * approx); |
| 203 } |
| 204 |
| 205 |
183 //ES6 draft 09-27-13, section 20.2.2.14. | 206 //ES6 draft 09-27-13, section 20.2.2.14. |
184 function MathExpm1(x) { | 207 function MathExpm1(x) { |
185 return %Math_expm1(TO_NUMBER_INLINE(x)); | 208 return %Math_expm1(TO_NUMBER_INLINE(x)); |
186 } | 209 } |
187 | 210 |
188 | 211 |
189 //ES6 draft 09-27-13, section 20.2.2.20. | 212 //ES6 draft 09-27-13, section 20.2.2.20. |
190 function MathLog1p(x) { | 213 function MathLog1p(x) { |
191 return %Math_log1p(TO_NUMBER_INLINE(x)); | 214 return %Math_log1p(TO_NUMBER_INLINE(x)); |
192 } | 215 } |
(...skipping 18 matching lines...) Expand all Loading... |
211 "fround", MathFround, | 234 "fround", MathFround, |
212 "clz32", MathClz32, | 235 "clz32", MathClz32, |
213 "cbrt", MathCbrt, | 236 "cbrt", MathCbrt, |
214 "log1p", MathLog1p, | 237 "log1p", MathLog1p, |
215 "expm1", MathExpm1 | 238 "expm1", MathExpm1 |
216 )); | 239 )); |
217 } | 240 } |
218 | 241 |
219 | 242 |
220 ExtendMath(); | 243 ExtendMath(); |
OLD | NEW |