random.hh revision 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;
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 "sim/host.hh"
36
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()
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()
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()
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()
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()
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()
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()
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()
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__
131