gpu-compute,mem-ruby: Refactor GPU coalescer
[gem5.git] / src / base / random.cc
index 8a2e3c1c0c8a47db4a761abc720c09a393ee1004..63f4b7f2ceec3b789815269514a3184a944152cf 100644 (file)
@@ -1,4 +1,16 @@
 /*
+ * 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
  */
 
-#ifdef __SUNPRO_CC
-#include <stdlib.h>
-#include <math.h>
-#endif
+#include "base/random.hh"
 
-#include <cstdlib>
-#include <cmath>
+#include <sstream>
 
-#include "base/fenv.hh"
-#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;
-    int oldrnd = m5_fegetround();
-    m5_fesetround(M5_FE_TONEAREST);
-    val = rint(r);
-    m5_fesetround(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;