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