gpu-compute,mem-ruby: Refactor GPU coalescer
[gem5.git] / src / base / random.cc
index e135b55f5b9b053ba7f9bb0d434ff357731006e6..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
  */
 
-#include <cstdlib>
-#include <cmath>
-
-#include "sim/param.hh"
 #include "base/random.hh"
-#include "base/trace.hh"
 
-using namespace std;
+#include <sstream>
 
-class RandomContext : public ParamContext
-{
-  public:
-    RandomContext(const string &_iniSection)
-        : ::ParamContext(_iniSection) {}
-    ~RandomContext() {}
+#include "base/logging.hh"
+#include "sim/serialize.hh"
 
-    void checkParams();
-};
-
-RandomContext paramContext("random");
-
-Param<unsigned>
-seed(&paramContext, "seed", "seed to random number generator", 1);
-
-void
-RandomContext::checkParams()
+Random::Random()
 {
-    ::srand48(seed);
+    // default random seed
+    init(5489);
 }
 
-long
-getLong()
+Random::Random(uint32_t s)
 {
-    return mrand48();
+    init(s);
 }
 
-int64_t
-getUniform(int64_t min, int64_t max)
+Random::~Random()
 {
-    double r;
-    r = drand48() * (max-min) + min;
-    return (int64_t)round(r);
 }
 
-uint64_t
-getUniformPos(uint64_t min, uint64_t max)
+void
+Random::init(uint32_t s)
 {
-    double r;
-    r = drand48() * (max-min) + min;
-    return (uint64_t)round(r);
+    gen.seed(s);
 }
 
-
-// idea for generating a double from erand48
-double
-getDouble()
+void
+Random::serialize(CheckpointOut &cp) const
 {
-    union {
-        uint32_t _long[2];
-        uint16_t _short[4];
-    };
+    panic("Currently not used anywhere.\n");
+
+    // get the state from the generator
+    std::ostringstream oss;
+    oss << gen;
+    std::string state = oss.str();
+    paramOut(cp, "mt_state", state);
+}
 
-    _long[0] = mrand48();
-    _long[1] = mrand48();
+void
+Random::unserialize(CheckpointIn &cp)
+{
+    panic("Currently not used anywhere.\n");
 
-    return ldexp((double) _short[0], -48) +
-        ldexp((double) _short[1], -32) +
-        ldexp((double) _short[2], -16);
+    // 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;