OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #ifndef V8_UTILS_RANDOM_NUMBER_GENERATOR_H_ |
| 29 #define V8_UTILS_RANDOM_NUMBER_GENERATOR_H_ |
| 30 |
| 31 #include "globals.h" |
| 32 |
| 33 namespace v8 { |
| 34 namespace internal { |
| 35 |
| 36 // ----------------------------------------------------------------------------- |
| 37 // RandomNumberGenerator |
| 38 // |
| 39 // This class is used to generate a stream of pseudorandom numbers. The class |
| 40 // uses a 48-bit seed, which is modified using a linear congruential formula. |
| 41 // (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.) |
| 42 // If two instances of RandomNumberGenerator are created with the same seed, and |
| 43 // the same sequence of method calls is made for each, they will generate and |
| 44 // return identical sequences of numbers. |
| 45 // This class is neither reentrant nor threadsafe. |
| 46 |
| 47 class RandomNumberGenerator V8_FINAL { |
| 48 public: |
| 49 // EntropySource is used as a callback function when V8 needs a source of |
| 50 // entropy. |
| 51 typedef bool (*EntropySource)(unsigned char* buffer, size_t buflen); |
| 52 static void SetEntropySource(EntropySource entropy_source); |
| 53 |
| 54 RandomNumberGenerator(); |
| 55 explicit RandomNumberGenerator(int64_t seed) { SetSeed(seed); } |
| 56 |
| 57 // Returns the next pseudorandom, uniformly distributed int value from this |
| 58 // random number generator's sequence. The general contract of |NextInt()| is |
| 59 // that one int value is pseudorandomly generated and returned. |
| 60 // All 2^32 possible integer values are produced with (approximately) equal |
| 61 // probability. |
| 62 V8_INLINE(int NextInt()) V8_WARN_UNUSED_RESULT { |
| 63 return Next(32); |
| 64 } |
| 65 |
| 66 // Returns a pseudorandom, uniformly distributed int value between 0 |
| 67 // (inclusive) and the specified max value (exclusive), drawn from this random |
| 68 // number generator's sequence. The general contract of |NextInt(int)| is that |
| 69 // one int value in the specified range is pseudorandomly generated and |
| 70 // returned. All max possible int values are produced with (approximately) |
| 71 // equal probability. |
| 72 int NextInt(int max) V8_WARN_UNUSED_RESULT; |
| 73 |
| 74 // Returns the next pseudorandom, uniformly distributed boolean value from |
| 75 // this random number generator's sequence. The general contract of |
| 76 // |NextBoolean()| is that one boolean value is pseudorandomly generated and |
| 77 // returned. The values true and false are produced with (approximately) equal |
| 78 // probability. |
| 79 V8_INLINE(bool NextBool()) V8_WARN_UNUSED_RESULT { |
| 80 return Next(1) != 0; |
| 81 } |
| 82 |
| 83 // Returns the next pseudorandom, uniformly distributed double value between |
| 84 // 0.0 and 1.0 from this random number generator's sequence. |
| 85 // The general contract of |NextDouble()| is that one double value, chosen |
| 86 // (approximately) uniformly from the range 0.0 (inclusive) to 1.0 |
| 87 // (exclusive), is pseudorandomly generated and returned. |
| 88 double NextDouble() V8_WARN_UNUSED_RESULT; |
| 89 |
| 90 // Fills the elements of a specified array of bytes with random numbers. |
| 91 void NextBytes(void* buffer, size_t buflen); |
| 92 |
| 93 private: |
| 94 static const int64_t kMultiplier = V8_2PART_UINT64_C(0x5, deece66d); |
| 95 static const int64_t kAddend = 0xb; |
| 96 static const int64_t kMask = V8_2PART_UINT64_C(0xffff, ffffffff); |
| 97 |
| 98 int Next(int bits) V8_WARN_UNUSED_RESULT; |
| 99 void SetSeed(int64_t seed); |
| 100 |
| 101 int64_t seed_; |
| 102 }; |
| 103 |
| 104 } } // namespace v8::internal |
| 105 |
| 106 #endif // V8_UTILS_RANDOM_NUMBER_GENERATOR_H_ |
OLD | NEW |