random.cc revision 10905
12SN/A/*
210349Sandreas.hansson@arm.com * Copyright (c) 2014 ARM Limited
310349Sandreas.hansson@arm.com * All rights reserved
410349Sandreas.hansson@arm.com *
510349Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
610349Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
710349Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
810349Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
910349Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
1010349Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
1110349Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
1210349Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
1310349Sandreas.hansson@arm.com *
141762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
152SN/A * All rights reserved.
162SN/A *
172SN/A * Redistribution and use in source and binary forms, with or without
182SN/A * modification, are permitted provided that the following conditions are
192SN/A * met: redistributions of source code must retain the above copyright
202SN/A * notice, this list of conditions and the following disclaimer;
212SN/A * redistributions in binary form must reproduce the above copyright
222SN/A * notice, this list of conditions and the following disclaimer in the
232SN/A * documentation and/or other materials provided with the distribution;
242SN/A * neither the name of the copyright holders nor the names of its
252SN/A * contributors may be used to endorse or promote products derived from
262SN/A * this software without specific prior written permission.
272SN/A *
282SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665Ssaidi@eecs.umich.edu *
402665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
412665Ssaidi@eecs.umich.edu *          Ali Saidi
4210349Sandreas.hansson@arm.com *          Andreas Hansson
432SN/A */
442SN/A
4510349Sandreas.hansson@arm.com#include <sstream>
4610349Sandreas.hansson@arm.com
475190Ssaidi@eecs.umich.edu#include "base/misc.hh"
4856SN/A#include "base/random.hh"
495190Ssaidi@eecs.umich.edu#include "sim/serialize.hh"
502SN/A
515190Ssaidi@eecs.umich.eduRandom::Random()
522SN/A{
5310349Sandreas.hansson@arm.com    // default random seed
545190Ssaidi@eecs.umich.edu    init(5489);
552SN/A}
562SN/A
575190Ssaidi@eecs.umich.eduRandom::Random(uint32_t s)
582SN/A{
595190Ssaidi@eecs.umich.edu    init(s);
602SN/A}
612SN/A
625190Ssaidi@eecs.umich.eduRandom::~Random()
631954SN/A{
645190Ssaidi@eecs.umich.edu}
653483Ssaidi@eecs.umich.edu
6610349Sandreas.hansson@arm.comvoid
6710349Sandreas.hansson@arm.comRandom::init(uint32_t s)
685190Ssaidi@eecs.umich.edu{
6910349Sandreas.hansson@arm.com    gen.seed(s);
701954SN/A}
715190Ssaidi@eecs.umich.edu
725190Ssaidi@eecs.umich.eduvoid
7310905Sandreas.sandberg@arm.comRandom::serialize(CheckpointOut &cp) const
745190Ssaidi@eecs.umich.edu{
7510349Sandreas.hansson@arm.com    panic("Currently not used anywhere.\n");
7610349Sandreas.hansson@arm.com
7710349Sandreas.hansson@arm.com    // get the state from the generator
7810349Sandreas.hansson@arm.com    std::ostringstream oss;
7910349Sandreas.hansson@arm.com    oss << gen;
8010349Sandreas.hansson@arm.com    std::string state = oss.str();
8110905Sandreas.sandberg@arm.com    paramOut(cp, "mt_state", state);
825190Ssaidi@eecs.umich.edu}
835190Ssaidi@eecs.umich.edu
845190Ssaidi@eecs.umich.eduvoid
8510905Sandreas.sandberg@arm.comRandom::unserialize(CheckpointIn &cp)
865190Ssaidi@eecs.umich.edu{
8710349Sandreas.hansson@arm.com    panic("Currently not used anywhere.\n");
885190Ssaidi@eecs.umich.edu
8910349Sandreas.hansson@arm.com    // the random generator state did not use to be part of the
9010349Sandreas.hansson@arm.com    // checkpoint state, so be forgiving in the unserialization and
9110349Sandreas.hansson@arm.com    // keep on going if the parameter is not there
9210349Sandreas.hansson@arm.com    std::string state;
9310905Sandreas.sandberg@arm.com    if (optParamIn(cp, "mt_state", state)) {
9410349Sandreas.hansson@arm.com        std::istringstream iss(state);
9510349Sandreas.hansson@arm.com        iss >> gen;
9610349Sandreas.hansson@arm.com    }
975190Ssaidi@eecs.umich.edu}
985190Ssaidi@eecs.umich.edu
995190Ssaidi@eecs.umich.eduRandom random_mt;
100