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

Side by Side Diff: src/js/math.js

Issue 1425693006: Store RNG state on function context. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: 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 unified diff | Download patch
« no previous file with comments | « src/bootstrapper.cc ('k') | src/js/prologue.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 var GlobalMath = global.Math; 13 var GlobalMath = global.Math;
14 var GlobalObject = global.Object; 14 var GlobalObject = global.Object;
15 var InternalArray = utils.InternalArray; 15 var InternalArray = utils.InternalArray;
16 var NaN = %GetRootNaN(); 16 var NaN = %GetRootNaN();
17 var rngstate; 17 var rngstate_0;
18 var rngstate_1;
19 var rngstate_2;
20 var rngstate_3;
18 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); 21 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
19 22
20 utils.SetupTypedArray(function(arg1, arg2, arg3) { 23 utils.InitializeRNG = function() {
21 rngstate = arg1; 24 var rngstate = %InitializeRNG();
22 }); 25 rngstate_0 = rngstate[0];
26 rngstate_1 = rngstate[1];
27 rngstate_2 = rngstate[2];
28 rngstate_3 = rngstate[3];
29 };
23 30
24 //------------------------------------------------------------------- 31 //-------------------------------------------------------------------
25 32
26 // ECMA 262 - 15.8.2.1 33 // ECMA 262 - 15.8.2.1
27 function MathAbs(x) { 34 function MathAbs(x) {
28 x = +x; 35 x = +x;
29 return (x > 0) ? x : 0 - x; 36 return (x > 0) ? x : 0 - x;
30 } 37 }
31 38
32 // ECMA 262 - 15.8.2.2 39 // ECMA 262 - 15.8.2.2
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 return r; 134 return r;
128 } 135 }
129 136
130 // ECMA 262 - 15.8.2.13 137 // ECMA 262 - 15.8.2.13
131 function MathPowJS(x, y) { 138 function MathPowJS(x, y) {
132 return %_MathPow(TO_NUMBER(x), TO_NUMBER(y)); 139 return %_MathPow(TO_NUMBER(x), TO_NUMBER(y));
133 } 140 }
134 141
135 // ECMA 262 - 15.8.2.14 142 // ECMA 262 - 15.8.2.14
136 function MathRandom() { 143 function MathRandom() {
137 var r0 = (MathImul(18030, rngstate[0] & 0xFFFF) + (rngstate[0] >>> 16)) | 0; 144 var r0 = (MathImul(18030, rngstate_0) + rngstate_1) | 0;
138 rngstate[0] = r0; 145 var r1 = (MathImul(36969, rngstate_2) + rngstate_3) | 0;
139 var r1 = (MathImul(36969, rngstate[1] & 0xFFFF) + (rngstate[1] >>> 16)) | 0; 146 rngstate_0 = r0 & 0xFFFF;
140 rngstate[1] = r1; 147 rngstate_1 = r0 >>> 16;
141 var x = ((r0 << 16) + (r1 & 0xFFFF)) | 0; 148 rngstate_2 = r1 & 0xFFFF;
142 // Division by 0x100000000 through multiplication by reciprocal. 149 rngstate_3 = r1 >>> 16;
143 return (x < 0 ? (x + 0x100000000) : x) * 2.3283064365386962890625e-10; 150 // Construct a double number 1.<32-bits of randomness> and subtract 1.
151 return %_ConstructDouble(0x3FF00000 | (r0 & 0x000FFFFF), r1 & 0xFFF00000) - 1;
144 } 152 }
145 153
146 function MathRandomRaw() { 154 function MathRandomRaw() {
147 var r0 = (MathImul(18030, rngstate[0] & 0xFFFF) + (rngstate[0] >>> 16)) | 0; 155 var r0 = (MathImul(18030, rngstate_0) + rngstate_1) | 0;
148 rngstate[0] = r0; 156 var r1 = (MathImul(36969, rngstate_2) + rngstate_3) | 0;
149 var r1 = (MathImul(36969, rngstate[1] & 0xFFFF) + (rngstate[1] >>> 16)) | 0; 157 rngstate_0 = r0 & 0xFFFF;
150 rngstate[1] = r1; 158 rngstate_1 = r0 >>> 16;
159 rngstate_2 = r1 & 0xFFFF;
160 rngstate_3 = r1 >>> 16;
151 var x = ((r0 << 16) + (r1 & 0xFFFF)) | 0; 161 var x = ((r0 << 16) + (r1 & 0xFFFF)) | 0;
152 return x & 0x3fffffff; 162 return x & 0x3FFFFFFF;
153 } 163 }
154 164
155 // ECMA 262 - 15.8.2.15 165 // ECMA 262 - 15.8.2.15
156 function MathRound(x) { 166 function MathRound(x) {
157 return %RoundNumber(TO_NUMBER(x)); 167 return %RoundNumber(TO_NUMBER(x));
158 } 168 }
159 169
160 // ECMA 262 - 15.8.2.17 170 // ECMA 262 - 15.8.2.17
161 function MathSqrtJS(x) { 171 function MathSqrtJS(x) {
162 return %_MathSqrt(+x); 172 return %_MathSqrt(+x);
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 utils.Export(function(to) { 358 utils.Export(function(to) {
349 to.MathAbs = MathAbs; 359 to.MathAbs = MathAbs;
350 to.MathExp = MathExp; 360 to.MathExp = MathExp;
351 to.MathFloor = MathFloorJS; 361 to.MathFloor = MathFloorJS;
352 to.IntRandom = MathRandomRaw; 362 to.IntRandom = MathRandomRaw;
353 to.MathMax = MathMax; 363 to.MathMax = MathMax;
354 to.MathMin = MathMin; 364 to.MathMin = MathMin;
355 }); 365 });
356 366
357 }) 367 })
OLDNEW
« 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