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 27 matching lines...) Expand all Loading... |
38 return; | 38 return; |
39 } | 39 } |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
43 #if V8_OS_CYGWIN || V8_OS_WIN | 43 #if V8_OS_CYGWIN || V8_OS_WIN |
44 // Use rand_s() to gather entropy on Windows. See: | 44 // Use rand_s() to gather entropy on Windows. See: |
45 // https://code.google.com/p/v8/issues/detail?id=2905 | 45 // https://code.google.com/p/v8/issues/detail?id=2905 |
46 unsigned first_half, second_half; | 46 unsigned first_half, second_half; |
47 errno_t result = rand_s(&first_half); | 47 errno_t result = rand_s(&first_half); |
48 ASSERT_EQ(0, result); | 48 DCHECK_EQ(0, result); |
49 result = rand_s(&second_half); | 49 result = rand_s(&second_half); |
50 ASSERT_EQ(0, result); | 50 DCHECK_EQ(0, result); |
51 SetSeed((static_cast<int64_t>(first_half) << 32) + second_half); | 51 SetSeed((static_cast<int64_t>(first_half) << 32) + second_half); |
52 #else | 52 #else |
53 // Gather entropy from /dev/urandom if available. | 53 // Gather entropy from /dev/urandom if available. |
54 FILE* fp = fopen("/dev/urandom", "rb"); | 54 FILE* fp = fopen("/dev/urandom", "rb"); |
55 if (fp != NULL) { | 55 if (fp != NULL) { |
56 int64_t seed; | 56 int64_t seed; |
57 size_t n = fread(&seed, sizeof(seed), 1, fp); | 57 size_t n = fread(&seed, sizeof(seed), 1, fp); |
58 fclose(fp); | 58 fclose(fp); |
59 if (n == 1) { | 59 if (n == 1) { |
60 SetSeed(seed); | 60 SetSeed(seed); |
(...skipping 11 matching lines...) Expand all Loading... |
72 // https://code.google.com/p/v8/issues/detail?id=2905 | 72 // https://code.google.com/p/v8/issues/detail?id=2905 |
73 int64_t seed = Time::NowFromSystemTime().ToInternalValue() << 24; | 73 int64_t seed = Time::NowFromSystemTime().ToInternalValue() << 24; |
74 seed ^= TimeTicks::HighResolutionNow().ToInternalValue() << 16; | 74 seed ^= TimeTicks::HighResolutionNow().ToInternalValue() << 16; |
75 seed ^= TimeTicks::Now().ToInternalValue() << 8; | 75 seed ^= TimeTicks::Now().ToInternalValue() << 8; |
76 SetSeed(seed); | 76 SetSeed(seed); |
77 #endif // V8_OS_CYGWIN || V8_OS_WIN | 77 #endif // V8_OS_CYGWIN || V8_OS_WIN |
78 } | 78 } |
79 | 79 |
80 | 80 |
81 int RandomNumberGenerator::NextInt(int max) { | 81 int RandomNumberGenerator::NextInt(int max) { |
82 ASSERT_LE(0, max); | 82 DCHECK_LE(0, max); |
83 | 83 |
84 // Fast path if max is a power of 2. | 84 // Fast path if max is a power of 2. |
85 if (IS_POWER_OF_TWO(max)) { | 85 if (IS_POWER_OF_TWO(max)) { |
86 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); |
87 } | 87 } |
88 | 88 |
89 while (true) { | 89 while (true) { |
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) { |
(...skipping 10 matching lines...) Expand all Loading... |
103 | 103 |
104 | 104 |
105 void RandomNumberGenerator::NextBytes(void* buffer, size_t buflen) { | 105 void RandomNumberGenerator::NextBytes(void* buffer, size_t buflen) { |
106 for (size_t n = 0; n < buflen; ++n) { | 106 for (size_t n = 0; n < buflen; ++n) { |
107 static_cast<uint8_t*>(buffer)[n] = static_cast<uint8_t>(Next(8)); | 107 static_cast<uint8_t*>(buffer)[n] = static_cast<uint8_t>(Next(8)); |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 | 111 |
112 int RandomNumberGenerator::Next(int bits) { | 112 int RandomNumberGenerator::Next(int bits) { |
113 ASSERT_LT(0, bits); | 113 DCHECK_LT(0, bits); |
114 ASSERT_GE(32, bits); | 114 DCHECK_GE(32, bits); |
115 // Do unsigned multiplication, which has the intended modulo semantics, while | 115 // Do unsigned multiplication, which has the intended modulo semantics, while |
116 // signed multiplication would expose undefined behavior. | 116 // signed multiplication would expose undefined behavior. |
117 uint64_t product = static_cast<uint64_t>(seed_) * kMultiplier; | 117 uint64_t product = static_cast<uint64_t>(seed_) * kMultiplier; |
118 // Assigning a uint64_t to an int64_t is implementation defined, but this | 118 // Assigning a uint64_t to an int64_t is implementation defined, but this |
119 // should be OK. Use a static_cast to explicitly state that we know what we're | 119 // should be OK. Use a static_cast to explicitly state that we know what we're |
120 // doing. (Famous last words...) | 120 // doing. (Famous last words...) |
121 int64_t seed = static_cast<int64_t>((product + kAddend) & kMask); | 121 int64_t seed = static_cast<int64_t>((product + kAddend) & kMask); |
122 seed_ = seed; | 122 seed_ = seed; |
123 return static_cast<int>(seed >> (48 - bits)); | 123 return static_cast<int>(seed >> (48 - bits)); |
124 } | 124 } |
125 | 125 |
126 | 126 |
127 void RandomNumberGenerator::SetSeed(int64_t seed) { | 127 void RandomNumberGenerator::SetSeed(int64_t seed) { |
128 seed_ = (seed ^ kMultiplier) & kMask; | 128 seed_ = (seed ^ kMultiplier) & kMask; |
129 } | 129 } |
130 | 130 |
131 } } // namespace v8::base | 131 } } // namespace v8::base |
OLD | NEW |