blob: 5ddd97f247fedfa551f01bf78cbb4b27e9635244 [file] [log] [blame]
Matt Wala1bd2fce2014-08-08 14:02:09 -07001//===- subzero/src/IceRNG.h - Random number generator -----------*- C++ -*-===//
2//
3// The Subzero Code Generator
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Andrew Scull9612d322015-07-06 14:53:25 -07009///
10/// \file
11/// This file declares a random number generator.
12///
Matt Wala1bd2fce2014-08-08 14:02:09 -070013//===----------------------------------------------------------------------===//
14
15#ifndef SUBZERO_SRC_ICERNG_H
16#define SUBZERO_SRC_ICERNG_H
17
Matt Wala1bd2fce2014-08-08 14:02:09 -070018#include "llvm/ADT/StringRef.h"
Matt Walac3302742014-08-15 16:21:56 -070019#include "llvm/Support/Compiler.h"
John Porto67f8de92015-06-25 10:14:17 -070020#include <cstdint>
Matt Wala1bd2fce2014-08-08 14:02:09 -070021
22namespace Ice {
23
24class RandomNumberGenerator {
Jim Stichnothc6ead202015-02-24 09:30:30 -080025 RandomNumberGenerator() = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -070026 RandomNumberGenerator(const RandomNumberGenerator &) = delete;
27 RandomNumberGenerator &operator=(const RandomNumberGenerator &) = delete;
28
Matt Wala1bd2fce2014-08-08 14:02:09 -070029public:
Jan Voung1f47ad02015-03-20 15:01:26 -070030 explicit RandomNumberGenerator(uint64_t Seed, llvm::StringRef Salt = "");
Matt Wala1bd2fce2014-08-08 14:02:09 -070031 uint64_t next(uint64_t Max);
32
33private:
34 uint64_t State;
35};
36
Andrew Scull9612d322015-07-06 14:53:25 -070037/// This class adds additional random number generator utilities. The
38/// reason for the wrapper class is that we want to keep the
39/// RandomNumberGenerator interface identical to LLVM's.
Matt Walac3302742014-08-15 16:21:56 -070040class RandomNumberGeneratorWrapper {
Jim Stichnothc6ead202015-02-24 09:30:30 -080041 RandomNumberGeneratorWrapper() = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -070042 RandomNumberGeneratorWrapper(const RandomNumberGeneratorWrapper &) = delete;
43 RandomNumberGeneratorWrapper &
44 operator=(const RandomNumberGeneratorWrapper &) = delete;
45
Matt Walac3302742014-08-15 16:21:56 -070046public:
Jim Stichnothe6d24782014-12-19 05:42:24 -080047 uint64_t operator()(uint64_t Max) { return RNG.next(Max); }
Matt Walac3302742014-08-15 16:21:56 -070048 bool getTrueWithProbability(float Probability);
Jim Stichnothc6ead202015-02-24 09:30:30 -080049 explicit RandomNumberGeneratorWrapper(RandomNumberGenerator &RNG)
50 : RNG(RNG) {}
Matt Walac3302742014-08-15 16:21:56 -070051
52private:
Matt Walac3302742014-08-15 16:21:56 -070053 RandomNumberGenerator &RNG;
54};
55
Andrew Scull9612d322015-07-06 14:53:25 -070056/// RandomShuffle is an implementation of std::random_shuffle() that
57/// doesn't change across stdlib implementations. Adapted from a
58/// sample implementation at cppreference.com.
Jim Stichnothcaef3482015-04-09 11:19:38 -070059template <class RandomIt, class RandomFunc>
60void RandomShuffle(RandomIt First, RandomIt Last, RandomFunc &&RNG) {
61 for (auto i = Last - First - 1; i > 0; --i)
62 std::swap(First[i], First[RNG(i + 1)]);
63}
64
Matt Wala1bd2fce2014-08-08 14:02:09 -070065} // end of namespace Ice
66
67#endif // SUBZERO_SRC_ICERNG_H