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

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

Issue 333053002: Untangle RNG from v8 core (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: updates Created 6 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « src/utils/random-number-generator.h ('k') | test/cctest/test-random-number-generator.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/utils/random-number-generator.h" 5 #include "src/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 "src/flags.h" 10 #include <new>
11
12 #include "src/base/macros.h"
11 #include "src/platform/mutex.h" 13 #include "src/platform/mutex.h"
12 #include "src/platform/time.h" 14 #include "src/platform/time.h"
13 #include "src/utils.h"
14 15
15 namespace v8 { 16 namespace v8 {
16 namespace internal { 17 namespace internal {
17 18
18 static LazyMutex entropy_mutex = LAZY_MUTEX_INITIALIZER; 19 static LazyMutex entropy_mutex = LAZY_MUTEX_INITIALIZER;
19 static RandomNumberGenerator::EntropySource entropy_source = NULL; 20 static RandomNumberGenerator::EntropySource entropy_source = NULL;
20 21
21 22
22 // static 23 // static
23 void RandomNumberGenerator::SetEntropySource(EntropySource source) { 24 void RandomNumberGenerator::SetEntropySource(EntropySource source) {
24 LockGuard<Mutex> lock_guard(entropy_mutex.Pointer()); 25 LockGuard<Mutex> lock_guard(entropy_mutex.Pointer());
25 entropy_source = source; 26 entropy_source = source;
26 } 27 }
27 28
28 29
29 RandomNumberGenerator::RandomNumberGenerator() { 30 RandomNumberGenerator::RandomNumberGenerator() {
30 // Check --random-seed flag first.
31 if (FLAG_random_seed != 0) {
32 SetSeed(FLAG_random_seed);
33 return;
34 }
35
36 // Check if embedder supplied an entropy source. 31 // Check if embedder supplied an entropy source.
37 { LockGuard<Mutex> lock_guard(entropy_mutex.Pointer()); 32 { LockGuard<Mutex> lock_guard(entropy_mutex.Pointer());
38 if (entropy_source != NULL) { 33 if (entropy_source != NULL) {
39 int64_t seed; 34 int64_t seed;
40 if (entropy_source(reinterpret_cast<unsigned char*>(&seed), 35 if (entropy_source(reinterpret_cast<unsigned char*>(&seed),
41 sizeof(seed))) { 36 sizeof(seed))) {
42 SetSeed(seed); 37 SetSeed(seed);
43 return; 38 return;
44 } 39 }
45 } 40 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 seed ^= TimeTicks::Now().ToInternalValue() << 8; 75 seed ^= TimeTicks::Now().ToInternalValue() << 8;
81 SetSeed(seed); 76 SetSeed(seed);
82 #endif // V8_OS_CYGWIN || V8_OS_WIN 77 #endif // V8_OS_CYGWIN || V8_OS_WIN
83 } 78 }
84 79
85 80
86 int RandomNumberGenerator::NextInt(int max) { 81 int RandomNumberGenerator::NextInt(int max) {
87 ASSERT_LE(0, max); 82 ASSERT_LE(0, max);
88 83
89 // Fast path if max is a power of 2. 84 // Fast path if max is a power of 2.
90 if (IsPowerOf2(max)) { 85 if (IS_POWER_OF_TWO(max)) {
91 return static_cast<int>((max * static_cast<int64_t>(Next(31))) >> 31); 86 return static_cast<int>((max * static_cast<int64_t>(Next(31))) >> 31);
92 } 87 }
93 88
94 while (true) { 89 while (true) {
95 int rnd = Next(31); 90 int rnd = Next(31);
96 int val = rnd % max; 91 int val = rnd % max;
97 if (rnd - val + (max - 1) >= 0) { 92 if (rnd - val + (max - 1) >= 0) {
98 return val; 93 return val;
99 } 94 }
100 } 95 }
(...skipping 26 matching lines...) Expand all
127 seed_ = seed; 122 seed_ = seed;
128 return static_cast<int>(seed >> (48 - bits)); 123 return static_cast<int>(seed >> (48 - bits));
129 } 124 }
130 125
131 126
132 void RandomNumberGenerator::SetSeed(int64_t seed) { 127 void RandomNumberGenerator::SetSeed(int64_t seed) {
133 seed_ = (seed ^ kMultiplier) & kMask; 128 seed_ = (seed ^ kMultiplier) & kMask;
134 } 129 }
135 130
136 } } // namespace v8::internal 131 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/utils/random-number-generator.h ('k') | test/cctest/test-random-number-generator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698