Files
archived-llvm/include/llvm/Support/RandomNumberGenerator.h
Justin Lebar 6feedc56f7 Remove LLVM_CONSTEXPR.
Summary: With MSVC 2013 and GCC < 4.8 gone, we can use the "constexpr" keyword.

Reviewers: bkramer, mehdi_amini

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D25901

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@284947 91177308-0d34-0410-b5e6-96231b3b80d8
2016-10-23 19:39:16 +00:00

81 lines
2.6 KiB
C++

//==- 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.
//
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
#define LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h" // Needed for uint64_t on Windows.
#include <random>
#include <system_error>
namespace llvm {
class StringRef;
/// A random number generator.
///
/// 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.
class RandomNumberGenerator {
// 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
// http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
// This RNG is deterministically portable across C++11
// implementations.
using generator_type = std::mt19937_64;
public:
using result_type = generator_type::result_type;
/// Returns a random number in the range [0, Max).
result_type operator()();
// We can only make min/max constexpr if generator_type::min/max are
// constexpr. The MSVC 2013 STL does not make these constexpr, so we have to
// avoid declaring them as constexpr even if the compiler, like clang-cl,
// supports it.
#if defined(_MSC_VER) && _MSC_VER < 1900
#define STL_CONSTEXPR
#else
#define STL_CONSTEXPR constexpr
#endif
static STL_CONSTEXPR result_type min() { return generator_type::min(); }
static STL_CONSTEXPR result_type max() { return generator_type::max(); }
private:
/// 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);
generator_type Generator;
// Noncopyable.
RandomNumberGenerator(const RandomNumberGenerator &other) = delete;
RandomNumberGenerator &operator=(const RandomNumberGenerator &other) = delete;
friend class Module;
};
// Get random vector of specified size
std::error_code getRandomBytes(void *Buffer, size_t Size);
}
#endif