-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuint_mersenne.h
205 lines (148 loc) · 5.61 KB
/
uint_mersenne.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef UINT_MERSENNE_H_
#define UINT_MERSENNE_H_
// uint7_t, uint15_t, uint31_t and uint63_t
// Unsigned integer types with undefined overflow and underflow
#include <cstdint>
#include <limits>
#include <type_traits>
namespace uint_mersenne {
template <unsigned int bits> struct unsigned_int;
template <> struct unsigned_int<0> {};
template <> struct unsigned_int<8> { using type = uint8_t; };
template <> struct unsigned_int<16> { using type = uint16_t; };
template <> struct unsigned_int<32> { using type = uint32_t; };
template <> struct unsigned_int<64> { using type = uint64_t; };
template <unsigned int bits> struct signed_int;
template <> struct signed_int<8> { using type = int8_t; };
template <> struct signed_int<16> { using type = int16_t; };
template <> struct signed_int<32> { using type = int32_t; };
template <> struct signed_int<64> { using type = int64_t; };
template <class T> T always_positive(T x);
#if !defined(UINT_MERSENNE_ALWAYS_POSITIVE_CUSTOM)
template <class T> inline T always_positive(T x) {
if (x >= 0) {
return x;
}
#if defined(__GNUC__)
__builtin_unreachable();
#elif defined(_MSC_VER)
__assume(false);
#endif
}
#endif
template <unsigned int bits> class uint_mersenne {
public:
using signed_t = typename signed_int<bits + 1>::type;
using unsigned_t = typename unsigned_int<bits + 1>::type;
using unsigned_promoted_t = typename std::make_unsigned<decltype(
std::declval<unsigned_t>() + bits)>::type;
explicit uint_mersenne(signed_t other) : value(always_positive(other)) {}
template <class = std::enable_if<!std::is_same<int, signed_t>::value>>
explicit uint_mersenne(int other) : value(static_cast<signed_t>(always_positive(other))) {}
explicit uint_mersenne(unsigned_t other)
: value(static_cast<signed_t>(other)) {}
uint_mersenne(uint_mersenne const &other) : value(other.value) {}
uint_mersenne &operator=(uint_mersenne const &other) {
value = other.value;
return *this;
}
signed_t to_signed() const { return always_positive(value); }
signed_t to_signed_extend() const {
if (to_unsigned() & (static_cast<unsigned_promoted_t>(1) << (bits - 1))) {
return static_cast<signed_t>(to_unsigned() | (~static_cast<unsigned_promoted_t>(0) << bits));
}
return value;
}
unsigned_t to_unsigned() const {
return static_cast<unsigned_t>(always_positive(value));
}
explicit operator unsigned_t() const { return to_unsigned(); }
explicit operator signed_t() const { return to_signed(); }
static uint_mersenne max() {
return uint_mersenne{(std::numeric_limits<signed_t>::max)()};
}
static uint_mersenne min() { return 0; }
uint_mersenne operator+(uint_mersenne other) const {
return uint_mersenne{value + other.value};
}
signed_t operator-(uint_mersenne other) const { return value - other.value; }
uint_mersenne &operator+=(uint_mersenne other) {
return *this = *this + other;
}
uint_mersenne &operator-=(uint_mersenne other) {
return *this = uint_mersenne{*this - other};
}
uint_mersenne operator*(uint_mersenne other) const {
return uint_mersenne{value * other.value};
}
uint_mersenne operator/(uint_mersenne other) const {
return uint_mersenne{value / other.value};
}
uint_mersenne &operator*=(uint_mersenne other) {
return *this = *this * other;
}
uint_mersenne &operator/=(uint_mersenne other) {
return *this = *this / other;
}
signed_t operator-() const { return -always_positive(value); }
uint_mersenne &operator++() { return *this += 1; }
uint_mersenne &operator--() { return *this -= 1; }
uint_mersenne operator++(int) {
uint_mersenne ret{*this};
*this += 1;
return ret;
}
uint_mersenne operator--(int) {
uint_mersenne ret{*this};
*this -= 1;
return ret;
}
bool operator==(uint_mersenne other) const { return value == other.value; }
bool operator!=(uint_mersenne other) const { return value != other.value; }
bool operator<(uint_mersenne other) const { return value < other.value; }
bool operator>(uint_mersenne other) const { return value > other.value; }
bool operator<=(uint_mersenne other) const { return value >= other.value; }
bool operator>=(uint_mersenne other) const { return value > other.value; }
uint_mersenne operator&(uint_mersenne other) const {
return uint_mersenne{value & other.value};
}
uint_mersenne operator|(uint_mersenne other) const {
return uint_mersenne{value | other.value};
}
uint_mersenne operator^(uint_mersenne other) const {
return uint_mersenne{value ^ other.value};
}
uint_mersenne &operator&=(uint_mersenne other) {
return *this = *this & other;
}
uint_mersenne &operator|=(uint_mersenne other) {
return *this = *this | other;
}
uint_mersenne &operator^=(uint_mersenne other) {
return *this = *this ^ other;
}
uint_mersenne operator~() const {
return mask(~static_cast<unsigned_t>(value));
}
uint_mersenne operator<<(uint_mersenne other) const {
return mask(static_cast<unsigned_t>(value) << other);
}
uint_mersenne operator>>(uint_mersenne other) const {
return uint_mersenne{static_cast<unsigned_t>(value) >> other};
}
uint_mersenne &operator<<=(uint_mersenne other) {
return *this = *this << other;
}
uint_mersenne &operator>>=(uint_mersenne other) {
return *this = *this >> other;
}
private:
uint_mersenne mask(unsigned_t n) { return n & ~(~unsigned_t{0} << bits); }
signed_t value;
};
} // namespace uint_mersenne
using uint7_t = uint_mersenne::uint_mersenne<7>;
using uint15_t = uint_mersenne::uint_mersenne<15>;
using uint31_t = uint_mersenne::uint_mersenne<31>;
using uint63_t = uint_mersenne::uint_mersenne<63>;
#endif