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