Index: src/js/math.js |
diff --git a/src/js/math.js b/src/js/math.js |
index 990a7e993c4f450bdd0879af036820099aa39ec2..18ea596408c6dc63754097a34008f52cdcf20b84 100644 |
--- a/src/js/math.js |
+++ b/src/js/math.js |
@@ -10,18 +10,17 @@ |
// ------------------------------------------------------------------- |
// Imports |
-define kRandomBatchSize = 64; |
-// The first two slots are reserved to persist PRNG state. |
-define kRandomNumberStart = 2; |
- |
-var GlobalFloat64Array = global.Float64Array; |
var GlobalMath = global.Math; |
var GlobalObject = global.Object; |
var InternalArray = utils.InternalArray; |
var NaN = %GetRootNaN(); |
-var nextRandomIndex = kRandomBatchSize; |
-var randomNumbers = UNDEFINED; |
+var rngstate = { a: 1, b: 2, c: 3, d: 4 }; |
var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
+ |
+utils.InitializeRNG = function() { |
+ var state = %InitializeRNG(); |
+ rngstate = { a: state[0], b: state[1], c: state[2], d: state[3] }; |
+}; |
//------------------------------------------------------------------- |
@@ -136,19 +135,26 @@ |
// ECMA 262 - 15.8.2.14 |
function MathRandom() { |
- if (nextRandomIndex >= kRandomBatchSize) { |
- randomNumbers = %GenerateRandomNumbers(randomNumbers); |
- nextRandomIndex = kRandomNumberStart; |
- } |
- return randomNumbers[nextRandomIndex++]; |
+ var r0 = (MathImul(18030, rngstate.a) + rngstate.b) | 0; |
+ var r1 = (MathImul(36969, rngstate.c) + rngstate.d) | 0; |
+ rngstate.a = r0 & 0xFFFF; |
+ rngstate.b = r0 >>> 16; |
+ rngstate.c = r1 & 0xFFFF; |
+ rngstate.d = r1 >>> 16; |
+ var r = r0 ^ r1; |
+ // Construct a double number 1.<32-bits of randomness> and subtract 1. |
+ return %_ConstructDouble(0x3FF00000 | (r & 0x000FFFFF), r & 0xFFF00000) - 1; |
} |
function MathRandomRaw() { |
- if (nextRandomIndex >= kRandomBatchSize) { |
- randomNumbers = %GenerateRandomNumbers(randomNumbers); |
- nextRandomIndex = kRandomNumberStart; |
- } |
- return %_DoubleLo(randomNumbers[nextRandomIndex++]) & 0x3FFFFFFF; |
+ var r0 = (MathImul(18030, rngstate.a) + rngstate.b) | 0; |
+ var r1 = (MathImul(36969, rngstate.c) + rngstate.d) | 0; |
+ rngstate.a = r0 & 0xFFFF; |
+ rngstate.b = r0 >>> 16; |
+ rngstate.c = r1 & 0xFFFF; |
+ rngstate.d = r1 >>> 16; |
+ var r = r0 ^ r1; |
+ return r & 0x3FFFFFFF; |
} |
// ECMA 262 - 15.8.2.15 |