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 var rngstate; // Initialized to a Uint32Array during genesis. | 5 var rngstate; // Initialized to a Uint32Array during genesis. |
6 | 6 |
7 (function(global, utils) { | 7 (function(global, utils) { |
8 "use strict"; | 8 "use strict"; |
9 | 9 |
10 %CheckIsBootstrapping(); | 10 %CheckIsBootstrapping(); |
11 | 11 |
12 // ------------------------------------------------------------------- | 12 // ------------------------------------------------------------------- |
13 // Imports | 13 // Imports |
14 | 14 |
15 var GlobalMath = global.Math; | 15 var GlobalMath = global.Math; |
16 var GlobalObject = global.Object; | 16 var GlobalObject = global.Object; |
17 var InternalArray = utils.InternalArray; | 17 var InternalArray = utils.InternalArray; |
| 18 var NaN = %GetRootNaN(); |
18 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 19 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
19 | 20 |
20 //------------------------------------------------------------------- | 21 //------------------------------------------------------------------- |
21 | 22 |
22 // ECMA 262 - 15.8.2.1 | 23 // ECMA 262 - 15.8.2.1 |
23 function MathAbs(x) { | 24 function MathAbs(x) { |
24 x = +x; | 25 x = +x; |
25 return (x > 0) ? x : 0 - x; | 26 return (x > 0) ? x : 0 - x; |
26 } | 27 } |
27 | 28 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 if (length == 2) { | 76 if (length == 2) { |
76 arg1 = TO_NUMBER(arg1); | 77 arg1 = TO_NUMBER(arg1); |
77 arg2 = TO_NUMBER(arg2); | 78 arg2 = TO_NUMBER(arg2); |
78 if (arg2 > arg1) return arg2; | 79 if (arg2 > arg1) return arg2; |
79 if (arg1 > arg2) return arg1; | 80 if (arg1 > arg2) return arg1; |
80 if (arg1 == arg2) { | 81 if (arg1 == arg2) { |
81 // Make sure -0 is considered less than +0. | 82 // Make sure -0 is considered less than +0. |
82 return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg2 : arg1; | 83 return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg2 : arg1; |
83 } | 84 } |
84 // All comparisons failed, one of the arguments must be NaN. | 85 // All comparisons failed, one of the arguments must be NaN. |
85 return NAN; | 86 return NaN; |
86 } | 87 } |
87 var r = -INFINITY; | 88 var r = -INFINITY; |
88 for (var i = 0; i < length; i++) { | 89 for (var i = 0; i < length; i++) { |
89 var n = %_Arguments(i); | 90 var n = %_Arguments(i); |
90 n = TO_NUMBER(n); | 91 n = TO_NUMBER(n); |
91 // Make sure +0 is considered greater than -0. | 92 // Make sure +0 is considered greater than -0. |
92 if (NUMBER_IS_NAN(n) || n > r || (r === 0 && n === 0 && %_IsMinusZero(r))) { | 93 if (NUMBER_IS_NAN(n) || n > r || (r === 0 && n === 0 && %_IsMinusZero(r))) { |
93 r = n; | 94 r = n; |
94 } | 95 } |
95 } | 96 } |
96 return r; | 97 return r; |
97 } | 98 } |
98 | 99 |
99 // ECMA 262 - 15.8.2.12 | 100 // ECMA 262 - 15.8.2.12 |
100 function MathMin(arg1, arg2) { // length == 2 | 101 function MathMin(arg1, arg2) { // length == 2 |
101 var length = %_ArgumentsLength(); | 102 var length = %_ArgumentsLength(); |
102 if (length == 2) { | 103 if (length == 2) { |
103 arg1 = TO_NUMBER(arg1); | 104 arg1 = TO_NUMBER(arg1); |
104 arg2 = TO_NUMBER(arg2); | 105 arg2 = TO_NUMBER(arg2); |
105 if (arg2 > arg1) return arg1; | 106 if (arg2 > arg1) return arg1; |
106 if (arg1 > arg2) return arg2; | 107 if (arg1 > arg2) return arg2; |
107 if (arg1 == arg2) { | 108 if (arg1 == arg2) { |
108 // Make sure -0 is considered less than +0. | 109 // Make sure -0 is considered less than +0. |
109 return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg1 : arg2; | 110 return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg1 : arg2; |
110 } | 111 } |
111 // All comparisons failed, one of the arguments must be NaN. | 112 // All comparisons failed, one of the arguments must be NaN. |
112 return NAN; | 113 return NaN; |
113 } | 114 } |
114 var r = INFINITY; | 115 var r = INFINITY; |
115 for (var i = 0; i < length; i++) { | 116 for (var i = 0; i < length; i++) { |
116 var n = %_Arguments(i); | 117 var n = %_Arguments(i); |
117 n = TO_NUMBER(n); | 118 n = TO_NUMBER(n); |
118 // Make sure -0 is considered less than +0. | 119 // Make sure -0 is considered less than +0. |
119 if (NUMBER_IS_NAN(n) || n < r || (r === 0 && n === 0 && %_IsMinusZero(n))) { | 120 if (NUMBER_IS_NAN(n) || n < r || (r === 0 && n === 0 && %_IsMinusZero(n))) { |
120 r = n; | 121 r = n; |
121 } | 122 } |
122 } | 123 } |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 // Idempotent for NaN, +/-0 and +/-Infinity. | 200 // Idempotent for NaN, +/-0 and +/-Infinity. |
200 if (x === 0 || !NUMBER_IS_FINITE(x)) return x; | 201 if (x === 0 || !NUMBER_IS_FINITE(x)) return x; |
201 if (x > 0) return MathLog(x + %_MathSqrt(x * x + 1)); | 202 if (x > 0) return MathLog(x + %_MathSqrt(x * x + 1)); |
202 // This is to prevent numerical errors caused by large negative x. | 203 // This is to prevent numerical errors caused by large negative x. |
203 return -MathLog(-x + %_MathSqrt(x * x + 1)); | 204 return -MathLog(-x + %_MathSqrt(x * x + 1)); |
204 } | 205 } |
205 | 206 |
206 // ES6 draft 09-27-13, section 20.2.2.3. | 207 // ES6 draft 09-27-13, section 20.2.2.3. |
207 function MathAcosh(x) { | 208 function MathAcosh(x) { |
208 x = TO_NUMBER(x); | 209 x = TO_NUMBER(x); |
209 if (x < 1) return NAN; | 210 if (x < 1) return NaN; |
210 // Idempotent for NaN and +Infinity. | 211 // Idempotent for NaN and +Infinity. |
211 if (!NUMBER_IS_FINITE(x)) return x; | 212 if (!NUMBER_IS_FINITE(x)) return x; |
212 return MathLog(x + %_MathSqrt(x + 1) * %_MathSqrt(x - 1)); | 213 return MathLog(x + %_MathSqrt(x + 1) * %_MathSqrt(x - 1)); |
213 } | 214 } |
214 | 215 |
215 // ES6 draft 09-27-13, section 20.2.2.7. | 216 // ES6 draft 09-27-13, section 20.2.2.7. |
216 function MathAtanh(x) { | 217 function MathAtanh(x) { |
217 x = TO_NUMBER(x); | 218 x = TO_NUMBER(x); |
218 // Idempotent for +/-0. | 219 // Idempotent for +/-0. |
219 if (x === 0) return x; | 220 if (x === 0) return x; |
220 // Returns NaN for NaN and +/- Infinity. | 221 // Returns NaN for NaN and +/- Infinity. |
221 if (!NUMBER_IS_FINITE(x)) return NAN; | 222 if (!NUMBER_IS_FINITE(x)) return NaN; |
222 return 0.5 * MathLog((1 + x) / (1 - x)); | 223 return 0.5 * MathLog((1 + x) / (1 - x)); |
223 } | 224 } |
224 | 225 |
225 // ES6 draft 09-27-13, section 20.2.2.17. | 226 // ES6 draft 09-27-13, section 20.2.2.17. |
226 function MathHypot(x, y) { // Function length is 2. | 227 function MathHypot(x, y) { // Function length is 2. |
227 // We may want to introduce fast paths for two arguments and when | 228 // We may want to introduce fast paths for two arguments and when |
228 // normalization to avoid overflow is not necessary. For now, we | 229 // normalization to avoid overflow is not necessary. For now, we |
229 // simply assume the general case. | 230 // simply assume the general case. |
230 var length = %_ArgumentsLength(); | 231 var length = %_ArgumentsLength(); |
231 var args = new InternalArray(length); | 232 var args = new InternalArray(length); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 utils.Export(function(to) { | 358 utils.Export(function(to) { |
358 to.MathAbs = MathAbs; | 359 to.MathAbs = MathAbs; |
359 to.MathExp = MathExp; | 360 to.MathExp = MathExp; |
360 to.MathFloor = MathFloorJS; | 361 to.MathFloor = MathFloorJS; |
361 to.IntRandom = MathRandomRaw; | 362 to.IntRandom = MathRandomRaw; |
362 to.MathMax = MathMax; | 363 to.MathMax = MathMax; |
363 to.MathMin = MathMin; | 364 to.MathMin = MathMin; |
364 }); | 365 }); |
365 | 366 |
366 }) | 367 }) |
OLD | NEW |