Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Side by Side Diff: src/IceRNG.h

Issue 1300993002: Use separate random number generator for each randomization pass (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Minor change in GlobalContext::getJumpTables(), make jump tables in deterministic order even pooled… Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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, randomization pass ID
34 /// 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 /// The global seed is 64 bits; since it is likely to originate from the
40 /// system time, the lower bits are more "valuable" than the upper bits. As
41 /// such, we merge the randomization pass ID and the salt into the global seed
42 /// by xor'ing them into high bit ranges. We expect the pass ID to fit within
43 /// 4 bits, so it gets shifted by 60 to merge into the upper 4 bits. We expect
44 /// the salt (usually the function sequence number) to fit within 12 bits, so
45 /// it gets shifted by 48 before merging.
46 explicit RandomNumberGenerator(uint64_t Seed,
47 RandomizationPassesEnum RandomizationPassID,
48 uint64_t Salt = 0);
31 uint64_t next(uint64_t Max); 49 uint64_t next(uint64_t Max);
32 50
33 private: 51 private:
34 uint64_t State; 52 uint64_t State;
35 }; 53 };
36 54
37 /// This class adds additional random number generator utilities. The 55 /// This class adds additional random number generator utilities. The
38 /// reason for the wrapper class is that we want to keep the 56 /// reason for the wrapper class is that we want to keep the
39 /// RandomNumberGenerator interface identical to LLVM's. 57 /// RandomNumberGenerator interface identical to LLVM's.
40 class RandomNumberGeneratorWrapper { 58 class RandomNumberGeneratorWrapper {
(...skipping 17 matching lines...) Expand all
58 /// sample implementation at cppreference.com. 76 /// sample implementation at cppreference.com.
59 template <class RandomIt, class RandomFunc> 77 template <class RandomIt, class RandomFunc>
60 void RandomShuffle(RandomIt First, RandomIt Last, RandomFunc &&RNG) { 78 void RandomShuffle(RandomIt First, RandomIt Last, RandomFunc &&RNG) {
61 for (auto i = Last - First - 1; i > 0; --i) 79 for (auto i = Last - First - 1; i > 0; --i)
62 std::swap(First[i], First[RNG(i + 1)]); 80 std::swap(First[i], First[RNG(i + 1)]);
63 } 81 }
64 82
65 } // end of namespace Ice 83 } // end of namespace Ice
66 84
67 #endif // SUBZERO_SRC_ICERNG_H 85 #endif // SUBZERO_SRC_ICERNG_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698