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 23 matching lines...) Expand all Loading... |
34 // The naming of y and x matches the spec, as does the order in which | 34 // The naming of y and x matches the spec, as does the order in which |
35 // ToNumber (valueOf) is called. | 35 // ToNumber (valueOf) is called. |
36 function MathAtan2JS(y, x) { | 36 function MathAtan2JS(y, x) { |
37 y = +y; | 37 y = +y; |
38 x = +x; | 38 x = +x; |
39 return %MathAtan2(y, x); | 39 return %MathAtan2(y, x); |
40 } | 40 } |
41 | 41 |
42 // ECMA 262 - 15.8.2.6 | 42 // ECMA 262 - 15.8.2.6 |
43 function MathCeil(x) { | 43 function MathCeil(x) { |
44 return -%_MathFloor(-x); | 44 return -%math_floor(-x); |
45 } | 45 } |
46 | 46 |
47 // ECMA 262 - 15.8.2.8 | 47 // ECMA 262 - 15.8.2.8 |
48 function MathExp(x) { | 48 function MathExp(x) { |
49 return %MathExpRT(TO_NUMBER(x)); | 49 return %MathExpRT(TO_NUMBER(x)); |
50 } | 50 } |
51 | 51 |
52 // ECMA 262 - 15.8.2.9 | |
53 function MathFloorJS(x) { | |
54 return %_MathFloor(+x); | |
55 } | |
56 | |
57 // ECMA 262 - 15.8.2.10 | 52 // ECMA 262 - 15.8.2.10 |
58 function MathLog(x) { | 53 function MathLog(x) { |
59 return %_MathLogRT(TO_NUMBER(x)); | 54 return %_MathLogRT(TO_NUMBER(x)); |
60 } | 55 } |
61 | 56 |
62 // ECMA 262 - 15.8.2.13 | 57 // ECMA 262 - 15.8.2.13 |
63 function MathPowJS(x, y) { | 58 function MathPowJS(x, y) { |
64 return %_MathPow(TO_NUMBER(x), TO_NUMBER(y)); | 59 return %_MathPow(TO_NUMBER(x), TO_NUMBER(y)); |
65 } | 60 } |
66 | 61 |
(...skipping 29 matching lines...) Expand all Loading... |
96 x = +x; | 91 x = +x; |
97 if (x > 0) return 1; | 92 if (x > 0) return 1; |
98 if (x < 0) return -1; | 93 if (x < 0) return -1; |
99 // -0, 0 or NaN. | 94 // -0, 0 or NaN. |
100 return x; | 95 return x; |
101 } | 96 } |
102 | 97 |
103 // ES6 draft 09-27-13, section 20.2.2.34. | 98 // ES6 draft 09-27-13, section 20.2.2.34. |
104 function MathTrunc(x) { | 99 function MathTrunc(x) { |
105 x = +x; | 100 x = +x; |
106 if (x > 0) return %_MathFloor(x); | 101 if (x > 0) return %math_floor(x); |
107 if (x < 0) return -%_MathFloor(-x); | 102 if (x < 0) return -%math_floor(-x); |
108 // -0, 0 or NaN. | 103 // -0, 0 or NaN. |
109 return x; | 104 return x; |
110 } | 105 } |
111 | 106 |
112 // ES6 draft 09-27-13, section 20.2.2.5. | 107 // ES6 draft 09-27-13, section 20.2.2.5. |
113 function MathAsinh(x) { | 108 function MathAsinh(x) { |
114 x = TO_NUMBER(x); | 109 x = TO_NUMBER(x); |
115 // Idempotent for NaN, +/-0 and +/-Infinity. | 110 // Idempotent for NaN, +/-0 and +/-Infinity. |
116 if (x === 0 || !NUMBER_IS_FINITE(x)) return x; | 111 if (x === 0 || !NUMBER_IS_FINITE(x)) return x; |
117 if (x > 0) return MathLog(x + %math_sqrt(x * x + 1)); | 112 if (x > 0) return MathLog(x + %math_sqrt(x * x + 1)); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 x = TO_NUMBER(x); | 175 x = TO_NUMBER(x); |
181 if (x == 0 || !NUMBER_IS_FINITE(x)) return x; | 176 if (x == 0 || !NUMBER_IS_FINITE(x)) return x; |
182 return x >= 0 ? CubeRoot(x) : -CubeRoot(-x); | 177 return x >= 0 ? CubeRoot(x) : -CubeRoot(-x); |
183 } | 178 } |
184 | 179 |
185 macro NEWTON_ITERATION_CBRT(x, approx) | 180 macro NEWTON_ITERATION_CBRT(x, approx) |
186 (1.0 / 3.0) * (x / (approx * approx) + 2 * approx); | 181 (1.0 / 3.0) * (x / (approx * approx) + 2 * approx); |
187 endmacro | 182 endmacro |
188 | 183 |
189 function CubeRoot(x) { | 184 function CubeRoot(x) { |
190 var approx_hi = MathFloorJS(%_DoubleHi(x) / 3) + 0x2A9F7893; | 185 var approx_hi = %math_floor(%_DoubleHi(x) / 3) + 0x2A9F7893; |
191 var approx = %_ConstructDouble(approx_hi | 0, 0); | 186 var approx = %_ConstructDouble(approx_hi | 0, 0); |
192 approx = NEWTON_ITERATION_CBRT(x, approx); | 187 approx = NEWTON_ITERATION_CBRT(x, approx); |
193 approx = NEWTON_ITERATION_CBRT(x, approx); | 188 approx = NEWTON_ITERATION_CBRT(x, approx); |
194 approx = NEWTON_ITERATION_CBRT(x, approx); | 189 approx = NEWTON_ITERATION_CBRT(x, approx); |
195 return NEWTON_ITERATION_CBRT(x, approx); | 190 return NEWTON_ITERATION_CBRT(x, approx); |
196 } | 191 } |
197 | 192 |
198 // ------------------------------------------------------------------- | 193 // ------------------------------------------------------------------- |
199 | 194 |
200 %InstallToContext([ | 195 %InstallToContext([ |
(...skipping 18 matching lines...) Expand all Loading... |
219 "SQRT2", 1.4142135623730951 | 214 "SQRT2", 1.4142135623730951 |
220 ]); | 215 ]); |
221 | 216 |
222 // Set up non-enumerable functions of the Math object and | 217 // Set up non-enumerable functions of the Math object and |
223 // set their names. | 218 // set their names. |
224 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ | 219 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ |
225 "random", MathRandom, | 220 "random", MathRandom, |
226 "abs", MathAbs, | 221 "abs", MathAbs, |
227 "ceil", MathCeil, | 222 "ceil", MathCeil, |
228 "exp", MathExp, | 223 "exp", MathExp, |
229 "floor", MathFloorJS, | |
230 "log", MathLog, | 224 "log", MathLog, |
231 "round", MathRound, | 225 "round", MathRound, |
232 "atan2", MathAtan2JS, | 226 "atan2", MathAtan2JS, |
233 "pow", MathPowJS, | 227 "pow", MathPowJS, |
234 "sign", MathSign, | 228 "sign", MathSign, |
235 "trunc", MathTrunc, | 229 "trunc", MathTrunc, |
236 "asinh", MathAsinh, | 230 "asinh", MathAsinh, |
237 "acosh", MathAcosh, | 231 "acosh", MathAcosh, |
238 "atanh", MathAtanh, | 232 "atanh", MathAtanh, |
239 "hypot", MathHypot, | 233 "hypot", MathHypot, |
240 "clz32", MathClz32JS, | 234 "clz32", MathClz32JS, |
241 "cbrt", MathCbrt | 235 "cbrt", MathCbrt |
242 ]); | 236 ]); |
243 | 237 |
244 %SetForceInlineFlag(MathAbs); | 238 %SetForceInlineFlag(MathAbs); |
245 %SetForceInlineFlag(MathAtan2JS); | 239 %SetForceInlineFlag(MathAtan2JS); |
246 %SetForceInlineFlag(MathCeil); | 240 %SetForceInlineFlag(MathCeil); |
247 %SetForceInlineFlag(MathClz32JS); | 241 %SetForceInlineFlag(MathClz32JS); |
248 %SetForceInlineFlag(MathFloorJS); | |
249 %SetForceInlineFlag(MathRandom); | 242 %SetForceInlineFlag(MathRandom); |
250 %SetForceInlineFlag(MathSign); | 243 %SetForceInlineFlag(MathSign); |
251 %SetForceInlineFlag(MathTrunc); | 244 %SetForceInlineFlag(MathTrunc); |
252 | 245 |
253 // ------------------------------------------------------------------- | 246 // ------------------------------------------------------------------- |
254 // Exports | 247 // Exports |
255 | 248 |
256 utils.Export(function(to) { | 249 utils.Export(function(to) { |
257 to.MathAbs = MathAbs; | 250 to.MathAbs = MathAbs; |
258 to.MathExp = MathExp; | 251 to.MathExp = MathExp; |
259 to.MathFloor = MathFloorJS; | |
260 to.IntRandom = MathRandomRaw; | 252 to.IntRandom = MathRandomRaw; |
261 }); | 253 }); |
262 | 254 |
263 }) | 255 }) |
OLD | NEW |