random.hh revision 5190
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 41class Checkpoint; 42 43class Random 44{ 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 52 uint32_t mt[N]; 53 int mti; 54 55 uint32_t genrand(); 56 uint32_t genrand(uint32_t max); 57 uint64_t genrand(uint64_t max); 58 59 void 60 _random(int8_t &value) 61 { 62 value = genrand() & (int8_t)-1; 63 } 64 65 void 66 _random(int16_t &value) 67 { 68 value = genrand() & (int16_t)-1; 69 } 70 71 void 72 _random(int32_t &value) 73 { 74 value = (int32_t)genrand(); 75 } 76 77 void 78 _random(int64_t &value) 79 { 80 value = (int64_t)genrand() << 32 | (int64_t)genrand(); 81 } 82 83 void 84 _random(uint8_t &value) 85 { 86 value = genrand() & (uint8_t)-1; 87 } 88 89 void 90 _random(uint16_t &value) 91 { 92 value = genrand() & (uint16_t)-1; 93 } 94 95 void 96 _random(uint32_t &value) 97 { 98 value = genrand(); 99 } 100 101 void 102 _random(uint64_t &value) 103 { 104 value = (uint64_t)genrand() << 32 | (uint64_t)genrand(); 105 } 106 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 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 123 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 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 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 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__ 243