random.cc revision 1954
12SN/A/* 22188SN/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. 272665SN/A */ 282665SN/A 292665SN/A#include <cstdlib> 302665SN/A#include <cmath> 312665SN/A 322SN/A#include "sim/param.hh" 332SN/A#include "base/random.hh" 342SN/A#include "base/trace.hh" 352SN/A 362465SN/Ausing namespace std; 371717SN/A 382683Sktlim@umich.educlass RandomContext : public ParamContext 392680SN/A{ 405529Snate@binkert.org public: 412SN/A RandomContext(const string &_iniSection) 421858SN/A : ::ParamContext(_iniSection) {} 433565Sgblack@eecs.umich.edu ~RandomContext() {} 445529Snate@binkert.org 451917SN/A void checkParams(); 461070SN/A}; 471917SN/A 482188SN/ARandomContext paramContext("random"); 491917SN/A 502290SN/AParam<unsigned> 511070SN/Aseed(¶mContext, "seed", "seed to random number generator", 1); 521917SN/A 532SN/Avoid 545529Snate@binkert.orgRandomContext::checkParams() 55360SN/A{ 562519SN/A ::srand48(seed); 572SN/A} 582SN/A 592SN/Along 602SN/AgetLong() 612SN/A{ 621858SN/A return mrand48(); 632683Sktlim@umich.edu} 643453Sgblack@eecs.umich.edu 652683Sktlim@umich.eduint64_t 665712Shsul@eecs.umich.edugetUniform(int64_t maxmin) 672683Sktlim@umich.edu{ 682521SN/A double r; 692SN/A r = (drand48() - 0.500) * 2 * maxmin; 702683Sktlim@umich.edu DPRINTFN("getUniform %f\n", r); 712190SN/A return (int64_t)round(r); 722680SN/A} 732290SN/A 742526SN/Auint64_t 751917SN/AgetUniformPos(uint64_t max) 765529Snate@binkert.org{ 771982SN/A double r; 781917SN/A r = drand48() * 2 * max; 792683Sktlim@umich.edu return (uint64_t)round(r); 802683Sktlim@umich.edu} 811917SN/A 821917SN/A 831917SN/A// idea for generating a double from erand48 841917SN/Adouble 851917SN/AgetDouble() 861917SN/A{ 871917SN/A union { 881917SN/A uint32_t _long[2]; 892521SN/A uint16_t _short[4]; 905482Snate@binkert.org }; 913548Sgblack@eecs.umich.edu 922SN/A _long[0] = mrand48(); 932SN/A _long[1] = mrand48(); 944997Sgblack@eecs.umich.edu 954997Sgblack@eecs.umich.edu return ldexp((double) _short[0], -48) + 965712Shsul@eecs.umich.edu ldexp((double) _short[1], -32) + 974997Sgblack@eecs.umich.edu ldexp((double) _short[2], -16); 982SN/A} 992526SN/A