2014-06-25 17:21:42 +02:00
|
|
|
//==- llvm/Support/RandomNumberGenerator.h - RNG for diversity ---*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-12-17 19:12:10 +01:00
|
|
|
// This file defines an abstraction for deterministic random number
|
|
|
|
// generation (RNG). Note that the current implementation is not
|
|
|
|
// cryptographically secure as it uses the C++11 <random> facilities.
|
2014-06-25 17:21:42 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
|
|
|
|
#define LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
|
|
|
|
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2014-06-25 17:47:36 +02:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2014-06-25 17:21:42 +02:00
|
|
|
#include "llvm/Support/DataTypes.h" // Needed for uint64_t on Windows.
|
|
|
|
#include <random>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/// A random number generator.
|
2014-12-17 19:12:10 +01:00
|
|
|
///
|
|
|
|
/// Instances of this class should not be shared across threads. The
|
|
|
|
/// seed should be set by passing the -rng-seed=<uint64> option. Use
|
|
|
|
/// Module::createRNG to create a new RNG instance for use with that
|
|
|
|
/// module.
|
2014-06-25 17:21:42 +02:00
|
|
|
class RandomNumberGenerator {
|
|
|
|
public:
|
|
|
|
/// Returns a random number in the range [0, Max).
|
2015-01-14 06:24:33 +01:00
|
|
|
uint_fast64_t operator()();
|
2014-06-25 17:21:42 +02:00
|
|
|
|
|
|
|
private:
|
2014-12-17 19:12:10 +01:00
|
|
|
/// Seeds and salts the underlying RNG engine.
|
|
|
|
///
|
|
|
|
/// This constructor should not be used directly. Instead use
|
|
|
|
/// Module::createRNG to create a new RNG salted with the Module ID.
|
|
|
|
RandomNumberGenerator(StringRef Salt);
|
|
|
|
|
2014-06-25 17:21:42 +02:00
|
|
|
// 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
|
|
|
|
// http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
|
2014-12-17 19:12:10 +01:00
|
|
|
// This RNG is deterministically portable across C++11
|
|
|
|
// implementations.
|
2015-01-14 06:24:33 +01:00
|
|
|
std::mt19937_64 Generator;
|
2014-06-25 17:21:42 +02:00
|
|
|
|
|
|
|
// Noncopyable.
|
2015-02-15 23:54:22 +01:00
|
|
|
RandomNumberGenerator(const RandomNumberGenerator &other) = delete;
|
|
|
|
RandomNumberGenerator &operator=(const RandomNumberGenerator &other) = delete;
|
2014-12-17 19:12:10 +01:00
|
|
|
|
|
|
|
friend class Module;
|
2014-06-25 17:21:42 +02:00
|
|
|
};
|
2015-06-23 11:49:53 +02:00
|
|
|
}
|
2014-06-25 17:21:42 +02:00
|
|
|
|
|
|
|
#endif
|