OLD | NEW |
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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 117 |
118 int RandomNumberGenerator::Next(int bits) { | 118 int RandomNumberGenerator::Next(int bits) { |
119 DCHECK_LT(0, bits); | 119 DCHECK_LT(0, bits); |
120 DCHECK_GE(32, bits); | 120 DCHECK_GE(32, bits); |
121 XorShift128(&state0_, &state1_); | 121 XorShift128(&state0_, &state1_); |
122 return static_cast<int>((state0_ + state1_) >> (64 - bits)); | 122 return static_cast<int>((state0_ + state1_) >> (64 - bits)); |
123 } | 123 } |
124 | 124 |
125 | 125 |
126 void RandomNumberGenerator::SetSeed(int64_t seed) { | 126 void RandomNumberGenerator::SetSeed(int64_t seed) { |
127 if (seed == 0) seed = 1; | |
128 initial_seed_ = seed; | 127 initial_seed_ = seed; |
129 state0_ = MurmurHash3(bit_cast<uint64_t>(seed)); | 128 state0_ = MurmurHash3(bit_cast<uint64_t>(seed)); |
130 state1_ = MurmurHash3(state0_); | 129 state1_ = MurmurHash3(~state0_); |
| 130 CHECK(state0_ != 0 || state1_ != 0); |
131 } | 131 } |
132 | 132 |
133 | 133 |
134 uint64_t RandomNumberGenerator::MurmurHash3(uint64_t h) { | 134 uint64_t RandomNumberGenerator::MurmurHash3(uint64_t h) { |
135 h ^= h >> 33; | 135 h ^= h >> 33; |
136 h *= V8_UINT64_C(0xFF51AFD7ED558CCD); | 136 h *= V8_UINT64_C(0xFF51AFD7ED558CCD); |
137 h ^= h >> 33; | 137 h ^= h >> 33; |
138 h *= V8_UINT64_C(0xC4CEB9FE1A85EC53); | 138 h *= V8_UINT64_C(0xC4CEB9FE1A85EC53); |
139 h ^= h >> 33; | 139 h ^= h >> 33; |
140 return h; | 140 return h; |
141 } | 141 } |
142 | 142 |
143 } // namespace base | 143 } // namespace base |
144 } // namespace v8 | 144 } // namespace v8 |
OLD | NEW |