random.cc revision 3483
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#include <cstdlib> 33#include <cmath> 34 35#if defined(__sun__) 36#include <ieeefp.h> 37#endif 38 39#include "sim/param.hh" 40#include "base/random.hh" 41#include "base/trace.hh" 42 43using namespace std; 44 45class RandomContext : public ParamContext 46{ 47 public: 48 RandomContext(const string &_iniSection) 49 : ::ParamContext(_iniSection) {} 50 ~RandomContext() {} 51 52 void checkParams(); 53}; 54 55RandomContext paramContext("random"); 56 57Param<unsigned> 58seed(¶mContext, "seed", "seed to random number generator", 1); 59 60void 61RandomContext::checkParams() 62{ 63 ::srand48(seed); 64} 65 66long 67getLong() 68{ 69 return mrand48(); 70} 71 72double 73m5round(double r) 74{ 75#if defined(__sun__) 76 double val; 77 fp_rnd oldrnd = fpsetround(FP_RN); 78 val = rint(r); 79 fpsetround(oldrnd); 80 return val; 81#else 82 return round(r); 83#endif 84} 85 86int64_t 87getUniform(int64_t min, int64_t max) 88{ 89 double r; 90 r = drand48() * (max-min) + min; 91 92 return (int64_t)m5round(r); 93} 94 95uint64_t 96getUniformPos(uint64_t min, uint64_t max) 97{ 98 double r; 99 r = drand48() * (max-min) + min; 100 101 return (uint64_t)m5round(r); 102} 103 104 105// idea for generating a double from erand48 106double 107getDouble() 108{ 109 union { 110 uint32_t _long[2]; 111 uint16_t _short[4]; 112 }; 113 114 _long[0] = mrand48(); 115 _long[1] = mrand48(); 116 117 return ldexp((double) _short[0], -48) + 118 ldexp((double) _short[1], -32) + 119 ldexp((double) _short[2], -16); 120} 121