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