random.cc revision 3918
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
323918Ssaidi@eecs.umich.edu#if defined(__sun)
333918Ssaidi@eecs.umich.edu#include <ieeefp.h>
343918Ssaidi@eecs.umich.edu#endif
353918Ssaidi@eecs.umich.edu#ifdef __SUNPRO_CC
363918Ssaidi@eecs.umich.edu#include <stdlib.h>
373918Ssaidi@eecs.umich.edu#include <math.h>
383918Ssaidi@eecs.umich.edu#endif
393918Ssaidi@eecs.umich.edu
402SN/A#include <cstdlib>
412SN/A#include <cmath>
422SN/A
433483Ssaidi@eecs.umich.edu
4456SN/A#include "sim/param.hh"
4556SN/A#include "base/random.hh"
461954SN/A#include "base/trace.hh"
472SN/A
482SN/Ausing namespace std;
492SN/A
502SN/Aclass RandomContext : public ParamContext
512SN/A{
522SN/A  public:
532SN/A    RandomContext(const string &_iniSection)
542SN/A        : ::ParamContext(_iniSection) {}
551296SN/A    ~RandomContext() {}
562SN/A
572SN/A    void checkParams();
582SN/A};
592SN/A
602SN/ARandomContext paramContext("random");
612SN/A
622SN/AParam<unsigned>
632SN/Aseed(&paramContext, "seed", "seed to random number generator", 1);
642SN/A
652SN/Avoid
662SN/ARandomContext::checkParams()
672SN/A{
681954SN/A    ::srand48(seed);
692SN/A}
702SN/A
712SN/Along
722SN/AgetLong()
732SN/A{
741954SN/A    return mrand48();
752SN/A}
762SN/A
773483Ssaidi@eecs.umich.edudouble
783483Ssaidi@eecs.umich.edum5round(double r)
793483Ssaidi@eecs.umich.edu{
803918Ssaidi@eecs.umich.edu#if defined(__sun)
813483Ssaidi@eecs.umich.edu    double val;
823483Ssaidi@eecs.umich.edu    fp_rnd oldrnd = fpsetround(FP_RN);
833483Ssaidi@eecs.umich.edu    val = rint(r);
843483Ssaidi@eecs.umich.edu    fpsetround(oldrnd);
853483Ssaidi@eecs.umich.edu    return val;
863483Ssaidi@eecs.umich.edu#else
873483Ssaidi@eecs.umich.edu    return round(r);
883483Ssaidi@eecs.umich.edu#endif
893483Ssaidi@eecs.umich.edu}
903483Ssaidi@eecs.umich.edu
911954SN/Aint64_t
921971SN/AgetUniform(int64_t min, int64_t max)
931954SN/A{
941954SN/A    double r;
951971SN/A    r = drand48() * (max-min) + min;
963483Ssaidi@eecs.umich.edu
973483Ssaidi@eecs.umich.edu    return (int64_t)m5round(r);
981954SN/A}
991954SN/A
1001954SN/Auint64_t
1011971SN/AgetUniformPos(uint64_t min, uint64_t max)
1021954SN/A{
1031954SN/A    double r;
1041971SN/A    r = drand48() * (max-min) + min;
1053483Ssaidi@eecs.umich.edu
1063483Ssaidi@eecs.umich.edu    return (uint64_t)m5round(r);
1071954SN/A}
1081954SN/A
1091954SN/A
1102SN/A// idea for generating a double from erand48
1112SN/Adouble
1122SN/AgetDouble()
1132SN/A{
1142SN/A    union {
1152SN/A        uint32_t _long[2];
1162SN/A        uint16_t _short[4];
1172SN/A    };
1182SN/A
1191954SN/A    _long[0] = mrand48();
1201954SN/A    _long[1] = mrand48();
1212SN/A
1222SN/A    return ldexp((double) _short[0], -48) +
1232SN/A        ldexp((double) _short[1], -32) +
1242SN/A        ldexp((double) _short[2], -16);
1252SN/A}
126