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 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 /// it gets shifted by 48 before merging. | 45 /// it gets shifted by 48 before merging. |
46 explicit RandomNumberGenerator(uint64_t Seed, | 46 explicit RandomNumberGenerator(uint64_t Seed, |
47 RandomizationPassesEnum RandomizationPassID, | 47 RandomizationPassesEnum RandomizationPassID, |
48 uint64_t Salt = 0); | 48 uint64_t Salt = 0); |
49 uint64_t next(uint64_t Max); | 49 uint64_t next(uint64_t Max); |
50 | 50 |
51 private: | 51 private: |
52 uint64_t State; | 52 uint64_t State; |
53 }; | 53 }; |
54 | 54 |
55 /// This class adds additional random number generator utilities. The | 55 /// This class adds additional random number generator utilities. The reason for |
56 /// reason for the wrapper class is that we want to keep the | 56 /// the wrapper class is that we want to keep the RandomNumberGenerator |
57 /// RandomNumberGenerator interface identical to LLVM's. | 57 /// interface identical to LLVM's. |
58 class RandomNumberGeneratorWrapper { | 58 class RandomNumberGeneratorWrapper { |
59 RandomNumberGeneratorWrapper() = delete; | 59 RandomNumberGeneratorWrapper() = delete; |
60 RandomNumberGeneratorWrapper(const RandomNumberGeneratorWrapper &) = delete; | 60 RandomNumberGeneratorWrapper(const RandomNumberGeneratorWrapper &) = delete; |
61 RandomNumberGeneratorWrapper & | 61 RandomNumberGeneratorWrapper & |
62 operator=(const RandomNumberGeneratorWrapper &) = delete; | 62 operator=(const RandomNumberGeneratorWrapper &) = delete; |
63 | 63 |
64 public: | 64 public: |
65 uint64_t operator()(uint64_t Max) { return RNG.next(Max); } | 65 uint64_t operator()(uint64_t Max) { return RNG.next(Max); } |
66 bool getTrueWithProbability(float Probability); | 66 bool getTrueWithProbability(float Probability); |
67 explicit RandomNumberGeneratorWrapper(RandomNumberGenerator &RNG) | 67 explicit RandomNumberGeneratorWrapper(RandomNumberGenerator &RNG) |
68 : RNG(RNG) {} | 68 : RNG(RNG) {} |
69 | 69 |
70 private: | 70 private: |
71 RandomNumberGenerator &RNG; | 71 RandomNumberGenerator &RNG; |
72 }; | 72 }; |
73 | 73 |
74 /// RandomShuffle is an implementation of std::random_shuffle() that | 74 /// RandomShuffle is an implementation of std::random_shuffle() that doesn't |
75 /// doesn't change across stdlib implementations. Adapted from a | 75 /// change across stdlib implementations. Adapted from a sample implementation |
76 /// sample implementation at cppreference.com. | 76 /// at cppreference.com. |
77 template <class RandomIt, class RandomFunc> | 77 template <class RandomIt, class RandomFunc> |
78 void RandomShuffle(RandomIt First, RandomIt Last, RandomFunc &&RNG) { | 78 void RandomShuffle(RandomIt First, RandomIt Last, RandomFunc &&RNG) { |
79 for (auto i = Last - First - 1; i > 0; --i) | 79 for (auto i = Last - First - 1; i > 0; --i) |
80 std::swap(First[i], First[RNG(i + 1)]); | 80 std::swap(First[i], First[RNG(i + 1)]); |
81 } | 81 } |
82 | 82 |
83 } // end of namespace Ice | 83 } // end of namespace Ice |
84 | 84 |
85 #endif // SUBZERO_SRC_ICERNG_H | 85 #endif // SUBZERO_SRC_ICERNG_H |
OLD | NEW |