32,39d31
< #ifdef __SUNPRO_CC
< #include <stdlib.h>
< #include <math.h>
< #endif
<
< #include <cstdlib>
< #include <cmath>
<
40a33,34
> #include "base/intmath.hh"
> #include "base/misc.hh"
41a36
> #include "sim/serialize.hh"
45,46c40
< uint32_t
< getInt32()
---
> Random::Random()
48c42,43
< return mrand48() & 0xffffffff;
---
> // default random seed taken from original source
> init(5489);
51,52c46
< double
< getDouble()
---
> Random::Random(uint32_t s)
54c48
< return drand48();
---
> init(s);
57,58c51
< double
< m5round(double r)
---
> Random::Random(uint32_t init_key[], int key_length)
60,69c53
< #if defined(__sun)
< double val;
< int oldrnd = m5_fegetround();
< m5_fesetround(M5_FE_TONEAREST);
< val = rint(r);
< m5_fesetround(oldrnd);
< return val;
< #else
< return round(r);
< #endif
---
> init(init_key, key_length);
72,73c56
< int64_t
< getUniform(int64_t min, int64_t max)
---
> Random::~Random()
75,76c58
< double r;
< r = drand48() * (max-min) + min;
---
> }
78c60,76
< return (int64_t)m5round(r);
---
> // To preserve the uniform random distribution between min and max,
> // and allow all numbers to be represented, we generate a uniform
> // random number to the nearest power of two greater than max. If
> // this number doesn't fall between 0 and max, we try again. Anything
> // else would skew the distribution.
> uint32_t
> Random::genrand(uint32_t max)
> {
> int log = ceilLog2(max);
> int shift = (sizeof(uint32_t) * 8 - log);
> uint32_t random;
>
> do {
> random = genrand() >> shift;
> } while (random > max);
>
> return random;
82c80
< getUniformPos(uint64_t min, uint64_t max)
---
> Random::genrand(uint64_t max)
84,85c82,84
< double r;
< r = drand48() * (max-min) + min;
---
> int log = ceilLog2(max);
> int shift = (sizeof(uint64_t) * 8 - log);
> uint64_t random;
87c86,91
< return (uint64_t)m5round(r);
---
> do {
> random = (uint64_t)genrand() << 32 | (uint64_t)genrand();
> random = random >> shift;
> } while (random > max);
>
> return random;
88a93,116
>
> void
> Random::serialize(const string &base, ostream &os)
> {
> int length = N;
> paramOut(os, base + ".mti", mti);
> paramOut(os, base + ".length", length);
> arrayParamOut(os, base + ".data", mt, length);
> }
>
> void
> Random::unserialize(const string &base, Checkpoint *cp, const string &section)
> {
> int length;
>
> paramIn(cp, section, base + ".mti", mti);
> paramIn(cp, section, base + ".length", length);
> if (length != N)
> panic("cant unserialize random number data. length != %d\n", length);
>
> arrayParamIn(cp, section, base + ".data", mt, length);
> }
>
> Random random_mt;