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

Side by Side Diff: src/math.js

Issue 1158083002: Random hashes with a more cache-friendly distribution Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Revert inadvertent change! Created 5 years, 7 months 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 | « no previous file | no next file » | 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 var rngstate; // Initialized to a Uint32Array during genesis. 5 var rngstate; // Initialized to a Uint32Array during genesis.
6 6
7 (function(global, utils) { 7 (function(global, utils) {
8 "use strict"; 8 "use strict";
9 9
10 %CheckIsBootstrapping(); 10 %CheckIsBootstrapping();
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 function MathRandom() { 130 function MathRandom() {
131 var r0 = (MathImul(18030, rngstate[0] & 0xFFFF) + (rngstate[0] >>> 16)) | 0; 131 var r0 = (MathImul(18030, rngstate[0] & 0xFFFF) + (rngstate[0] >>> 16)) | 0;
132 rngstate[0] = r0; 132 rngstate[0] = r0;
133 var r1 = (MathImul(36969, rngstate[1] & 0xFFFF) + (rngstate[1] >>> 16)) | 0; 133 var r1 = (MathImul(36969, rngstate[1] & 0xFFFF) + (rngstate[1] >>> 16)) | 0;
134 rngstate[1] = r1; 134 rngstate[1] = r1;
135 var x = ((r0 << 16) + (r1 & 0xFFFF)) | 0; 135 var x = ((r0 << 16) + (r1 & 0xFFFF)) | 0;
136 // Division by 0x100000000 through multiplication by reciprocal. 136 // Division by 0x100000000 through multiplication by reciprocal.
137 return (x < 0 ? (x + 0x100000000) : x) * 2.3283064365386962890625e-10; 137 return (x < 0 ? (x + 0x100000000) : x) * 2.3283064365386962890625e-10;
138 } 138 }
139 139
140 function MathRandomRaw() { 140 var level_0_left = 0;
141 var level_0 = 0;
142 var level_1_left = 0;
143 var level_1 = 0;
144 var level_2_left = 0;
145 var level_2 = 0;
146
147
148 function RandomHelper() {
141 var r0 = (MathImul(18030, rngstate[0] & 0xFFFF) + (rngstate[0] >>> 16)) | 0; 149 var r0 = (MathImul(18030, rngstate[0] & 0xFFFF) + (rngstate[0] >>> 16)) | 0;
142 rngstate[0] = r0; 150 rngstate[0] = r0;
143 var r1 = (MathImul(36969, rngstate[1] & 0xFFFF) + (rngstate[1] >>> 16)) | 0; 151 var r1 = (MathImul(36969, rngstate[1] & 0xFFFF) + (rngstate[1] >>> 16)) | 0;
144 rngstate[1] = r1; 152 rngstate[1] = r1;
145 var x = ((r0 << 16) + (r1 & 0xFFFF)) | 0; 153 var x = ((r0 << 16) + (r1 & 0xFFFF)) | 0;
146 return x & 0x3fffffff; 154 return x & 0x3fffffff;
147 } 155 }
148 156
157 function MathRandomRaw() {
158 if (level_0_left != 0) {
159 // We have to clash 4 times before it gets annoying.
160 level_0 += 4;
Erik Corry Chromium.org 2015/05/26 21:56:56 This should perhaps be co-prime with powers of 2.
161 level_0_left--;
162 return level_0;
163 }
164 if (level_1_left != 0) {
165 level_0_left = 8;
166 level_0 = (RandomHelper() & 0x1ff) + level_1;
167 level_1_left--;
168 return level_0;
169 }
170 if (level_2_left == 0) {
171 // Reduced 1 bit so the '+' doesn't overflow Smi.
172 level_2 = (RandomHelper() & 0x1fffffff);
173 level_2_left = 8;
174 }
175 // Level 0 and 1 give 128 entries, and the mask is 512, so we have
176 // to clash 4 times before it gets annoying at this level too. The
177 // mask of 512 is chosen to stay in one page on a 4k-page 8-byte-pointer
178 // system.
179 level_1_left = 16;
180 level_1 = (RandomHelper() & 0x3ffff) + level_2;
181 level_0_left = 8;
182 level_0 = (RandomHelper() & 0x1ff) + level_1;
183 level_2_left--;
184 return level_0;
185 }
186
149 // ECMA 262 - 15.8.2.15 187 // ECMA 262 - 15.8.2.15
150 function MathRound(x) { 188 function MathRound(x) {
151 return %RoundNumber(TO_NUMBER_INLINE(x)); 189 return %RoundNumber(TO_NUMBER_INLINE(x));
152 } 190 }
153 191
154 // ECMA 262 - 15.8.2.17 192 // ECMA 262 - 15.8.2.17
155 function MathSqrtJS(x) { 193 function MathSqrtJS(x) {
156 return %_MathSqrt(+x); 194 return %_MathSqrt(+x);
157 } 195 }
158 196
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 utils.Export(function(to) { 403 utils.Export(function(to) {
366 to.MathAbs = MathAbs; 404 to.MathAbs = MathAbs;
367 to.MathExp = MathExp; 405 to.MathExp = MathExp;
368 to.MathFloor = MathFloorJS; 406 to.MathFloor = MathFloorJS;
369 to.IntRandom = MathRandomRaw; 407 to.IntRandom = MathRandomRaw;
370 to.MathMax = MathMax; 408 to.MathMax = MathMax;
371 to.MathMin = MathMin; 409 to.MathMin = MathMin;
372 }); 410 });
373 411
374 }) 412 })
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698