Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:33

0001 //==- llvm/Support/RandomNumberGenerator.h - RNG for diversity ---*- C++ -*-==//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines an abstraction for deterministic random number
0010 // generation (RNG).  Note that the current implementation is not
0011 // cryptographically secure as it uses the C++11 <random> facilities.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
0016 #define LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
0017 
0018 #include "llvm/Support/Compiler.h"
0019 #include "llvm/Support/DataTypes.h" // Needed for uint64_t on Windows.
0020 #include <random>
0021 #include <system_error>
0022 
0023 namespace llvm {
0024 class StringRef;
0025 
0026 /// A random number generator.
0027 ///
0028 /// Instances of this class should not be shared across threads. The
0029 /// seed should be set by passing the -rng-seed=<uint64> option. Use
0030 /// Module::createRNG to create a new RNG instance for use with that
0031 /// module.
0032 class RandomNumberGenerator {
0033 
0034   // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
0035   // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
0036   // This RNG is deterministically portable across C++11
0037   // implementations.
0038   using generator_type = std::mt19937_64;
0039 
0040 public:
0041   using result_type = generator_type::result_type;
0042 
0043   /// Returns a random number in the range [0, Max).
0044   result_type operator()();
0045 
0046   static constexpr result_type min() { return generator_type::min(); }
0047   static constexpr result_type max() { return generator_type::max(); }
0048 
0049 private:
0050   /// Seeds and salts the underlying RNG engine.
0051   ///
0052   /// This constructor should not be used directly. Instead use
0053   /// Module::createRNG to create a new RNG salted with the Module ID.
0054   RandomNumberGenerator(StringRef Salt);
0055 
0056   generator_type Generator;
0057 
0058   // Noncopyable.
0059   RandomNumberGenerator(const RandomNumberGenerator &other) = delete;
0060   RandomNumberGenerator &operator=(const RandomNumberGenerator &other) = delete;
0061 
0062   friend class Module;
0063 };
0064 
0065 // Get random vector of specified size
0066 std::error_code getRandomBytes(void *Buffer, size_t Size);
0067 }
0068 
0069 #endif