1a2,13
> * Copyright (c) 2014 ARM Limited
> * All rights reserved
> *
> * The license below extends only to copyright in the software and shall
> * not be construed as granting a license to any other intellectual
> * property including but not limited to intellectual property relating
> * to a hardware implementation of the functionality of the software
> * licensed hereunder. You may use the software subject to the license
> * terms below provided that you ensure that this notice is replicated
> * unmodified and in its entirety in all distributions of the software,
> * modified or unmodified, in source code or in binary form.
> *
29a42
> * Andreas Hansson
32,34c45,46
< #include <limits>
< #include "base/fenv.hh"
< #include "base/intmath.hh"
---
> #include <sstream>
>
39,40d50
< using namespace std;
<
43c53
< // default random seed taken from original source
---
> // default random seed
52,56d61
< Random::Random(uint32_t init_key[], int key_length)
< {
< init(init_key, key_length);
< }
<
61,67c66,67
< // 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)
---
> void
> Random::init(uint32_t s)
69,82c69
< if (max == 0)
< return 0;
< if (max == std::numeric_limits<uint32_t>::max())
< return genrand();
<
< int log = ceilLog2(max + 1);
< int shift = (sizeof(uint32_t) * 8 - log);
< uint32_t random;
<
< do {
< random = genrand() >> shift;
< } while (random > max);
<
< return random;
---
> gen.seed(s);
85,86c72,73
< uint64_t
< Random::genrand(uint64_t max)
---
> void
> Random::serialize(std::ostream &os)
88,91c75
< if (max == 0)
< return 0;
< if (max == std::numeric_limits<uint64_t>::max())
< return genrand();
---
> panic("Currently not used anywhere.\n");
93,102c77,81
< int log = ceilLog2(max + 1);
< int shift = (sizeof(uint64_t) * 8 - log);
< uint64_t random;
<
< do {
< random = (uint64_t)genrand() << 32 | (uint64_t)genrand();
< random = random >> shift;
< } while (random > max);
<
< return random;
---
> // get the state from the generator
> std::ostringstream oss;
> oss << gen;
> std::string state = oss.str();
> paramOut(os, "mt_state", state);
106c85
< Random::serialize(const string &base, ostream &os)
---
> Random::unserialize(Checkpoint *cp, const std::string &section)
108,112c87
< int length = N;
< paramOut(os, base + ".mti", mti);
< paramOut(os, base + ".length", length);
< arrayParamOut(os, base + ".data", mt, length);
< }
---
> panic("Currently not used anywhere.\n");
114,124c89,96
< 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);
---
> // the random generator state did not use to be part of the
> // checkpoint state, so be forgiving in the unserialization and
> // keep on going if the parameter is not there
> std::string state;
> if (optParamIn(cp, section, "mt_state", state)) {
> std::istringstream iss(state);
> iss >> gen;
> }