LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
random.h
Go to the documentation of this file.
1/*
2 * Frozen
3 * Copyright 2016 QuarksLab
4 *
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23#ifndef FROZEN_LETITGO_RANDOM_H
24#define FROZEN_LETITGO_RANDOM_H
25
27#include "frozen/bits/version.h"
28
29#include <cstdint>
30#include <type_traits>
31
32namespace frozen {
33template <class UIntType, UIntType a, UIntType c, UIntType m>
35
36 static_assert(std::is_unsigned<UIntType>::value,
37 "UIntType must be an unsigned integral type");
38
39 template<class T>
40 static constexpr UIntType modulo(T val, std::integral_constant<UIntType, 0>) {
41 return static_cast<UIntType>(val);
42 }
43
44 template<class T, UIntType M>
45 static constexpr UIntType modulo(T val, std::integral_constant<UIntType, M>) {
46 // the static cast below may end up doing a truncation
47 return static_cast<UIntType>(val % M);
48 }
49
50public:
51 using result_type = UIntType;
52 static constexpr result_type multiplier = a;
53 static constexpr result_type increment = c;
54 static constexpr result_type modulus = m;
55 static constexpr result_type default_seed = 1u;
56
59
60 void seed(result_type s = default_seed) { state_ = s; }
61 constexpr result_type operator()() {
62 using uint_least_t = bits::select_uint_least_t<bits::log(a) + bits::log(m) + 4>;
63 uint_least_t tmp = static_cast<uint_least_t>(multiplier) * state_ + increment;
64
65 state_ = modulo(tmp, std::integral_constant<UIntType, modulus>());
66 return state_;
67 }
68 constexpr void discard(unsigned long long n) {
69 while (n--)
70 operator()();
71 }
72 static constexpr result_type min() { return increment == 0u ? 1u : 0u; }
73 static constexpr result_type max() { return modulus - 1u; }
74 friend constexpr bool operator==(linear_congruential_engine const &self,
75 linear_congruential_engine const &other) {
76 return self.state_ == other.state_;
77 }
78 friend constexpr bool operator!=(linear_congruential_engine const &self,
79 linear_congruential_engine const &other) {
80 return !(self == other);
81 }
82
83private:
85};
86
91
92// This generator is used by default in unordered frozen containers
94
95} // namespace frozen
96
97#endif
constexpr void discard(unsigned long long n)
Definition random.h:68
void seed(result_type s=default_seed)
Definition random.h:60
friend constexpr bool operator!=(linear_congruential_engine const &self, linear_congruential_engine const &other)
Definition random.h:78
constexpr linear_congruential_engine(result_type s)
Definition random.h:58
static constexpr result_type multiplier
Definition random.h:52
static constexpr result_type max()
Definition random.h:73
static constexpr result_type modulus
Definition random.h:54
static constexpr result_type min()
Definition random.h:72
constexpr result_type operator()()
Definition random.h:61
static constexpr result_type default_seed
Definition random.h:55
static constexpr result_type increment
Definition random.h:53
friend constexpr bool operator==(linear_congruential_engine const &self, linear_congruential_engine const &other)
Definition random.h:74
UIntType result_type
Definition random.h:51
auto constexpr log(T v)
Definition algorithms.h:46
decltype(select_uint_least(std::integral_constant< std::size_t, bit_weight(N)>())) select_uint_least_t
Definition algorithms.h:73
Definition algorithm.h:30
minstd_rand default_prg_t
Definition random.h:93
linear_congruential_engine< std::uint_fast32_t, 16807, 0, 2147483647 > minstd_rand0
Definition random.h:87
linear_congruential_engine< std::uint_fast32_t, 48271, 0, 2147483647 > minstd_rand
Definition random.h:89