1/*
| 1/*
|
| 2 * Copyright (c) 2014 ARM Limited 3 * All rights reserved 4 * 5 * The license below extends only to copyright in the software and shall 6 * not be construed as granting a license to any other intellectual 7 * property including but not limited to intellectual property relating 8 * to a hardware implementation of the functionality of the software 9 * licensed hereunder. You may use the software subject to the license 10 * terms below provided that you ensure that this notice is replicated 11 * unmodified and in its entirety in all distributions of the software, 12 * modified or unmodified, in source code or in binary form. 13 *
|
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * 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 * Authors: Nathan Binkert 29 * Ali Saidi
| 14 * Copyright (c) 2003-2005 The Regents of The University of Michigan 15 * All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions are 19 * met: redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer; 21 * redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution; 24 * neither the name of the copyright holders nor the names of its 25 * contributors may be used to endorse or promote products derived from 26 * this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 * 40 * Authors: Nathan Binkert 41 * Ali Saidi
|
| 42 * Andreas Hansson
|
30 */ 31 32/*
| 43 */ 44 45/*
|
33 * Mersenne Twister random number generator has a period of 34 * 2^19937-1. 35 * 36 * The actual math is in its own file to keep the license clear.
| 46 * Mersenne twister random number generator.
|
37 */ 38 39#ifndef __BASE_RANDOM_HH__ 40#define __BASE_RANDOM_HH__ 41
| 47 */ 48 49#ifndef __BASE_RANDOM_HH__ 50#define __BASE_RANDOM_HH__ 51
|
42#include <ios>
| 52#include <random>
|
43#include <string>
| 53#include <string>
|
| 54#include <type_traits>
|
44 45#include "base/types.hh" 46 47class Checkpoint; 48 49class Random 50{
| 55 56#include "base/types.hh" 57 58class Checkpoint; 59 60class Random 61{
|
51 protected: 52 static const int N = 624; 53 static const int M = 397; 54 static const uint32_t MATRIX_A = (uint32_t)0x9908b0df; 55 static const uint32_t UPPER_MASK = (uint32_t)0x80000000; 56 static const uint32_t LOWER_MASK = (uint32_t)0x7fffffff;
| |
57
| 62
|
58 uint32_t mt[N]; 59 int mti;
| 63 private:
|
60
| 64
|
61 uint32_t genrand(); 62 uint32_t genrand(uint32_t max); 63 uint64_t genrand(uint64_t max);
| 65 std::mt19937_64 gen;
|
64
| 66
|
65 void 66 _random(int8_t &value) 67 { 68 value = genrand() & (int8_t)-1; 69 } 70 71 void 72 _random(int16_t &value) 73 { 74 value = genrand() & (int16_t)-1; 75 } 76 77 void 78 _random(int32_t &value) 79 { 80 value = (int32_t)genrand(); 81 } 82 83 void 84 _random(int64_t &value) 85 { 86 value = (int64_t)genrand() << 32 | (int64_t)genrand(); 87 } 88 89 void 90 _random(uint8_t &value) 91 { 92 value = genrand() & (uint8_t)-1; 93 } 94 95 void 96 _random(uint16_t &value) 97 { 98 value = genrand() & (uint16_t)-1; 99 } 100 101 void 102 _random(uint32_t &value) 103 { 104 value = genrand(); 105 } 106 107 void 108 _random(uint64_t &value) 109 { 110 value = (uint64_t)genrand() << 32 | (uint64_t)genrand(); 111 } 112 113 // [0,1] 114 void 115 _random(float &value) 116 { 117 // ieee floats have 23 bits of mantissa 118 value = (genrand() >> 9) / 8388608.0; 119 } 120 121 // [0,1] 122 void 123 _random(double &value) 124 { 125 double number = genrand() * 2097152.0 + (genrand() >> 11); 126 value = number / 9007199254740992.0; 127 } 128 129 130 // Range based versions of the random number generator 131 int8_t 132 _random(int8_t min, int8_t max) 133 { 134 uint32_t diff = max - min; 135 return static_cast<int8_t>(min + genrand(diff)); 136 } 137 138 int16_t 139 _random(int16_t min, int16_t max) 140 { 141 uint32_t diff = max - min; 142 return static_cast<int16_t>(min + genrand(diff)); 143 } 144 145 int32_t 146 _random(int32_t min, int32_t max) 147 { 148 uint32_t diff = max - min; 149 return static_cast<int32_t>(min + genrand(diff)); 150 } 151 152 int64_t 153 _random(int64_t min, int64_t max) 154 { 155 uint64_t diff = max - min; 156 return static_cast<int64_t>(min + genrand(diff)); 157 } 158 159 uint8_t 160 _random(uint8_t min, uint8_t max) 161 { 162 uint32_t diff = max - min; 163 return static_cast<uint8_t>(min + genrand(diff)); 164 } 165 166 uint16_t 167 _random(uint16_t min, uint16_t max) 168 { 169 uint32_t diff = max - min; 170 return static_cast<uint16_t>(min + genrand(diff)); 171 } 172 173 uint32_t 174 _random(uint32_t min, uint32_t max) 175 { 176 uint32_t diff = max - min; 177 return static_cast<uint32_t>(min + genrand(diff)); 178 } 179 180 uint64_t 181 _random(uint64_t min, uint64_t max) 182 { 183 uint64_t diff = max - min; 184 return static_cast<uint64_t>(min + genrand(diff)); 185 } 186
| |
187 public:
| 67 public:
|
| 68
|
188 Random(); 189 Random(uint32_t s);
| 69 Random(); 70 Random(uint32_t s);
|
190 Random(uint32_t init_key[], int key_length);
| |
191 ~Random(); 192 193 void init(uint32_t s);
| 71 ~Random(); 72 73 void init(uint32_t s);
|
194 void init(uint32_t init_key[], int key_length);
| |
195
| 74
|
| 75 /** 76 * Use the SFINAE idiom to choose an implementation based on 77 * whether the type is integral or floating point. 78 */
|
196 template <typename T>
| 79 template <typename T>
|
197 T
| 80 typename std::enable_if<std::is_integral<T>::value, T>::type
|
198 random() 199 {
| 81 random() 82 {
|
200 T value; 201 _random(value); 202 return value;
| 83 // [0, max_value] for integer types 84 std::uniform_int_distribution<T> dist; 85 return dist(gen);
|
203 } 204 205 template <typename T>
| 86 } 87 88 template <typename T>
|
206 T 207 random(T min, T max)
| 89 typename std::enable_if<std::is_floating_point<T>::value, T>::type 90 random()
|
208 {
| 91 {
|
209 return _random(min, max);
| 92 // [0, 1) for real types 93 std::uniform_real_distribution<T> dist; 94 return dist(gen);
|
210 } 211
| 95 } 96
|
212 // [0,1] 213 double 214 gen_real1()
| 97 template <typename T> 98 typename std::enable_if<std::is_integral<T>::value, T>::type 99 random(T min, T max)
|
215 {
| 100 {
|
216 return genrand() / 4294967296.0;
| 101 std::uniform_int_distribution<T> dist(min, max); 102 return dist(gen);
|
217 } 218
| 103 } 104
|
219 // [0,1) 220 double 221 gen_real2() 222 { 223 return genrand() / 4294967295.0; 224 } 225 226 // (0,1) 227 double 228 gen_real3() 229 { 230 return ((double)genrand() + 0.5) / 4294967296.0; 231 } 232 233 public: 234 void serialize(const std::string &base, std::ostream &os); 235 void unserialize(const std::string &base, Checkpoint *cp, 236 const std::string §ion);
| 105 void serialize(std::ostream &os); 106 void unserialize(Checkpoint *cp, const std::string §ion);
|
237}; 238 239extern Random random_mt; 240 241#endif // __BASE_RANDOM_HH__
| 107}; 108 109extern Random random_mt; 110 111#endif // __BASE_RANDOM_HH__
|