-
Notifications
You must be signed in to change notification settings - Fork 7
/
common.hpp
296 lines (249 loc) · 6.87 KB
/
common.hpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// solid/utility/common.hpp
//
// Copyright (c) 2007, 2008 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#pragma once
#include "solid/system/common.hpp"
#include <limits>
#ifdef __cpp_lib_bitops
#include <bit>
#endif
namespace solid {
template <typename T>
inline constexpr bool overflow_safe_less(const T& _u1, const T& _u2)
{
static_assert(std::is_unsigned_v<T>, "Type must be an unsigned_integer");
constexpr T pivot = (std::numeric_limits<T>::max() << 1);
if (_u1 < _u2) {
return (_u2 - _u1) <= pivot;
} else {
return (_u1 - _u2) > pivot;
}
}
template <typename T>
inline constexpr T overflow_safe_max(const T& _u1, const T& _u2)
{
if (overflow_safe_less(_u1, _u2)) {
return _u2;
} else {
return _u1;
}
}
inline constexpr uint64_t overflow_safe_max(const uint64_t& _u1, const uint64_t& _u2)
{
if (overflow_safe_less(_u1, _u2)) {
return _u2;
} else {
return _u1;
}
}
template <typename T>
inline constexpr T circular_distance(const T& _v, const T& _piv, const T& _max)
{
if (_v >= _piv) {
return _v - _piv;
} else {
return _max - _piv + _v;
}
}
inline constexpr size_t padded_size(const size_t _sz, const size_t _pad)
{
const size_t pad = (_pad - (_sz % _pad)) % _pad;
return _sz + pad;
}
inline constexpr size_t fast_padded_size(const size_t _sz, const size_t _bitpad)
{
// return padded_size(_sz, 1<<_bitpad);
const size_t padv = static_cast<size_t>(1) << _bitpad;
const size_t padmsk = padv - 1;
const size_t pad = (padv - (_sz & padmsk)) & padmsk;
return _sz + pad;
}
#ifdef __cpp_lib_bitops
template <typename T>
size_t bit_count(const T _v)
{
return static_cast<size_t>(std::popcount(_v));
}
#else
size_t bit_count(const uint8_t _v);
size_t bit_count(const uint16_t _v);
size_t bit_count(const uint32_t _v);
size_t bit_count(const uint64_t _v);
#endif
inline size_t leading_zero_count(uint64_t x)
{
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >> 16);
x = x | (x >> 32);
return bit_count(~x);
}
inline size_t leading_zero_count(uint32_t x)
{
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >> 16);
return bit_count(~x);
}
inline size_t leading_zero_count(uint16_t x)
{
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
return bit_count(static_cast<uint16_t>(~x));
}
inline size_t leading_zero_count(uint8_t x)
{
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
return bit_count(static_cast<uint8_t>(~x));
}
inline constexpr void pack(uint32_t& _v, const uint16_t _v1, const uint16_t _v2)
{
_v = _v2;
_v <<= 16;
_v |= _v1;
}
inline constexpr uint32_t pack(const uint16_t _v1, const uint16_t _v2)
{
uint32_t v = 0;
pack(v, _v1, _v2);
return v;
}
inline constexpr void unpack(uint16_t& _v1, uint16_t& _v2, const uint32_t _v)
{
_v1 = _v & 0xffffUL;
_v2 = (_v >> 16) & 0xffffUL;
}
extern const uint8_t reverted_chars[];
inline uint32_t bit_revert(const uint32_t _v)
{
uint32_t r = (((uint32_t)reverted_chars[_v & 0xff]) << 24);
r |= (((uint32_t)reverted_chars[(_v >> 8) & 0xff]) << 16);
r |= (((uint32_t)reverted_chars[(_v >> 16) & 0xff]) << 8);
r |= (((uint32_t)reverted_chars[(_v >> 24) & 0xff]) << 0);
return r;
}
inline uint64_t bit_revert(const uint64_t _v)
{
uint64_t r = (((uint64_t)reverted_chars[_v & 0xff]) << 56);
r |= (((uint64_t)reverted_chars[(_v >> 8) & 0xff]) << 48);
r |= (((uint64_t)reverted_chars[(_v >> 16) & 0xff]) << 40);
r |= (((uint64_t)reverted_chars[(_v >> 24) & 0xff]) << 32);
r |= (((uint64_t)reverted_chars[(_v >> 32) & 0xff]) << 24);
r |= (((uint64_t)reverted_chars[(_v >> 40) & 0xff]) << 16);
r |= (((uint64_t)reverted_chars[(_v >> 48) & 0xff]) << 8);
r |= (((uint64_t)reverted_chars[(_v >> 56) & 0xff]) << 0);
return r;
}
struct InvalidIndex {
template <typename SizeT>
constexpr operator SizeT() const
{
return (std::numeric_limits<SizeT>::max)();
}
};
struct InvalidSize {
template <typename SizeT>
operator SizeT() const
{
return (std::numeric_limits<SizeT>::max)();
}
};
template <typename SizeT>
constexpr bool operator==(SizeT const& _index, InvalidIndex const& _invalid)
{
return _index == static_cast<SizeT>(_invalid);
}
template <typename SizeT>
constexpr bool operator!=(SizeT const& _index, InvalidIndex const& _invalid)
{
return _index != static_cast<SizeT>(_invalid);
}
template <typename SizeT>
constexpr bool operator==(SizeT const& _index, InvalidSize const& _invalid)
{
return _index == static_cast<SizeT>(_invalid);
}
template <typename SizeT>
constexpr bool operator!=(SizeT const& _index, InvalidSize const& _invalid)
{
return _index != static_cast<SizeT>(_invalid);
}
template <typename SizeT>
constexpr inline bool is_invalid_index(SizeT const& _index)
{
return _index == InvalidIndex();
}
template <typename SizeT>
constexpr inline bool is_valid_index(SizeT const& _index)
{
return _index != InvalidIndex();
}
template <typename SizeT>
constexpr inline bool is_invalid_size(SizeT const& _index)
{
return _index == InvalidSize();
}
template <typename SizeT>
constexpr inline bool is_valid_size(SizeT const& _index)
{
return _index != InvalidSize();
}
template <class Enum>
inline constexpr std::underlying_type_t<Enum> to_underlying(const Enum& _val)
{
return static_cast<std::underlying_type_t<Enum>>(_val);
}
enum struct EndianessE : int {
#ifdef SOLID_ON_WINDOWS
Little = 0,
Big = 1,
#ifdef SOLID_ON_BIG_ENDIAN
Native = Big
#else
Native = Little
#endif
#else
Little = __ORDER_LITTLE_ENDIAN__,
Big = __ORDER_BIG_ENDIAN__,
Native = __BYTE_ORDER__
#endif
};
#ifdef __cpp_concepts
template <class Base, size_t Size = hardware_destructive_interference_size>
class Padded;
template <class Base, size_t Size>
requires((sizeof(Base) % Size) == 0)
class Padded<Base, Size> : public Base {
};
template <class Base, size_t Size>
class Padded : public Base {
static constexpr size_t padding_size = (Size - (sizeof(Base) % Size));
uint8_t padding_[padding_size];
};
#else
template <class Base, size_t Size = hardware_destructive_interference_size, class Enabled = void>
class Padded;
template <class Base, size_t Size>
class Padded<Base, Size, typename std::enable_if_t<(sizeof(Base) % Size) == 0>> : public Base {
};
template <class Base, size_t Size, class Enable>
class Padded : public Base {
static constexpr size_t padding_size = (Size - (sizeof(Base) % Size));
uint8_t padding_[padding_size];
};
#endif
} // namespace solid