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 // ------------------------------------------------------------------- |
11 // Imports | 11 // Imports |
12 | 12 |
| 13 define kRandomBatchSize = 64; |
13 // The first two slots are reserved to persist PRNG state. | 14 // The first two slots are reserved to persist PRNG state. |
14 define kRandomNumberStart = 2; | 15 define kRandomNumberStart = 2; |
15 | 16 |
16 var GlobalFloat64Array = global.Float64Array; | 17 var GlobalFloat64Array = global.Float64Array; |
17 var GlobalMath = global.Math; | 18 var GlobalMath = global.Math; |
18 var GlobalObject = global.Object; | 19 var GlobalObject = global.Object; |
19 var InternalArray = utils.InternalArray; | 20 var InternalArray = utils.InternalArray; |
20 var NaN = %GetRootNaN(); | 21 var NaN = %GetRootNaN(); |
21 var nextRandomIndex = 0; | 22 var nextRandomIndex = kRandomBatchSize; |
22 var randomNumbers = UNDEFINED; | 23 var randomNumbers = UNDEFINED; |
23 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 24 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
24 | 25 |
25 //------------------------------------------------------------------- | 26 //------------------------------------------------------------------- |
26 | 27 |
27 // ECMA 262 - 15.8.2.1 | 28 // ECMA 262 - 15.8.2.1 |
28 function MathAbs(x) { | 29 function MathAbs(x) { |
29 x = +x; | 30 x = +x; |
30 return (x > 0) ? x : 0 - x; | 31 return (x > 0) ? x : 0 - x; |
31 } | 32 } |
(...skipping 27 matching lines...) Expand all Loading... |
59 return %_MathLogRT(TO_NUMBER(x)); | 60 return %_MathLogRT(TO_NUMBER(x)); |
60 } | 61 } |
61 | 62 |
62 // ECMA 262 - 15.8.2.13 | 63 // ECMA 262 - 15.8.2.13 |
63 function MathPowJS(x, y) { | 64 function MathPowJS(x, y) { |
64 return %_MathPow(TO_NUMBER(x), TO_NUMBER(y)); | 65 return %_MathPow(TO_NUMBER(x), TO_NUMBER(y)); |
65 } | 66 } |
66 | 67 |
67 // ECMA 262 - 15.8.2.14 | 68 // ECMA 262 - 15.8.2.14 |
68 function MathRandom() { | 69 function MathRandom() { |
69 // While creating a startup snapshot, %GenerateRandomNumbers returns a | 70 if (nextRandomIndex >= kRandomBatchSize) { |
70 // normal array containing a single random number, and has to be called for | |
71 // every new random number. | |
72 // Otherwise, it returns a pre-populated typed array of random numbers. The | |
73 // first two elements are reserved for the PRNG state. | |
74 if (nextRandomIndex <= kRandomNumberStart) { | |
75 randomNumbers = %GenerateRandomNumbers(randomNumbers); | 71 randomNumbers = %GenerateRandomNumbers(randomNumbers); |
76 nextRandomIndex = randomNumbers.length; | 72 nextRandomIndex = kRandomNumberStart; |
77 } | 73 } |
78 return randomNumbers[--nextRandomIndex]; | 74 return randomNumbers[nextRandomIndex++]; |
79 } | 75 } |
80 | 76 |
81 function MathRandomRaw() { | 77 function MathRandomRaw() { |
82 if (nextRandomIndex <= kRandomNumberStart) { | 78 if (nextRandomIndex >= kRandomBatchSize) { |
83 randomNumbers = %GenerateRandomNumbers(randomNumbers); | 79 randomNumbers = %GenerateRandomNumbers(randomNumbers); |
84 nextRandomIndex = randomNumbers.length; | 80 nextRandomIndex = kRandomNumberStart; |
85 } | 81 } |
86 return %_DoubleLo(randomNumbers[--nextRandomIndex]) & 0x3FFFFFFF; | 82 return %_DoubleLo(randomNumbers[nextRandomIndex++]) & 0x3FFFFFFF; |
87 } | 83 } |
88 | 84 |
89 // ECMA 262 - 15.8.2.15 | 85 // ECMA 262 - 15.8.2.15 |
90 function MathRound(x) { | 86 function MathRound(x) { |
91 return %RoundNumber(TO_NUMBER(x)); | 87 return %RoundNumber(TO_NUMBER(x)); |
92 } | 88 } |
93 | 89 |
94 // ECMA 262 - 15.8.2.17 | 90 // ECMA 262 - 15.8.2.17 |
95 function MathSqrtJS(x) { | 91 function MathSqrtJS(x) { |
96 return %_MathSqrt(+x); | 92 return %_MathSqrt(+x); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 // Exports | 253 // Exports |
258 | 254 |
259 utils.Export(function(to) { | 255 utils.Export(function(to) { |
260 to.MathAbs = MathAbs; | 256 to.MathAbs = MathAbs; |
261 to.MathExp = MathExp; | 257 to.MathExp = MathExp; |
262 to.MathFloor = MathFloorJS; | 258 to.MathFloor = MathFloorJS; |
263 to.IntRandom = MathRandomRaw; | 259 to.IntRandom = MathRandomRaw; |
264 }); | 260 }); |
265 | 261 |
266 }) | 262 }) |
OLD | NEW |