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 // This file implements the random number generator. | 10 // This file implements the random number generator. |
| 11 // | 11 // |
| 12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
| 13 | 13 |
| 14 #include "IceRNG.h" | |
| 15 | |
| 14 #include <time.h> | 16 #include <time.h> |
| 15 | 17 |
| 16 #include "IceRNG.h" | |
| 17 | |
| 18 namespace Ice { | 18 namespace Ice { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 const unsigned MAX = 2147483647; | 21 const unsigned MAX = 2147483647; |
|
Jim Stichnoth
2015/06/24 21:35:33
constexpr if you feel like fixing this... (just ca
John
2015/06/25 17:13:12
Done.
| |
| 22 } // end of anonymous namespace | 22 } // end of anonymous namespace |
| 23 | 23 |
| 24 // TODO(wala,stichnot): Switch to RNG implementation from LLVM or C++11. | 24 // TODO(wala,stichnot): Switch to RNG implementation from LLVM or C++11. |
| 25 // | 25 // |
| 26 // TODO(wala,stichnot): Make it possible to replay the RNG sequence in a | 26 // TODO(wala,stichnot): Make it possible to replay the RNG sequence in a |
| 27 // subsequent run, for reproducing a bug. Print the seed in a comment | 27 // subsequent run, for reproducing a bug. Print the seed in a comment |
| 28 // in the asm output. Embed the seed in the binary via metadata that an | 28 // in the asm output. Embed the seed in the binary via metadata that an |
| 29 // attacker can't introspect. | 29 // attacker can't introspect. |
| 30 RandomNumberGenerator::RandomNumberGenerator(uint64_t Seed, llvm::StringRef) | 30 RandomNumberGenerator::RandomNumberGenerator(uint64_t Seed, llvm::StringRef) |
| 31 : State(Seed) {} | 31 : State(Seed) {} |
| 32 | 32 |
| 33 uint64_t RandomNumberGenerator::next(uint64_t Max) { | 33 uint64_t RandomNumberGenerator::next(uint64_t Max) { |
| 34 // Lewis, Goodman, and Miller (1969) | 34 // Lewis, Goodman, and Miller (1969) |
| 35 State = (16807 * State) % MAX; | 35 State = (16807 * State) % MAX; |
| 36 return State % Max; | 36 return State % Max; |
| 37 } | 37 } |
| 38 | 38 |
| 39 bool RandomNumberGeneratorWrapper::getTrueWithProbability(float Probability) { | 39 bool RandomNumberGeneratorWrapper::getTrueWithProbability(float Probability) { |
| 40 return RNG.next(MAX) < Probability * MAX; | 40 return RNG.next(MAX) < Probability * MAX; |
| 41 } | 41 } |
| 42 | 42 |
| 43 } // end of namespace Ice | 43 } // end of namespace Ice |
| OLD | NEW |