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 <time.h> | 14 #include <time.h> |
15 | 15 |
16 #include "llvm/Support/CommandLine.h" | |
17 | |
18 #include "IceRNG.h" | 16 #include "IceRNG.h" |
19 | 17 |
20 namespace Ice { | 18 namespace Ice { |
21 | 19 |
22 namespace { | 20 namespace { |
23 namespace cl = llvm::cl; | |
24 | |
25 // TODO(stichnot): See if we can easily use LLVM's -rng-seed option | |
26 // and implementation. I expect the implementation is different and | |
27 // therefore the tests would need to be changed. | |
28 cl::opt<unsigned long long> | |
29 RandomSeed("sz-seed", cl::desc("Seed the random number generator"), | |
30 cl::init(time(0))); | |
31 | |
32 const unsigned MAX = 2147483647; | 21 const unsigned MAX = 2147483647; |
33 | |
34 } // end of anonymous namespace | 22 } // end of anonymous namespace |
35 | 23 |
36 // 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. |
37 // | 25 // |
38 // 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 |
39 // 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 |
40 // 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 |
41 // attacker can't introspect. | 29 // attacker can't introspect. |
42 RandomNumberGenerator::RandomNumberGenerator(llvm::StringRef) | 30 RandomNumberGenerator::RandomNumberGenerator(uint64_t Seed, llvm::StringRef) |
43 : State(RandomSeed) {} | 31 : State(Seed) {} |
44 | 32 |
45 uint64_t RandomNumberGenerator::next(uint64_t Max) { | 33 uint64_t RandomNumberGenerator::next(uint64_t Max) { |
46 // Lewis, Goodman, and Miller (1969) | 34 // Lewis, Goodman, and Miller (1969) |
47 State = (16807 * State) % MAX; | 35 State = (16807 * State) % MAX; |
48 return State % Max; | 36 return State % Max; |
49 } | 37 } |
50 | 38 |
51 bool RandomNumberGeneratorWrapper::getTrueWithProbability(float Probability) { | 39 bool RandomNumberGeneratorWrapper::getTrueWithProbability(float Probability) { |
52 return RNG.next(MAX) < Probability * MAX; | 40 return RNG.next(MAX) < Probability * MAX; |
53 } | 41 } |
54 | 42 |
55 } // end of namespace Ice | 43 } // end of namespace Ice |
OLD | NEW |