Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/src/IceRNG.cpp - PRNG implementation -----------------------===// | 1 //===- subzero/src/IceRNG.cpp - PRNG implementation -----------------------===// |
| 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 12 matching lines...) Expand all Loading... | |
| 23 } // end of anonymous namespace | 23 } // end of anonymous namespace |
| 24 | 24 |
| 25 // TODO(wala,stichnot): Switch to RNG implementation from LLVM or C++11. | 25 // TODO(wala,stichnot): Switch to RNG implementation from LLVM or C++11. |
| 26 // | 26 // |
| 27 // TODO(wala,stichnot): Make it possible to replay the RNG sequence in a | 27 // TODO(wala,stichnot): Make it possible to replay the RNG sequence in a |
| 28 // subsequent run, for reproducing a bug. Print the seed in a comment | 28 // subsequent run, for reproducing a bug. Print the seed in a comment |
| 29 // in the asm output. Embed the seed in the binary via metadata that an | 29 // in the asm output. Embed the seed in the binary via metadata that an |
| 30 // attacker can't introspect. | 30 // attacker can't introspect. |
| 31 RandomNumberGenerator::RandomNumberGenerator(uint64_t Seed, llvm::StringRef) | 31 RandomNumberGenerator::RandomNumberGenerator(uint64_t Seed, llvm::StringRef) |
| 32 : State(Seed) {} | 32 : State(Seed) {} |
| 33 | 33 /// Create a random number generator with: global seed, |
|
Jim Stichnoth
2015/08/19 19:03:04
The same comment/docstring appears in IceRNG.h and
qining
2015/08/19 23:25:49
Done. Removed the one in .cpp.
| |
| 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 RNG. | |
| 38 /// Final seed = Seed ^ Salt << 16 ^ RandomizationPassID << 28 | |
|
Jim Stichnoth
2015/08/19 19:03:04
I'd like to see an explanation of these magic shif
qining
2015/08/19 23:25:49
Done.
| |
| 39 RandomNumberGenerator::RandomNumberGenerator( | |
| 40 uint64_t Seed, RandomizationPassesEnum RandomizationPassID, uint64_t Salt) | |
| 41 : State(Seed ^ Salt << 16 ^ RandomizationPassID << 28) { | |
|
Jim Stichnoth
2015/08/19 19:03:04
Instead of 16 and 28 (or 48 and 60 in the proposed
qining
2015/08/19 23:25:49
It seems I have to put these constexpr and the ass
| |
| 42 assert(RandomizationPassID < 0x10 && "RandomizationPassID requires more than " | |
|
Jim Stichnoth
2015/08/19 19:03:04
Use a static_assert instead:
static_assert(blah
qining
2015/08/19 23:25:49
Done.
| |
| 43 "4 bits, left shifting 28 bits causes " | |
| 44 "overflow"); | |
| 45 } | |
| 34 uint64_t RandomNumberGenerator::next(uint64_t Max) { | 46 uint64_t RandomNumberGenerator::next(uint64_t Max) { |
| 35 // Lewis, Goodman, and Miller (1969) | 47 // Lewis, Goodman, and Miller (1969) |
| 36 State = (16807 * State) % MAX; | 48 State = (16807 * State) % MAX; |
| 37 return State % Max; | 49 return State % Max; |
| 38 } | 50 } |
| 39 | 51 |
| 40 bool RandomNumberGeneratorWrapper::getTrueWithProbability(float Probability) { | 52 bool RandomNumberGeneratorWrapper::getTrueWithProbability(float Probability) { |
| 41 return RNG.next(MAX) < Probability * MAX; | 53 return RNG.next(MAX) < Probability * MAX; |
| 42 } | 54 } |
| 43 | 55 |
| 44 } // end of namespace Ice | 56 } // end of namespace Ice |
| OLD | NEW |