aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Support/RandomNumberGenerator.cpp
diff options
context:
space:
mode:
authorStephen Hines <srhines@google.com>2015-04-01 18:49:24 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2015-04-01 18:49:26 +0000
commit3fa16bd6062e23bcdb82ed4dd965674792e6b761 (patch)
tree9348fc507292f7e8715d22d64ce5a32131b4f875 /lib/Support/RandomNumberGenerator.cpp
parentbeed47390a60f6f0c77532b3d3f76bb47ef49423 (diff)
parentebe69fe11e48d322045d5949c83283927a0d790b (diff)
downloadexternal_llvm-3fa16bd6062e23bcdb82ed4dd965674792e6b761.zip
external_llvm-3fa16bd6062e23bcdb82ed4dd965674792e6b761.tar.gz
external_llvm-3fa16bd6062e23bcdb82ed4dd965674792e6b761.tar.bz2
Merge "Update aosp/master LLVM for rebase to r230699."
Diffstat (limited to 'lib/Support/RandomNumberGenerator.cpp')
-rw-r--r--lib/Support/RandomNumberGenerator.cpp30
1 files changed, 12 insertions, 18 deletions
diff --git a/lib/Support/RandomNumberGenerator.cpp b/lib/Support/RandomNumberGenerator.cpp
index c50e7cb..2943137 100644
--- a/lib/Support/RandomNumberGenerator.cpp
+++ b/lib/Support/RandomNumberGenerator.cpp
@@ -7,16 +7,16 @@
//
//===----------------------------------------------------------------------===//
//
-// This file implements random number generation (RNG).
+// This file implements deterministic random number generation (RNG).
// The current implementation is NOT cryptographically secure as it uses
// the C++11 <random> facilities.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "rng"
-#include "llvm/Support/RandomNumberGenerator.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
+#include "llvm/Support/RandomNumberGenerator.h"
using namespace llvm;
@@ -31,31 +31,25 @@ Seed("rng-seed", cl::value_desc("seed"),
RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
DEBUG(
if (Seed == 0)
- errs() << "Warning! Using unseeded random number generator.\n"
+ dbgs() << "Warning! Using unseeded random number generator.\n"
);
- // Combine seed and salt using std::seed_seq.
- // Entropy: Seed-low, Seed-high, Salt...
+ // Combine seed and salts using std::seed_seq.
+ // Data: Seed-low, Seed-high, Salt
+ // Note: std::seed_seq can only store 32-bit values, even though we
+ // are using a 64-bit RNG. This isn't a problem since the Mersenne
+ // twister constructor copies these correctly into its initial state.
std::vector<uint32_t> Data;
- Data.reserve(2 + Salt.size()/4 + 1);
+ Data.reserve(2 + Salt.size());
Data.push_back(Seed);
Data.push_back(Seed >> 32);
- uint32_t Pack = 0;
- for (size_t I = 0; I < Salt.size(); ++I) {
- Pack <<= 8;
- Pack += Salt[I];
-
- if (I%4 == 3)
- Data.push_back(Pack);
- }
- Data.push_back(Pack);
+ std::copy(Salt.begin(), Salt.end(), Data.end());
std::seed_seq SeedSeq(Data.begin(), Data.end());
Generator.seed(SeedSeq);
}
-uint64_t RandomNumberGenerator::next(uint64_t Max) {
- std::uniform_int_distribution<uint64_t> distribution(0, Max - 1);
- return distribution(Generator);
+uint_fast64_t RandomNumberGenerator::operator()() {
+ return Generator();
}