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

Side by Side Diff: src/IceRNG.cpp

Issue 463563006: Subzero: Randomly insert nops. (Closed) Base URL: https://gerrit.chromium.org/gerrit/p/native_client/pnacl-subzero.git@master
Patch Set: Fix insertion strategy Created 6 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
« no previous file with comments | « src/IceRNG.h ('k') | src/IceTargetLowering.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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" 16 #include "llvm/Support/CommandLine.h"
17 17
18 #include "IceRNG.h" 18 #include "IceRNG.h"
19 19
20 namespace Ice { 20 namespace Ice {
21 21
22 namespace { 22 namespace {
23 namespace cl = llvm::cl; 23 namespace cl = llvm::cl;
24 24
25 cl::opt<unsigned long long> 25 cl::opt<unsigned long long>
26 RandomSeed("rng-seed", cl::desc("Seed the random number generator"), 26 RandomSeed("rng-seed", cl::desc("Seed the random number generator"),
27 cl::init(time(0))); 27 cl::init(time(0)));
28 28
29 const unsigned MAX = 2147483647;
30
29 } // end of anonymous namespace 31 } // end of anonymous namespace
30 32
31 // TODO(wala,stichnot): Switch to RNG implementation from LLVM or C++11. 33 // TODO(wala,stichnot): Switch to RNG implementation from LLVM or C++11.
32 // 34 //
33 // TODO(wala,stichnot): Make it possible to replay the RNG sequence in a 35 // TODO(wala,stichnot): Make it possible to replay the RNG sequence in a
34 // subsequent run, for reproducing a bug. Print the seed in a comment 36 // subsequent run, for reproducing a bug. Print the seed in a comment
35 // in the asm output. Embed the seed in the binary via metadata that an 37 // in the asm output. Embed the seed in the binary via metadata that an
36 // attacker can't introspect. 38 // attacker can't introspect.
37 RandomNumberGenerator::RandomNumberGenerator(llvm::StringRef) 39 RandomNumberGenerator::RandomNumberGenerator(llvm::StringRef)
38 : State(RandomSeed) {} 40 : State(RandomSeed) {}
39 41
40 uint64_t RandomNumberGenerator::next(uint64_t Max) { 42 uint64_t RandomNumberGenerator::next(uint64_t Max) {
41 // Lewis, Goodman, and Miller (1969) 43 // Lewis, Goodman, and Miller (1969)
42 State = (16807 * State) % 2147483647; 44 State = (16807 * State) % MAX;
43 return State % Max; 45 return State % Max;
44 } 46 }
45 47
48 bool RandomNumberGeneratorWrapper::getTrueWithProbability(float Probability) {
49 return RNG.next(MAX) < Probability * MAX;
50 }
51
46 } // end of namespace Ice 52 } // end of namespace Ice
OLDNEW
« no previous file with comments | « src/IceRNG.h ('k') | src/IceTargetLowering.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698