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

Side by Side Diff: src/base/utils/random-number-generator.cc

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 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/base/utils/random-number-generator.h ('k') | src/bootstrapper.cc » ('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 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 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 #include "src/base/utils/random-number-generator.h" 5 #include "src/base/utils/random-number-generator.h"
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 9
10 #include <new> 10 #include <new>
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 int rnd = Next(31); 90 int rnd = Next(31);
91 int val = rnd % max; 91 int val = rnd % max;
92 if (rnd - val + (max - 1) >= 0) { 92 if (rnd - val + (max - 1) >= 0) {
93 return val; 93 return val;
94 } 94 }
95 } 95 }
96 } 96 }
97 97
98 98
99 double RandomNumberGenerator::NextDouble() { 99 double RandomNumberGenerator::NextDouble() {
100 return ((static_cast<int64_t>(Next(26)) << 27) + Next(27)) / 100 XorShift128(&state0_, &state1_);
101 static_cast<double>(static_cast<int64_t>(1) << 53); 101 return ToDouble(state0_, state1_);
102 } 102 }
103 103
104 104
105 int64_t RandomNumberGenerator::NextInt64() { 105 int64_t RandomNumberGenerator::NextInt64() {
106 uint64_t lo = bit_cast<unsigned>(Next(32)); 106 XorShift128(&state0_, &state1_);
107 uint64_t hi = bit_cast<unsigned>(Next(32)); 107 return bit_cast<int64_t>(state0_ + state1_);
108 return lo | (hi << 32);
109 } 108 }
110 109
111 110
112 void RandomNumberGenerator::NextBytes(void* buffer, size_t buflen) { 111 void RandomNumberGenerator::NextBytes(void* buffer, size_t buflen) {
113 for (size_t n = 0; n < buflen; ++n) { 112 for (size_t n = 0; n < buflen; ++n) {
114 static_cast<uint8_t*>(buffer)[n] = static_cast<uint8_t>(Next(8)); 113 static_cast<uint8_t*>(buffer)[n] = static_cast<uint8_t>(Next(8));
115 } 114 }
116 } 115 }
117 116
118 117
119 int RandomNumberGenerator::Next(int bits) { 118 int RandomNumberGenerator::Next(int bits) {
120 DCHECK_LT(0, bits); 119 DCHECK_LT(0, bits);
121 DCHECK_GE(32, bits); 120 DCHECK_GE(32, bits);
122 // Do unsigned multiplication, which has the intended modulo semantics, while 121 XorShift128(&state0_, &state1_);
123 // signed multiplication would expose undefined behavior. 122 return static_cast<int>((state0_ + state1_) >> (64 - bits));
124 uint64_t product = static_cast<uint64_t>(seed_) * kMultiplier;
125 // Assigning a uint64_t to an int64_t is implementation defined, but this
126 // should be OK. Use a static_cast to explicitly state that we know what we're
127 // doing. (Famous last words...)
128 int64_t seed = static_cast<int64_t>((product + kAddend) & kMask);
129 seed_ = seed;
130 return static_cast<int>(seed >> (48 - bits));
131 } 123 }
132 124
133 125
134 void RandomNumberGenerator::SetSeed(int64_t seed) { 126 void RandomNumberGenerator::SetSeed(int64_t seed) {
135 initial_seed_ = seed; 127 initial_seed_ = seed;
136 seed_ = (seed ^ kMultiplier) & kMask; 128 state0_ = MurmurHash3(bit_cast<uint64_t>(seed));
129 state1_ = MurmurHash3(state0_);
130 }
131
132
133 uint64_t RandomNumberGenerator::MurmurHash3(uint64_t h) {
134 h ^= h >> 33;
135 h *= V8_UINT64_C(0xFF51AFD7ED558CCD);
136 h ^= h >> 33;
137 h *= V8_UINT64_C(0xC4CEB9FE1A85EC53);
138 h ^= h >> 33;
139 return h;
137 } 140 }
138 141
139 } // namespace base 142 } // namespace base
140 } // namespace v8 143 } // namespace v8
OLDNEW
« no previous file with comments | « src/base/utils/random-number-generator.h ('k') | src/bootstrapper.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698