Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(525)

Unified Diff: src/js/math.js

Issue 1464303002: Implement xorshift128+ for Math.random. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: add missing include Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/bootstrapper.cc ('k') | src/js/prologue.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/js/math.js
diff --git a/src/js/math.js b/src/js/math.js
index 18ea596408c6dc63754097a34008f52cdcf20b84..990a7e993c4f450bdd0879af036820099aa39ec2 100644
--- a/src/js/math.js
+++ b/src/js/math.js
@@ -10,18 +10,19 @@
// -------------------------------------------------------------------
// 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 rngstate = { a: 1, b: 2, c: 3, d: 4 };
+var nextRandomIndex = kRandomBatchSize;
+var randomNumbers = UNDEFINED;
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] };
-};
-
//-------------------------------------------------------------------
// ECMA 262 - 15.8.2.1
@@ -135,26 +136,19 @@ function MathPowJS(x, y) {
// ECMA 262 - 15.8.2.14
function MathRandom() {
- 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;
+ if (nextRandomIndex >= kRandomBatchSize) {
+ randomNumbers = %GenerateRandomNumbers(randomNumbers);
+ nextRandomIndex = kRandomNumberStart;
+ }
+ return randomNumbers[nextRandomIndex++];
}
function MathRandomRaw() {
- 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;
+ if (nextRandomIndex >= kRandomBatchSize) {
+ randomNumbers = %GenerateRandomNumbers(randomNumbers);
+ nextRandomIndex = kRandomNumberStart;
+ }
+ return %_DoubleLo(randomNumbers[nextRandomIndex++]) & 0x3FFFFFFF;
}
// ECMA 262 - 15.8.2.15
« no previous file with comments | « src/bootstrapper.cc ('k') | src/js/prologue.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698