random.cc revision 3483
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
322SN/A#include <cstdlib>
332SN/A#include <cmath>
342SN/A
353483Ssaidi@eecs.umich.edu#if defined(__sun__)
363483Ssaidi@eecs.umich.edu#include <ieeefp.h>
373483Ssaidi@eecs.umich.edu#endif
383483Ssaidi@eecs.umich.edu
3956SN/A#include "sim/param.hh"
4056SN/A#include "base/random.hh"
411954SN/A#include "base/trace.hh"
422SN/A
432SN/Ausing namespace std;
442SN/A
452SN/Aclass RandomContext : public ParamContext
462SN/A{
472SN/A  public:
482SN/A    RandomContext(const string &_iniSection)
492SN/A        : ::ParamContext(_iniSection) {}
501296SN/A    ~RandomContext() {}
512SN/A
522SN/A    void checkParams();
532SN/A};
542SN/A
552SN/ARandomContext paramContext("random");
562SN/A
572SN/AParam<unsigned>
582SN/Aseed(&paramContext, "seed", "seed to random number generator", 1);
592SN/A
602SN/Avoid
612SN/ARandomContext::checkParams()
622SN/A{
631954SN/A    ::srand48(seed);
642SN/A}
652SN/A
662SN/Along
672SN/AgetLong()
682SN/A{
691954SN/A    return mrand48();
702SN/A}
712SN/A
723483Ssaidi@eecs.umich.edudouble
733483Ssaidi@eecs.umich.edum5round(double r)
743483Ssaidi@eecs.umich.edu{
753483Ssaidi@eecs.umich.edu#if defined(__sun__)
763483Ssaidi@eecs.umich.edu    double val;
773483Ssaidi@eecs.umich.edu    fp_rnd oldrnd = fpsetround(FP_RN);
783483Ssaidi@eecs.umich.edu    val = rint(r);
793483Ssaidi@eecs.umich.edu    fpsetround(oldrnd);
803483Ssaidi@eecs.umich.edu    return val;
813483Ssaidi@eecs.umich.edu#else
823483Ssaidi@eecs.umich.edu    return round(r);
833483Ssaidi@eecs.umich.edu#endif
843483Ssaidi@eecs.umich.edu}
853483Ssaidi@eecs.umich.edu
861954SN/Aint64_t
871971SN/AgetUniform(int64_t min, int64_t max)
881954SN/A{
891954SN/A    double r;
901971SN/A    r = drand48() * (max-min) + min;
913483Ssaidi@eecs.umich.edu
923483Ssaidi@eecs.umich.edu    return (int64_t)m5round(r);
931954SN/A}
941954SN/A
951954SN/Auint64_t
961971SN/AgetUniformPos(uint64_t min, uint64_t max)
971954SN/A{
981954SN/A    double r;
991971SN/A    r = drand48() * (max-min) + min;
1003483Ssaidi@eecs.umich.edu
1013483Ssaidi@eecs.umich.edu    return (uint64_t)m5round(r);
1021954SN/A}
1031954SN/A
1041954SN/A
1052SN/A// idea for generating a double from erand48
1062SN/Adouble
1072SN/AgetDouble()
1082SN/A{
1092SN/A    union {
1102SN/A        uint32_t _long[2];
1112SN/A        uint16_t _short[4];
1122SN/A    };
1132SN/A
1141954SN/A    _long[0] = mrand48();
1151954SN/A    _long[1] = mrand48();
1162SN/A
1172SN/A    return ldexp((double) _short[0], -48) +
1182SN/A        ldexp((double) _short[1], -32) +
1192SN/A        ldexp((double) _short[2], -16);
1202SN/A}
121