random.cc (2665:a124942bacb8) | random.cc (3483:edede8473667) |
---|---|
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; --- 18 unchanged lines hidden (view full) --- 27 * 28 * Authors: Nathan Binkert 29 * Ali Saidi 30 */ 31 32#include <cstdlib> 33#include <cmath> 34 | 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; --- 18 unchanged lines hidden (view full) --- 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 |
|
35#include "sim/param.hh" 36#include "base/random.hh" 37#include "base/trace.hh" 38 39using namespace std; 40 41class RandomContext : public ParamContext 42{ --- 17 unchanged lines hidden (view full) --- 60} 61 62long 63getLong() 64{ 65 return mrand48(); 66} 67 | 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{ --- 17 unchanged lines hidden (view full) --- 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 |
|
68int64_t 69getUniform(int64_t min, int64_t max) 70{ 71 double r; 72 r = drand48() * (max-min) + min; | 86int64_t 87getUniform(int64_t min, int64_t max) 88{ 89 double r; 90 r = drand48() * (max-min) + min; |
73 return (int64_t)round(r); | 91 92 return (int64_t)m5round(r); |
74} 75 76uint64_t 77getUniformPos(uint64_t min, uint64_t max) 78{ 79 double r; 80 r = drand48() * (max-min) + min; | 93} 94 95uint64_t 96getUniformPos(uint64_t min, uint64_t max) 97{ 98 double r; 99 r = drand48() * (max-min) + min; |
81 return (uint64_t)round(r); | 100 101 return (uint64_t)m5round(r); |
82} 83 84 85// idea for generating a double from erand48 86double 87getDouble() 88{ 89 union { 90 uint32_t _long[2]; 91 uint16_t _short[4]; 92 }; 93 94 _long[0] = mrand48(); 95 _long[1] = mrand48(); 96 97 return ldexp((double) _short[0], -48) + 98 ldexp((double) _short[1], -32) + 99 ldexp((double) _short[2], -16); 100} | 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} |