LIEF: Library to Instrument Executable Formats
Version 1.0.0
Toggle main menu visibility
Loading...
Searching...
No Matches
lief-install
x86_64
static
include
frozen
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
26
#include "
frozen/bits/algorithms.h
"
27
#include "
frozen/bits/version.h
"
28
29
#include <cstdint>
30
#include <type_traits>
31
32
namespace
frozen
{
33
template
<
class
UIntType, UIntType a, UIntType c, UIntType m>
34
class
linear_congruential_engine
{
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
50
public
:
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
57
linear_congruential_engine
() =
default
;
58
constexpr
linear_congruential_engine
(
result_type
s) {
seed
(s); }
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
83
private
:
84
result_type
state_ =
default_seed
;
85
};
86
87
using
minstd_rand0
=
88
linear_congruential_engine<std::uint_fast32_t, 16807, 0, 2147483647>
;
89
using
minstd_rand
=
90
linear_congruential_engine<std::uint_fast32_t, 48271, 0, 2147483647>
;
91
92
// This generator is used by default in unordered frozen containers
93
using
default_prg_t
=
minstd_rand
;
94
95
}
// namespace frozen
96
97
#endif
algorithms.h
frozen::linear_congruential_engine
Definition
random.h:34
frozen::linear_congruential_engine::discard
constexpr void discard(unsigned long long n)
Definition
random.h:68
frozen::linear_congruential_engine::seed
void seed(result_type s=default_seed)
Definition
random.h:60
frozen::linear_congruential_engine::operator!=
friend constexpr bool operator!=(linear_congruential_engine const &self, linear_congruential_engine const &other)
Definition
random.h:78
frozen::linear_congruential_engine::linear_congruential_engine
constexpr linear_congruential_engine(result_type s)
Definition
random.h:58
frozen::linear_congruential_engine< std::uint_fast32_t, 16807, 0, 2147483647 >::multiplier
static constexpr result_type multiplier
Definition
random.h:52
frozen::linear_congruential_engine::max
static constexpr result_type max()
Definition
random.h:73
frozen::linear_congruential_engine< std::uint_fast32_t, 16807, 0, 2147483647 >::modulus
static constexpr result_type modulus
Definition
random.h:54
frozen::linear_congruential_engine::min
static constexpr result_type min()
Definition
random.h:72
frozen::linear_congruential_engine::operator()
constexpr result_type operator()()
Definition
random.h:61
frozen::linear_congruential_engine::linear_congruential_engine
linear_congruential_engine()=default
frozen::linear_congruential_engine< std::uint_fast32_t, 16807, 0, 2147483647 >::default_seed
static constexpr result_type default_seed
Definition
random.h:55
frozen::linear_congruential_engine< std::uint_fast32_t, 16807, 0, 2147483647 >::increment
static constexpr result_type increment
Definition
random.h:53
frozen::linear_congruential_engine::operator==
friend constexpr bool operator==(linear_congruential_engine const &self, linear_congruential_engine const &other)
Definition
random.h:74
frozen::linear_congruential_engine::result_type
UIntType result_type
Definition
random.h:51
version.h
frozen::bits::log
auto constexpr log(T v)
Definition
algorithms.h:46
frozen::bits::select_uint_least_t
decltype(select_uint_least(std::integral_constant< std::size_t, bit_weight(N)>())) select_uint_least_t
Definition
algorithms.h:73
frozen
Definition
algorithm.h:30
frozen::default_prg_t
minstd_rand default_prg_t
Definition
random.h:93
frozen::minstd_rand0
linear_congruential_engine< std::uint_fast32_t, 16807, 0, 2147483647 > minstd_rand0
Definition
random.h:87
frozen::minstd_rand
linear_congruential_engine< std::uint_fast32_t, 48271, 0, 2147483647 > minstd_rand
Definition
random.h:89
Generated by
1.17.0