1/* 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 30 */ 31 32#ifndef __BASE_RANDOM_HH__ 33#define __BASE_RANDOM_HH__ 34
|
35#include <ios> 36#include <string> 37 38#include "base/range.hh" |
39#include "sim/host.hh" 40
|
37uint32_t getUInt32();
38double getDouble();
39double m5random(double r);
40uint64_t getUniformPos(uint64_t min, uint64_t max);
41int64_t getUniform(int64_t min, int64_t max);
|
41class Checkpoint; |
42
|
43template <typename T>
44struct Random;
45
46template<> struct Random<int8_t>
|
43class Random |
44{
|
48 static int8_t get()
49 { return getUInt32() & (int8_t)-1; }
|
45 protected: 46 static const int N = 624; 47 static const int M = 397; 48 static const uint32_t MATRIX_A = (uint32_t)0x9908b0df; 49 static const uint32_t UPPER_MASK = (uint32_t)0x80000000; 50 static const uint32_t LOWER_MASK = (uint32_t)0x7fffffff; |
51
|
51 static int8_t uniform(int8_t min, int8_t max)
52 { return getUniform(min, max); }
53};
|
52 uint32_t mt[N]; 53 int mti; |
54
|
55template<> struct Random<uint8_t>
56{
57 static uint8_t get()
58 { return getUInt32() & (uint8_t)-1; }
|
55 uint32_t genrand(); 56 uint32_t genrand(uint32_t max); 57 uint64_t genrand(uint64_t max); |
58
|
60 static uint8_t uniform(uint8_t min, uint8_t max)
61 { return getUniformPos(min, max); }
62};
|
59 void 60 _random(int8_t &value) 61 { 62 value = genrand() & (int8_t)-1; 63 } |
64
|
64template<> struct Random<int16_t>
65{
66 static int16_t get()
67 { return getUInt32() & (int16_t)-1; }
|
65 void 66 _random(int16_t &value) 67 { 68 value = genrand() & (int16_t)-1; 69 } |
70
|
69 static int16_t uniform(int16_t min, int16_t max)
70 { return getUniform(min, max); }
71};
|
71 void 72 _random(int32_t &value) 73 { 74 value = (int32_t)genrand(); 75 } |
76
|
73template<> struct Random<uint16_t>
74{
75 static uint16_t get()
76 { return getUInt32() & (uint16_t)-1; }
|
77 void 78 _random(int64_t &value) 79 { 80 value = (int64_t)genrand() << 32 | (int64_t)genrand(); 81 } |
82
|
78 static uint16_t uniform(uint16_t min, uint16_t max)
79 { return getUniformPos(min, max); }
80};
|
83 void 84 _random(uint8_t &value) 85 { 86 value = genrand() & (uint8_t)-1; 87 } |
88
|
82template<> struct Random<int32_t>
83{
84 static int32_t get()
85 { return (int32_t)getUInt32(); }
|
89 void 90 _random(uint16_t &value) 91 { 92 value = genrand() & (uint16_t)-1; 93 } |
94
|
87 static int32_t uniform(int32_t min, int32_t max)
88 { return getUniform(min, max); }
89};
|
95 void 96 _random(uint32_t &value) 97 { 98 value = genrand(); 99 } |
100
|
91template<> struct Random<uint32_t>
92{
93 static uint32_t get()
94 { return (uint32_t)getUInt32(); }
|
101 void 102 _random(uint64_t &value) 103 { 104 value = (uint64_t)genrand() << 32 | (uint64_t)genrand(); 105 } |
106
|
96 static uint32_t uniform(uint32_t min, uint32_t max)
97 { return getUniformPos(min, max); }
98};
|
107 // [0,1] 108 void 109 _random(float &value) 110 { 111 // ieee floats have 23 bits of mantissa 112 value = (genrand() >> 9) / 8388608.0; 113 } |
114
|
100template<> struct Random<int64_t>
101{
102 static int64_t get()
103 { return (int64_t)getUInt32() << 32 || (uint64_t)getUInt32(); }
|
115 // [0,1] 116 void 117 _random(double &value) 118 { 119 double number = genrand() * 2097152.0 + (genrand() >> 11); 120 value = number / 9007199254740992.0; 121 } |
122
|
105 static int64_t uniform(int64_t min, int64_t max)
106 { return getUniform(min, max); }
107};
|
123
|
109template<> struct Random<uint64_t>
110{
111 static uint64_t get()
112 { return (uint64_t)getUInt32() << 32 || (uint64_t)getUInt32(); }
|
124 // Range based versions of the random number generator 125 int8_t 126 _random(int8_t min, int8_t max) 127 { 128 uint32_t diff = max - min; 129 return static_cast<int8_t>(min + genrand(diff)); 130 } |
131
|
114 static uint64_t uniform(uint64_t min, uint64_t max)
115 { return getUniformPos(min, max); }
116};
|
132 int16_t 133 _random(int16_t min, int16_t max) 134 { 135 uint32_t diff = max - min; 136 return static_cast<int16_t>(min + genrand(diff)); 137 } |
138
|
118template<> struct Random<float>
119{
120 static float get()
121 { return getDouble(); }
122};
|
139 int32_t 140 _random(int32_t min, int32_t max) 141 { 142 uint32_t diff = max - min; 143 return static_cast<int32_t>(min + genrand(diff)); 144 } |
145
|
124template<> struct Random<double>
125{
126 static double get()
127 { return getDouble(); }
|
146 int64_t 147 _random(int64_t min, int64_t max) 148 { 149 uint64_t diff = max - min; 150 return static_cast<int64_t>(min + genrand(diff)); 151 } 152 153 uint8_t 154 _random(uint8_t min, uint8_t max) 155 { 156 uint32_t diff = max - min; 157 return static_cast<uint8_t>(min + genrand(diff)); 158 } 159 160 uint16_t 161 _random(uint16_t min, uint16_t max) 162 { 163 uint32_t diff = max - min; 164 return static_cast<uint16_t>(min + genrand(diff)); 165 } 166 167 uint32_t 168 _random(uint32_t min, uint32_t max) 169 { 170 uint32_t diff = max - min; 171 return static_cast<uint32_t>(min + genrand(diff)); 172 } 173 174 uint64_t 175 _random(uint64_t min, uint64_t max) 176 { 177 uint64_t diff = max - min; 178 return static_cast<uint64_t>(min + genrand(diff)); 179 } 180 181 public: 182 Random(); 183 Random(uint32_t s); 184 Random(uint32_t init_key[], int key_length); 185 ~Random(); 186 187 void init(uint32_t s); 188 void init(uint32_t init_key[], int key_length); 189 190 template <typename T> 191 T 192 random() 193 { 194 T value; 195 _random(value); 196 return value; 197 } 198 199 template <typename T> 200 T 201 random(T min, T max) 202 { 203 return _random(min, max); 204 } 205 206 template <typename T> 207 T 208 random(const Range<T> &range) 209 { 210 return _random(range.start, range.end); 211 } 212 213 // [0,1] 214 double 215 gen_real1() 216 { 217 return genrand() / 4294967296.0; 218 } 219 220 // [0,1) 221 double 222 gen_real2() 223 { 224 return genrand() / 4294967295.0; 225 } 226 227 // (0,1) 228 double 229 gen_real3() 230 { 231 return ((double)genrand() + 0.5) / 4294967296.0; 232 } 233 234 public: 235 void serialize(const std::string &base, std::ostream &os); 236 void unserialize(const std::string &base, Checkpoint *cp, 237 const std::string §ion); |
238}; 239
|
240extern Random random_mt; 241 |
242#endif // __BASE_RANDOM_HH__
|