/*
+ * 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.
+ *
* Copyright (c) 2003-2005 The Regents of The University of Michigan
* All rights reserved.
*
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * Authors: Nathan Binkert
- * Ali Saidi
*/
-#if defined(__sun)
-#include <ieeefp.h>
-#endif
-#ifdef __SUNPRO_CC
-#include <stdlib.h>
-#include <math.h>
-#endif
+#include "base/random.hh"
-#include <cstdlib>
-#include <cmath>
+#include <sstream>
-#include "base/random.hh"
+#include "base/logging.hh"
+#include "sim/serialize.hh"
-using namespace std;
+Random::Random()
+{
+ // default random seed
+ init(5489);
+}
-uint32_t
-getInt32()
+Random::Random(uint32_t s)
{
- return mrand48() & 0xffffffff;
+ init(s);
}
-double
-getDouble()
+Random::~Random()
{
- return drand48();
}
-double
-m5round(double r)
+void
+Random::init(uint32_t s)
{
-#if defined(__sun)
- double val;
- fp_rnd oldrnd = fpsetround(FP_RN);
- val = rint(r);
- fpsetround(oldrnd);
- return val;
-#else
- return round(r);
-#endif
+ gen.seed(s);
}
-int64_t
-getUniform(int64_t min, int64_t max)
+void
+Random::serialize(CheckpointOut &cp) const
{
- double r;
- r = drand48() * (max-min) + min;
+ panic("Currently not used anywhere.\n");
- return (int64_t)m5round(r);
+ // get the state from the generator
+ std::ostringstream oss;
+ oss << gen;
+ std::string state = oss.str();
+ paramOut(cp, "mt_state", state);
}
-uint64_t
-getUniformPos(uint64_t min, uint64_t max)
+void
+Random::unserialize(CheckpointIn &cp)
{
- double r;
- r = drand48() * (max-min) + min;
+ panic("Currently not used anywhere.\n");
- return (uint64_t)m5round(r);
+ // 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, "mt_state", state)) {
+ std::istringstream iss(state);
+ iss >> gen;
+ }
}
+
+Random random_mt;