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 457771Snate@binkert.org/* 4610349Sandreas.hansson@arm.com * Mersenne twister random number generator. 477771Snate@binkert.org */ 487771Snate@binkert.org 491296SN/A#ifndef __BASE_RANDOM_HH__ 501296SN/A#define __BASE_RANDOM_HH__ 512SN/A 5210349Sandreas.hansson@arm.com#include <random> 535190Ssaidi@eecs.umich.edu#include <string> 5410349Sandreas.hansson@arm.com#include <type_traits> 555190Ssaidi@eecs.umich.edu 5610905Sandreas.sandberg@arm.com#include "base/compiler.hh" 576214Snate@binkert.org#include "base/types.hh" 5810905Sandreas.sandberg@arm.com#include "sim/serialize.hh" 592SN/A 605190Ssaidi@eecs.umich.educlass Checkpoint; 612SN/A 6210905Sandreas.sandberg@arm.comclass Random : public Serializable 635190Ssaidi@eecs.umich.edu{ 642SN/A 6510349Sandreas.hansson@arm.com private: 661954SN/A 6710349Sandreas.hansson@arm.com std::mt19937_64 gen; 685190Ssaidi@eecs.umich.edu 695190Ssaidi@eecs.umich.edu public: 7010349Sandreas.hansson@arm.com 715190Ssaidi@eecs.umich.edu Random(); 725190Ssaidi@eecs.umich.edu Random(uint32_t s); 735190Ssaidi@eecs.umich.edu ~Random(); 745190Ssaidi@eecs.umich.edu 755190Ssaidi@eecs.umich.edu void init(uint32_t s); 765190Ssaidi@eecs.umich.edu 7710349Sandreas.hansson@arm.com /** 7810349Sandreas.hansson@arm.com * Use the SFINAE idiom to choose an implementation based on 7910349Sandreas.hansson@arm.com * whether the type is integral or floating point. 8010349Sandreas.hansson@arm.com */ 815190Ssaidi@eecs.umich.edu template <typename T> 8210349Sandreas.hansson@arm.com typename std::enable_if<std::is_integral<T>::value, T>::type 835190Ssaidi@eecs.umich.edu random() 845190Ssaidi@eecs.umich.edu { 8510349Sandreas.hansson@arm.com // [0, max_value] for integer types 8610349Sandreas.hansson@arm.com std::uniform_int_distribution<T> dist; 8710349Sandreas.hansson@arm.com return dist(gen); 885190Ssaidi@eecs.umich.edu } 895190Ssaidi@eecs.umich.edu 905190Ssaidi@eecs.umich.edu template <typename T> 9110349Sandreas.hansson@arm.com typename std::enable_if<std::is_floating_point<T>::value, T>::type 9210349Sandreas.hansson@arm.com random() 9310349Sandreas.hansson@arm.com { 9410349Sandreas.hansson@arm.com // [0, 1) for real types 9510349Sandreas.hansson@arm.com std::uniform_real_distribution<T> dist; 9610349Sandreas.hansson@arm.com return dist(gen); 9710349Sandreas.hansson@arm.com } 9810349Sandreas.hansson@arm.com 9910349Sandreas.hansson@arm.com template <typename T> 10010349Sandreas.hansson@arm.com typename std::enable_if<std::is_integral<T>::value, T>::type 1015190Ssaidi@eecs.umich.edu random(T min, T max) 1025190Ssaidi@eecs.umich.edu { 10310349Sandreas.hansson@arm.com std::uniform_int_distribution<T> dist(min, max); 10410349Sandreas.hansson@arm.com return dist(gen); 1055190Ssaidi@eecs.umich.edu } 1065190Ssaidi@eecs.umich.edu 10711168Sandreas.hansson@arm.com void serialize(CheckpointOut &cp) const override; 10811168Sandreas.hansson@arm.com void unserialize(CheckpointIn &cp) override; 1092SN/A}; 1102SN/A 1115190Ssaidi@eecs.umich.eduextern Random random_mt; 1122SN/A 1131296SN/A#endif // __BASE_RANDOM_HH__ 114