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

Side by Side Diff: src/IceRNG.cpp

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: 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.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 13 matching lines...) Expand all
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
34 RandomNumberGenerator::RandomNumberGenerator(
35 uint64_t Seed, RandomizationPassesEnum RandomizationPassID, uint64_t Salt) {
36 constexpr unsigned NumBitsGlobalSeed = CHAR_BIT * sizeof(State);
37 constexpr unsigned NumBitsPassID = 4;
38 constexpr unsigned NumBitsSalt = 12;
39 static_assert(RPE_num < (1 << NumBitsPassID), "NumBitsPassID too small");
40 State = Seed ^ ((uint64_t)RandomizationPassID
41 << (NumBitsGlobalSeed - NumBitsPassID)) ^
42 (Salt << (NumBitsGlobalSeed - NumBitsPassID - NumBitsSalt));
43 }
34 uint64_t RandomNumberGenerator::next(uint64_t Max) { 44 uint64_t RandomNumberGenerator::next(uint64_t Max) {
35 // Lewis, Goodman, and Miller (1969) 45 // Lewis, Goodman, and Miller (1969)
36 State = (16807 * State) % MAX; 46 State = (16807 * State) % MAX;
37 return State % Max; 47 return State % Max;
38 } 48 }
39 49
40 bool RandomNumberGeneratorWrapper::getTrueWithProbability(float Probability) { 50 bool RandomNumberGeneratorWrapper::getTrueWithProbability(float Probability) {
41 return RNG.next(MAX) < Probability * MAX; 51 return RNG.next(MAX) < Probability * MAX;
42 } 52 }
43 53
44 } // end of namespace Ice 54 } // end of namespace Ice
OLDNEW
« no previous file with comments | « src/IceRNG.h ('k') | src/IceRegAlloc.cpp » ('j') | src/IceRegAlloc.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698