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

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

Issue 23548024: Introduce a RandonNumberGenerator class. Refactor the random/private_random uses in Isolate/Context. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: REBASE Created 7 years, 3 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') | src/v8.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "utils/random-number-generator.h"
29
30 #include <cstdio>
31
32 #include "flags.h"
33 #include "platform/mutex.h"
34 #include "platform/time.h"
35 #include "utils.h"
36
37 namespace v8 {
38 namespace internal {
39
40 static LazyMutex entropy_mutex = LAZY_MUTEX_INITIALIZER;
41 static RandomNumberGenerator::EntropySource entropy_source = NULL;
42
43
44 // static
45 void RandomNumberGenerator::SetEntropySource(EntropySource source) {
46 LockGuard<Mutex> lock_guard(entropy_mutex.Pointer());
47 entropy_source = source;
48 }
49
50
51 RandomNumberGenerator::RandomNumberGenerator() {
52 // Check --random-seed flag first.
53 if (FLAG_random_seed != 0) {
54 SetSeed(FLAG_random_seed);
55 return;
56 }
57
58 // Check if embedder supplied an entropy source.
59 { LockGuard<Mutex> lock_guard(entropy_mutex.Pointer());
60 if (entropy_source != NULL) {
61 int64_t seed;
62 if (entropy_source(reinterpret_cast<unsigned char*>(&seed),
63 sizeof(seed))) {
64 SetSeed(seed);
65 return;
66 }
67 }
68 }
69
70 // Gather entropy from /dev/urandom if available.
71 FILE* fp = fopen("/dev/urandom", "rb");
72 if (fp != NULL) {
73 int64_t seed;
74 size_t n = fread(&seed, sizeof(seed), 1, fp);
75 fclose(fp);
76 if (n == 1) {
77 SetSeed(seed);
78 return;
79 }
80 }
81
82 // We cannot assume that random() or rand() were seeded
83 // properly, so instead of relying on random() or rand(),
84 // we just seed our PRNG using timing data as fallback.
85 int64_t seed = Time::NowFromSystemTime().ToInternalValue() << 24;
86 seed ^= TimeTicks::HighResNow().ToInternalValue() << 16;
87 seed ^= TimeTicks::Now().ToInternalValue() << 8;
88 SetSeed(seed);
89 }
90
91
92 int RandomNumberGenerator::NextInt(int max) {
93 ASSERT_LE(0, max);
94
95 // Fast path if max is a power of 2.
96 if (IsPowerOf2(max)) {
97 return static_cast<int>((max * static_cast<int64_t>(Next(31))) >> 31);
98 }
99
100 while (true) {
101 int rnd = Next(31);
102 int val = rnd % max;
103 if (rnd - val + (max - 1) >= 0) {
104 return val;
105 }
106 }
107 }
108
109
110 double RandomNumberGenerator::NextDouble() {
111 return ((static_cast<int64_t>(Next(26)) << 27) + Next(27)) /
112 static_cast<double>(static_cast<int64_t>(1) << 53);
113 }
114
115
116 void RandomNumberGenerator::NextBytes(void* buffer, size_t buflen) {
117 for (size_t n = 0; n < buflen; ++n) {
118 static_cast<uint8_t*>(buffer)[n] = static_cast<uint8_t>(Next(8));
119 }
120 }
121
122
123 int RandomNumberGenerator::Next(int bits) {
124 ASSERT_LT(0, bits);
125 ASSERT_GE(32, bits);
126 int64_t seed = (seed_ * kMultiplier + kAddend) & kMask;
127 seed_ = seed;
128 return static_cast<int>(seed >> (48 - bits));
129 }
130
131
132 void RandomNumberGenerator::SetSeed(int64_t seed) {
133 seed_ = (seed ^ kMultiplier) & kMask;
134 }
135
136 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/utils/random-number-generator.h ('k') | src/v8.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698