Deleted Added
sdiff udiff text old ( 9235:5aa4896ed55a ) new ( 10349:939094c17866 )
full compact
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

--- 12 unchanged lines hidden (view full) ---

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/*
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.
37 */
38
39#ifndef __BASE_RANDOM_HH__
40#define __BASE_RANDOM_HH__
41
42#include <ios>
43#include <string>
44
45#include "base/types.hh"
46
47class Checkpoint;
48
49class Random
50{
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
58 uint32_t mt[N];
59 int mti;
60
61 uint32_t genrand();
62 uint32_t genrand(uint32_t max);
63 uint64_t genrand(uint64_t max);
64
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:
188 Random();
189 Random(uint32_t s);
190 Random(uint32_t init_key[], int key_length);
191 ~Random();
192
193 void init(uint32_t s);
194 void init(uint32_t init_key[], int key_length);
195
196 template <typename T>
197 T
198 random()
199 {
200 T value;
201 _random(value);
202 return value;
203 }
204
205 template <typename T>
206 T
207 random(T min, T max)
208 {
209 return _random(min, max);
210 }
211
212 // [0,1]
213 double
214 gen_real1()
215 {
216 return genrand() / 4294967296.0;
217 }
218
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 &section);
237};
238
239extern Random random_mt;
240
241#endif // __BASE_RANDOM_HH__