random.hh (3483:edede8473667) | random.hh (4045:43eb54e807d1) |
---|---|
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; --- 20 unchanged lines hidden (view full) --- 29 * Ali Saidi 30 */ 31 32#ifndef __BASE_RANDOM_HH__ 33#define __BASE_RANDOM_HH__ 34 35#include "sim/host.hh" 36 | 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; --- 20 unchanged lines hidden (view full) --- 29 * Ali Saidi 30 */ 31 32#ifndef __BASE_RANDOM_HH__ 33#define __BASE_RANDOM_HH__ 34 35#include "sim/host.hh" 36 |
37long getLong(); | 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); 42 43template <typename T> 44struct Random; 45 46template<> struct Random<int8_t> 47{ 48 static int8_t get() | 38double getDouble(); 39double m5random(double r); 40uint64_t getUniformPos(uint64_t min, uint64_t max); 41int64_t getUniform(int64_t min, int64_t max); 42 43template <typename T> 44struct Random; 45 46template<> struct Random<int8_t> 47{ 48 static int8_t get() |
49 { return getLong() & (int8_t)-1; } | 49 { return getUInt32() & (int8_t)-1; } |
50 51 static int8_t uniform(int8_t min, int8_t max) 52 { return getUniform(min, max); } 53}; 54 55template<> struct Random<uint8_t> 56{ 57 static uint8_t get() | 50 51 static int8_t uniform(int8_t min, int8_t max) 52 { return getUniform(min, max); } 53}; 54 55template<> struct Random<uint8_t> 56{ 57 static uint8_t get() |
58 { return getLong() & (uint8_t)-1; } | 58 { return getUInt32() & (uint8_t)-1; } |
59 60 static uint8_t uniform(uint8_t min, uint8_t max) 61 { return getUniformPos(min, max); } 62}; 63 64template<> struct Random<int16_t> 65{ 66 static int16_t get() | 59 60 static uint8_t uniform(uint8_t min, uint8_t max) 61 { return getUniformPos(min, max); } 62}; 63 64template<> struct Random<int16_t> 65{ 66 static int16_t get() |
67 { return getLong() & (int16_t)-1; } | 67 { return getUInt32() & (int16_t)-1; } |
68 69 static int16_t uniform(int16_t min, int16_t max) 70 { return getUniform(min, max); } 71}; 72 73template<> struct Random<uint16_t> 74{ 75 static uint16_t get() | 68 69 static int16_t uniform(int16_t min, int16_t max) 70 { return getUniform(min, max); } 71}; 72 73template<> struct Random<uint16_t> 74{ 75 static uint16_t get() |
76 { return getLong() & (uint16_t)-1; } | 76 { return getUInt32() & (uint16_t)-1; } |
77 78 static uint16_t uniform(uint16_t min, uint16_t max) 79 { return getUniformPos(min, max); } 80}; 81 82template<> struct Random<int32_t> 83{ 84 static int32_t get() | 77 78 static uint16_t uniform(uint16_t min, uint16_t max) 79 { return getUniformPos(min, max); } 80}; 81 82template<> struct Random<int32_t> 83{ 84 static int32_t get() |
85 { return (int32_t)getLong(); } | 85 { return (int32_t)getUInt32(); } |
86 87 static int32_t uniform(int32_t min, int32_t max) 88 { return getUniform(min, max); } 89}; 90 91template<> struct Random<uint32_t> 92{ 93 static uint32_t get() | 86 87 static int32_t uniform(int32_t min, int32_t max) 88 { return getUniform(min, max); } 89}; 90 91template<> struct Random<uint32_t> 92{ 93 static uint32_t get() |
94 { return (uint32_t)getLong(); } | 94 { return (uint32_t)getUInt32(); } |
95 96 static uint32_t uniform(uint32_t min, uint32_t max) 97 { return getUniformPos(min, max); } 98}; 99 100template<> struct Random<int64_t> 101{ 102 static int64_t get() | 95 96 static uint32_t uniform(uint32_t min, uint32_t max) 97 { return getUniformPos(min, max); } 98}; 99 100template<> struct Random<int64_t> 101{ 102 static int64_t get() |
103 { return (int64_t)getLong() << 32 || (uint64_t)getLong(); } | 103 { return (int64_t)getUInt32() << 32 || (uint64_t)getUInt32(); } |
104 105 static int64_t uniform(int64_t min, int64_t max) 106 { return getUniform(min, max); } 107}; 108 109template<> struct Random<uint64_t> 110{ 111 static uint64_t get() | 104 105 static int64_t uniform(int64_t min, int64_t max) 106 { return getUniform(min, max); } 107}; 108 109template<> struct Random<uint64_t> 110{ 111 static uint64_t get() |
112 { return (uint64_t)getLong() << 32 || (uint64_t)getLong(); } | 112 { return (uint64_t)getUInt32() << 32 || (uint64_t)getUInt32(); } |
113 114 static uint64_t uniform(uint64_t min, uint64_t max) 115 { return getUniformPos(min, max); } 116}; 117 118template<> struct Random<float> 119{ 120 static float get() 121 { return getDouble(); } 122}; 123 124template<> struct Random<double> 125{ 126 static double get() 127 { return getDouble(); } 128}; 129 130#endif // __BASE_RANDOM_HH__ | 113 114 static uint64_t uniform(uint64_t min, uint64_t max) 115 { return getUniformPos(min, max); } 116}; 117 118template<> struct Random<float> 119{ 120 static float get() 121 { return getDouble(); } 122}; 123 124template<> struct Random<double> 125{ 126 static double get() 127 { return getDouble(); } 128}; 129 130#endif // __BASE_RANDOM_HH__ |