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. | |
15 define kRandomNumberStart = 2; | |
16 | |
17 var GlobalFloat64Array = global.Float64Array; | |
18 var GlobalMath = global.Math; | 13 var GlobalMath = global.Math; |
19 var GlobalObject = global.Object; | 14 var GlobalObject = global.Object; |
20 var InternalArray = utils.InternalArray; | 15 var InternalArray = utils.InternalArray; |
21 var NaN = %GetRootNaN(); | 16 var NaN = %GetRootNaN(); |
22 var nextRandomIndex = kRandomBatchSize; | 17 var rngstate = { a: 1, b: 2, c: 3, d: 4 }; |
23 var randomNumbers = UNDEFINED; | |
24 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 18 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
25 | 19 |
| 20 utils.InitializeRNG = function() { |
| 21 var state = %InitializeRNG(); |
| 22 rngstate = { a: state[0], b: state[1], c: state[2], d: state[3] }; |
| 23 }; |
| 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 } |
33 | 32 |
34 // ECMA 262 - 15.8.2.2 | 33 // ECMA 262 - 15.8.2.2 |
35 function MathAcosJS(x) { | 34 function MathAcosJS(x) { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 return r; | 128 return r; |
130 } | 129 } |
131 | 130 |
132 // ECMA 262 - 15.8.2.13 | 131 // ECMA 262 - 15.8.2.13 |
133 function MathPowJS(x, y) { | 132 function MathPowJS(x, y) { |
134 return %_MathPow(TO_NUMBER(x), TO_NUMBER(y)); | 133 return %_MathPow(TO_NUMBER(x), TO_NUMBER(y)); |
135 } | 134 } |
136 | 135 |
137 // ECMA 262 - 15.8.2.14 | 136 // ECMA 262 - 15.8.2.14 |
138 function MathRandom() { | 137 function MathRandom() { |
139 if (nextRandomIndex >= kRandomBatchSize) { | 138 var r0 = (MathImul(18030, rngstate.a) + rngstate.b) | 0; |
140 randomNumbers = %GenerateRandomNumbers(randomNumbers); | 139 var r1 = (MathImul(36969, rngstate.c) + rngstate.d) | 0; |
141 nextRandomIndex = kRandomNumberStart; | 140 rngstate.a = r0 & 0xFFFF; |
142 } | 141 rngstate.b = r0 >>> 16; |
143 return randomNumbers[nextRandomIndex++]; | 142 rngstate.c = r1 & 0xFFFF; |
| 143 rngstate.d = r1 >>> 16; |
| 144 var r = r0 ^ r1; |
| 145 // Construct a double number 1.<32-bits of randomness> and subtract 1. |
| 146 return %_ConstructDouble(0x3FF00000 | (r & 0x000FFFFF), r & 0xFFF00000) - 1; |
144 } | 147 } |
145 | 148 |
146 function MathRandomRaw() { | 149 function MathRandomRaw() { |
147 if (nextRandomIndex >= kRandomBatchSize) { | 150 var r0 = (MathImul(18030, rngstate.a) + rngstate.b) | 0; |
148 randomNumbers = %GenerateRandomNumbers(randomNumbers); | 151 var r1 = (MathImul(36969, rngstate.c) + rngstate.d) | 0; |
149 nextRandomIndex = kRandomNumberStart; | 152 rngstate.a = r0 & 0xFFFF; |
150 } | 153 rngstate.b = r0 >>> 16; |
151 return %_DoubleLo(randomNumbers[nextRandomIndex++]) & 0x3FFFFFFF; | 154 rngstate.c = r1 & 0xFFFF; |
| 155 rngstate.d = r1 >>> 16; |
| 156 var r = r0 ^ r1; |
| 157 return r & 0x3FFFFFFF; |
152 } | 158 } |
153 | 159 |
154 // ECMA 262 - 15.8.2.15 | 160 // ECMA 262 - 15.8.2.15 |
155 function MathRound(x) { | 161 function MathRound(x) { |
156 return %RoundNumber(TO_NUMBER(x)); | 162 return %RoundNumber(TO_NUMBER(x)); |
157 } | 163 } |
158 | 164 |
159 // ECMA 262 - 15.8.2.17 | 165 // ECMA 262 - 15.8.2.17 |
160 function MathSqrtJS(x) { | 166 function MathSqrtJS(x) { |
161 return %_MathSqrt(+x); | 167 return %_MathSqrt(+x); |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 utils.Export(function(to) { | 353 utils.Export(function(to) { |
348 to.MathAbs = MathAbs; | 354 to.MathAbs = MathAbs; |
349 to.MathExp = MathExp; | 355 to.MathExp = MathExp; |
350 to.MathFloor = MathFloorJS; | 356 to.MathFloor = MathFloorJS; |
351 to.IntRandom = MathRandomRaw; | 357 to.IntRandom = MathRandomRaw; |
352 to.MathMax = MathMax; | 358 to.MathMax = MathMax; |
353 to.MathMin = MathMin; | 359 to.MathMin = MathMin; |
354 }); | 360 }); |
355 | 361 |
356 }) | 362 }) |
OLD | NEW |