random.hh revision 2665
12SN/A/*
21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292665Ssaidi@eecs.umich.edu *          Ali Saidi
302SN/A */
312SN/A
321296SN/A#ifndef __BASE_RANDOM_HH__
331296SN/A#define __BASE_RANDOM_HH__
342SN/A
3556SN/A#include "sim/host.hh"
362SN/A
372SN/Along getLong();
382SN/Adouble getDouble();
391971SN/Auint64_t getUniformPos(uint64_t min, uint64_t max);
401971SN/Aint64_t getUniform(int64_t min, int64_t max);
412SN/A
422SN/Atemplate <typename T>
432SN/Astruct Random;
442SN/A
451354SN/Atemplate<> struct Random<int8_t>
462SN/A{
472SN/A    static int8_t get()
482SN/A    { return getLong() & (int8_t)-1; }
491954SN/A
501971SN/A    static int8_t uniform(int8_t min, int8_t max)
511971SN/A    { return getUniform(min, max); }
522SN/A};
532SN/A
541354SN/Atemplate<> struct Random<uint8_t>
552SN/A{
561296SN/A    static uint8_t get()
572SN/A    { return getLong() & (uint8_t)-1; }
581954SN/A
591971SN/A    static uint8_t uniform(uint8_t min, uint8_t max)
601971SN/A    { return getUniformPos(min, max); }
612SN/A};
622SN/A
631354SN/Atemplate<> struct Random<int16_t>
642SN/A{
651296SN/A    static int16_t get()
662SN/A    { return getLong() & (int16_t)-1; }
671954SN/A
681971SN/A    static int16_t uniform(int16_t min, int16_t max)
691971SN/A    { return getUniform(min, max); }
702SN/A};
712SN/A
721354SN/Atemplate<> struct Random<uint16_t>
732SN/A{
741296SN/A    static uint16_t get()
752SN/A    { return getLong() & (uint16_t)-1; }
761954SN/A
771971SN/A    static uint16_t uniform(uint16_t min, uint16_t max)
781971SN/A    { return getUniformPos(min, max); }
792SN/A};
802SN/A
811354SN/Atemplate<> struct Random<int32_t>
822SN/A{
831296SN/A    static int32_t get()
842SN/A    { return (int32_t)getLong(); }
851954SN/A
861971SN/A    static int32_t uniform(int32_t min, int32_t max)
871971SN/A    { return getUniform(min, max); }
882SN/A};
892SN/A
901354SN/Atemplate<> struct Random<uint32_t>
912SN/A{
921296SN/A    static uint32_t get()
932SN/A    { return (uint32_t)getLong(); }
941954SN/A
951971SN/A    static uint32_t uniform(uint32_t min, uint32_t max)
961971SN/A    { return getUniformPos(min, max); }
972SN/A};
982SN/A
991354SN/Atemplate<> struct Random<int64_t>
1002SN/A{
1011296SN/A    static int64_t get()
1022SN/A    { return (int64_t)getLong() << 32 || (uint64_t)getLong(); }
1031954SN/A
1041971SN/A    static int64_t uniform(int64_t min, int64_t max)
1051971SN/A    { return getUniform(min, max); }
1062SN/A};
1072SN/A
1081354SN/Atemplate<> struct Random<uint64_t>
1092SN/A{
1101296SN/A    static uint64_t get()
1112SN/A    { return (uint64_t)getLong() << 32 || (uint64_t)getLong(); }
1121954SN/A
1131971SN/A    static uint64_t uniform(uint64_t min, uint64_t max)
1141971SN/A    { return getUniformPos(min, max); }
1152SN/A};
1162SN/A
1171354SN/Atemplate<> struct Random<float>
1182SN/A{
1191296SN/A    static float get()
1202SN/A    { return getDouble(); }
1212SN/A};
1222SN/A
1231354SN/Atemplate<> struct Random<double>
1242SN/A{
1251296SN/A    static double get()
1262SN/A    { return getDouble(); }
1272SN/A};
1282SN/A
1291296SN/A#endif // __BASE_RANDOM_HH__
130