| OLD | NEW |
| 1 //===- subzero/src/IceRNG.h - Random number generator -----------*- C++ -*-===// | 1 //===- subzero/src/IceRNG.h - Random number generator -----------*- C++ -*-===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 /// | 9 /// |
| 10 /// \file | 10 /// \file |
| 11 /// This file declares a random number generator. | 11 /// This file declares a random number generator. |
| 12 /// | 12 /// |
| 13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
| 14 | 14 |
| 15 #ifndef SUBZERO_SRC_ICERNG_H | 15 #ifndef SUBZERO_SRC_ICERNG_H |
| 16 #define SUBZERO_SRC_ICERNG_H | 16 #define SUBZERO_SRC_ICERNG_H |
| 17 | 17 |
| 18 #include "llvm/ADT/StringRef.h" | 18 #include "llvm/ADT/StringRef.h" |
| 19 #include "llvm/Support/Compiler.h" | 19 #include "llvm/Support/Compiler.h" |
| 20 #include "IceDefs.h" |
| 21 |
| 20 #include <cstdint> | 22 #include <cstdint> |
| 21 | 23 |
| 22 namespace Ice { | 24 namespace Ice { |
| 23 | 25 |
| 24 class RandomNumberGenerator { | 26 class RandomNumberGenerator { |
| 25 RandomNumberGenerator() = delete; | 27 RandomNumberGenerator() = delete; |
| 26 RandomNumberGenerator(const RandomNumberGenerator &) = delete; | 28 RandomNumberGenerator(const RandomNumberGenerator &) = delete; |
| 27 RandomNumberGenerator &operator=(const RandomNumberGenerator &) = delete; | 29 RandomNumberGenerator &operator=(const RandomNumberGenerator &) = delete; |
| 28 | 30 |
| 29 public: | 31 public: |
| 30 explicit RandomNumberGenerator(uint64_t Seed, llvm::StringRef Salt = ""); | 32 explicit RandomNumberGenerator(uint64_t Seed, llvm::StringRef Salt = ""); |
| 33 /// Create a random number generator with: global seed, |
| 34 /// randomization pass ID and a salt uint64_t integer. |
| 35 /// @param Seed should be a global seed. |
| 36 /// @param RandomizationPassID should be one of RandomizationPassesEnum. |
| 37 /// @param Salt should be an additional integer input for generating unique |
| 38 /// RNG. |
| 39 /// Final seed = Seed ^ Salt << 16 ^ RandomizationPassID << 28 |
| 40 explicit RandomNumberGenerator(uint64_t Seed, |
| 41 RandomizationPassesEnum RandomizationPassID, |
| 42 uint64_t Salt = 0); |
| 31 uint64_t next(uint64_t Max); | 43 uint64_t next(uint64_t Max); |
| 32 | 44 |
| 33 private: | 45 private: |
| 34 uint64_t State; | 46 uint64_t State; |
| 35 }; | 47 }; |
| 36 | 48 |
| 37 /// This class adds additional random number generator utilities. The | 49 /// This class adds additional random number generator utilities. The |
| 38 /// reason for the wrapper class is that we want to keep the | 50 /// reason for the wrapper class is that we want to keep the |
| 39 /// RandomNumberGenerator interface identical to LLVM's. | 51 /// RandomNumberGenerator interface identical to LLVM's. |
| 40 class RandomNumberGeneratorWrapper { | 52 class RandomNumberGeneratorWrapper { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 58 /// sample implementation at cppreference.com. | 70 /// sample implementation at cppreference.com. |
| 59 template <class RandomIt, class RandomFunc> | 71 template <class RandomIt, class RandomFunc> |
| 60 void RandomShuffle(RandomIt First, RandomIt Last, RandomFunc &&RNG) { | 72 void RandomShuffle(RandomIt First, RandomIt Last, RandomFunc &&RNG) { |
| 61 for (auto i = Last - First - 1; i > 0; --i) | 73 for (auto i = Last - First - 1; i > 0; --i) |
| 62 std::swap(First[i], First[RNG(i + 1)]); | 74 std::swap(First[i], First[RNG(i + 1)]); |
| 63 } | 75 } |
| 64 | 76 |
| 65 } // end of namespace Ice | 77 } // end of namespace Ice |
| 66 | 78 |
| 67 #endif // SUBZERO_SRC_ICERNG_H | 79 #endif // SUBZERO_SRC_ICERNG_H |
| OLD | NEW |