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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
#ifndef MPT_ENDIAN_INTEGER_HPP
#define MPT_ENDIAN_INTEGER_HPP
#include "mpt/base/detect.hpp"
#include "mpt/base/bit.hpp"
#include "mpt/base/integer.hpp"
#include "mpt/base/macros.hpp"
#include "mpt/base/memory.hpp"
#include "mpt/base/namespace.hpp"
#include <array>
#include <limits>
#include <type_traits>
#include <cstddef>
#include <cstdint>
#include <cstring>
#if MPT_COMPILER_MSVC
#include <intrin.h>
#endif
namespace mpt {
inline namespace MPT_INLINE_NS {
struct BigEndian_tag {
static constexpr mpt::endian endian = mpt::endian::big;
};
struct LittleEndian_tag {
static constexpr mpt::endian endian = mpt::endian::little;
};
constexpr inline uint16 constexpr_bswap16(uint16 x) noexcept {
return uint16(0)
| ((x >> 8) & 0x00FFu)
| ((x << 8) & 0xFF00u);
}
constexpr inline uint32 constexpr_bswap32(uint32 x) noexcept {
return uint32(0)
| ((x & 0x000000FFu) << 24)
| ((x & 0x0000FF00u) << 8)
| ((x & 0x00FF0000u) >> 8)
| ((x & 0xFF000000u) >> 24);
}
constexpr inline uint64 constexpr_bswap64(uint64 x) noexcept {
return uint64(0)
| (((x >> 0) & 0xffull) << 56)
| (((x >> 8) & 0xffull) << 48)
| (((x >> 16) & 0xffull) << 40)
| (((x >> 24) & 0xffull) << 32)
| (((x >> 32) & 0xffull) << 24)
| (((x >> 40) & 0xffull) << 16)
| (((x >> 48) & 0xffull) << 8)
| (((x >> 56) & 0xffull) << 0);
}
#if MPT_COMPILER_GCC
#define MPT_bswap16 __builtin_bswap16
#define MPT_bswap32 __builtin_bswap32
#define MPT_bswap64 __builtin_bswap64
#elif MPT_COMPILER_MSVC
#define MPT_bswap16 _byteswap_ushort
#define MPT_bswap32 _byteswap_ulong
#define MPT_bswap64 _byteswap_uint64
#endif
// No intrinsics available
#ifndef MPT_bswap16
#define MPT_bswap16(x) mpt::constexpr_bswap16(x)
#endif
#ifndef MPT_bswap32
#define MPT_bswap32(x) mpt::constexpr_bswap32(x)
#endif
#ifndef MPT_bswap64
#define MPT_bswap64(x) mpt::constexpr_bswap64(x)
#endif
template <typename T, typename Tendian, std::size_t size>
MPT_CONSTEXPRINLINE std::array<std::byte, size> EndianEncode(T val) noexcept {
static_assert(Tendian::endian == mpt::endian::little || Tendian::endian == mpt::endian::big);
static_assert(std::numeric_limits<T>::is_integer);
static_assert(!std::numeric_limits<T>::is_signed);
static_assert(sizeof(T) == size);
using base_type = T;
using unsigned_base_type = typename std::make_unsigned<base_type>::type;
using endian_type = Tendian;
unsigned_base_type uval = static_cast<unsigned_base_type>(val);
std::array<std::byte, size> data{};
if constexpr (endian_type::endian == mpt::endian::little) {
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
data[i] = static_cast<std::byte>(static_cast<uint8>((uval >> (i * 8)) & 0xffu));
}
} else {
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
data[(sizeof(base_type) - 1) - i] = static_cast<std::byte>(static_cast<uint8>((uval >> (i * 8)) & 0xffu));
}
}
return data;
}
template <typename T, typename Tendian, std::size_t size>
MPT_CONSTEXPRINLINE T EndianDecode(std::array<std::byte, size> data) noexcept {
static_assert(Tendian::endian == mpt::endian::little || Tendian::endian == mpt::endian::big);
static_assert(std::numeric_limits<T>::is_integer);
static_assert(!std::numeric_limits<T>::is_signed);
static_assert(sizeof(T) == size);
using base_type = T;
using unsigned_base_type = typename std::make_unsigned<base_type>::type;
using endian_type = Tendian;
base_type val = base_type();
unsigned_base_type uval = unsigned_base_type();
if constexpr (endian_type::endian == mpt::endian::little) {
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
uval |= static_cast<unsigned_base_type>(static_cast<uint8>(data[i])) << (i * 8);
}
} else {
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
uval |= static_cast<unsigned_base_type>(static_cast<uint8>(data[(sizeof(base_type) - 1) - i])) << (i * 8);
}
}
val = static_cast<base_type>(uval);
return val;
}
MPT_CONSTEXPR20_FUN uint64 SwapBytesImpl(uint64 value) noexcept {
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
return mpt::constexpr_bswap64(value);
} else {
return MPT_bswap64(value);
}
}
MPT_CONSTEXPR20_FUN uint32 SwapBytesImpl(uint32 value) noexcept {
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
return mpt::constexpr_bswap32(value);
} else {
return MPT_bswap32(value);
}
}
MPT_CONSTEXPR20_FUN uint16 SwapBytesImpl(uint16 value) noexcept {
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
return mpt::constexpr_bswap16(value);
} else {
return MPT_bswap16(value);
}
}
MPT_CONSTEXPR20_FUN int64 SwapBytesImpl(int64 value) noexcept {
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
return mpt::constexpr_bswap64(value);
} else {
return MPT_bswap64(value);
}
}
MPT_CONSTEXPR20_FUN int32 SwapBytesImpl(int32 value) noexcept {
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
return mpt::constexpr_bswap32(value);
} else {
return MPT_bswap32(value);
}
}
MPT_CONSTEXPR20_FUN int16 SwapBytesImpl(int16 value) noexcept {
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
return mpt::constexpr_bswap16(value);
} else {
return MPT_bswap16(value);
}
}
// Do NOT remove these overloads, even if they seem useless.
// We do not want risking to extend 8bit integers to int and then
// endian-converting and casting back to int.
// Thus these overloads.
MPT_CONSTEXPR20_FUN uint8 SwapBytesImpl(uint8 value) noexcept {
return value;
}
MPT_CONSTEXPR20_FUN int8 SwapBytesImpl(int8 value) noexcept {
return value;
}
MPT_CONSTEXPR20_FUN char SwapBytesImpl(char value) noexcept {
return value;
}
#undef MPT_bswap16
#undef MPT_bswap32
#undef MPT_bswap64
// On-disk integer types with defined endianness and no alignemnt requirements
// Note: To easily debug module loaders (and anything else that uses this
// wrapper struct), you can use the Debugger Visualizers available in
// build/vs/debug/ to conveniently view the wrapped contents.
template <typename T, typename Tendian>
struct packed {
public:
using base_type = T;
using endian_type = Tendian;
public:
std::array<std::byte, sizeof(base_type)> data;
public:
MPT_CONSTEXPR20_FUN void set(base_type val) noexcept {
static_assert(std::numeric_limits<T>::is_integer);
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
if constexpr (endian_type::endian == mpt::endian::big) {
typename std::make_unsigned<base_type>::type uval = val;
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
data[i] = static_cast<std::byte>((uval >> (8 * (sizeof(base_type) - 1 - i))) & 0xffu);
}
} else {
typename std::make_unsigned<base_type>::type uval = val;
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
data[i] = static_cast<std::byte>((uval >> (8 * i)) & 0xffu);
}
}
} else {
if constexpr (mpt::endian::native == mpt::endian::little || mpt::endian::native == mpt::endian::big) {
if constexpr (mpt::endian::native != endian_type::endian) {
val = mpt::SwapBytesImpl(val);
}
std::memcpy(data.data(), &val, sizeof(val));
} else {
using unsigned_base_type = typename std::make_unsigned<base_type>::type;
data = EndianEncode<unsigned_base_type, Tendian, sizeof(T)>(val);
}
}
}
MPT_CONSTEXPR20_FUN base_type get() const noexcept {
static_assert(std::numeric_limits<T>::is_integer);
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
if constexpr (endian_type::endian == mpt::endian::big) {
typename std::make_unsigned<base_type>::type uval = 0;
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
uval |= static_cast<typename std::make_unsigned<base_type>::type>(data[i]) << (8 * (sizeof(base_type) - 1 - i));
}
return static_cast<base_type>(uval);
} else {
typename std::make_unsigned<base_type>::type uval = 0;
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
uval |= static_cast<typename std::make_unsigned<base_type>::type>(data[i]) << (8 * i);
}
return static_cast<base_type>(uval);
}
} else {
if constexpr (mpt::endian::native == mpt::endian::little || mpt::endian::native == mpt::endian::big) {
base_type val = base_type();
std::memcpy(&val, data.data(), sizeof(val));
if constexpr (mpt::endian::native != endian_type::endian) {
val = mpt::SwapBytesImpl(val);
}
return val;
} else {
using unsigned_base_type = typename std::make_unsigned<base_type>::type;
return EndianDecode<unsigned_base_type, Tendian, sizeof(T)>(data);
}
}
}
MPT_CONSTEXPR20_FUN packed & operator=(const base_type & val) noexcept {
set(val);
return *this;
}
MPT_CONSTEXPR20_FUN operator base_type() const noexcept {
return get();
}
public:
MPT_CONSTEXPR20_FUN packed & operator&=(base_type val) noexcept {
set(get() & val);
return *this;
}
MPT_CONSTEXPR20_FUN packed & operator|=(base_type val) noexcept {
set(get() | val);
return *this;
}
MPT_CONSTEXPR20_FUN packed & operator^=(base_type val) noexcept {
set(get() ^ val);
return *this;
}
MPT_CONSTEXPR20_FUN packed & operator+=(base_type val) noexcept {
set(get() + val);
return *this;
}
MPT_CONSTEXPR20_FUN packed & operator-=(base_type val) noexcept {
set(get() - val);
return *this;
}
MPT_CONSTEXPR20_FUN packed & operator*=(base_type val) noexcept {
set(get() * val);
return *this;
}
MPT_CONSTEXPR20_FUN packed & operator/=(base_type val) noexcept {
set(get() / val);
return *this;
}
MPT_CONSTEXPR20_FUN packed & operator%=(base_type val) noexcept {
set(get() % val);
return *this;
}
MPT_CONSTEXPR20_FUN packed & operator++() noexcept { // prefix
set(get() + 1);
return *this;
}
MPT_CONSTEXPR20_FUN packed & operator--() noexcept { // prefix
set(get() - 1);
return *this;
}
MPT_CONSTEXPR20_FUN base_type operator++(int) noexcept { // postfix
base_type old = get();
set(old + 1);
return old;
}
MPT_CONSTEXPR20_FUN base_type operator--(int) noexcept { // postfix
base_type old = get();
set(old - 1);
return old;
}
};
using int64le = packed<int64, LittleEndian_tag>;
using int32le = packed<int32, LittleEndian_tag>;
using int16le = packed<int16, LittleEndian_tag>;
using int8le = packed<int8, LittleEndian_tag>;
using uint64le = packed<uint64, LittleEndian_tag>;
using uint32le = packed<uint32, LittleEndian_tag>;
using uint16le = packed<uint16, LittleEndian_tag>;
using uint8le = packed<uint8, LittleEndian_tag>;
using int64be = packed<int64, BigEndian_tag>;
using int32be = packed<int32, BigEndian_tag>;
using int16be = packed<int16, BigEndian_tag>;
using int8be = packed<int8, BigEndian_tag>;
using uint64be = packed<uint64, BigEndian_tag>;
using uint32be = packed<uint32, BigEndian_tag>;
using uint16be = packed<uint16, BigEndian_tag>;
using uint8be = packed<uint8, BigEndian_tag>;
constexpr bool declare_binary_safe(const int64le &) {
return true;
}
constexpr bool declare_binary_safe(const int32le &) {
return true;
}
constexpr bool declare_binary_safe(const int16le &) {
return true;
}
constexpr bool declare_binary_safe(const int8le &) {
return true;
}
constexpr bool declare_binary_safe(const uint64le &) {
return true;
}
constexpr bool declare_binary_safe(const uint32le &) {
return true;
}
constexpr bool declare_binary_safe(const uint16le &) {
return true;
}
constexpr bool declare_binary_safe(const uint8le &) {
return true;
}
constexpr bool declare_binary_safe(const int64be &) {
return true;
}
constexpr bool declare_binary_safe(const int32be &) {
return true;
}
constexpr bool declare_binary_safe(const int16be &) {
return true;
}
constexpr bool declare_binary_safe(const int8be &) {
return true;
}
constexpr bool declare_binary_safe(const uint64be &) {
return true;
}
constexpr bool declare_binary_safe(const uint32be &) {
return true;
}
constexpr bool declare_binary_safe(const uint16be &) {
return true;
}
constexpr bool declare_binary_safe(const uint8be &) {
return true;
}
static_assert(mpt::check_binary_size<int64le>(8));
static_assert(mpt::check_binary_size<int32le>(4));
static_assert(mpt::check_binary_size<int16le>(2));
static_assert(mpt::check_binary_size<int8le>(1));
static_assert(mpt::check_binary_size<uint64le>(8));
static_assert(mpt::check_binary_size<uint32le>(4));
static_assert(mpt::check_binary_size<uint16le>(2));
static_assert(mpt::check_binary_size<uint8le>(1));
static_assert(mpt::check_binary_size<int64be>(8));
static_assert(mpt::check_binary_size<int32be>(4));
static_assert(mpt::check_binary_size<int16be>(2));
static_assert(mpt::check_binary_size<int8be>(1));
static_assert(mpt::check_binary_size<uint64be>(8));
static_assert(mpt::check_binary_size<uint32be>(4));
static_assert(mpt::check_binary_size<uint16be>(2));
static_assert(mpt::check_binary_size<uint8be>(1));
template <typename T>
struct make_le {
using type = packed<typename std::remove_const<T>::type, LittleEndian_tag>;
};
template <typename T>
struct make_be {
using type = packed<typename std::remove_const<T>::type, BigEndian_tag>;
};
template <mpt::endian endian, typename T>
struct make_endian {
};
template <typename T>
struct make_endian<mpt::endian::little, T> {
using type = packed<typename std::remove_const<T>::type, LittleEndian_tag>;
};
template <typename T>
struct make_endian<mpt::endian::big, T> {
using type = packed<typename std::remove_const<T>::type, BigEndian_tag>;
};
template <typename T>
MPT_CONSTEXPR20_FUN auto as_le(T v) noexcept -> typename mpt::make_le<typename std::remove_const<T>::type>::type {
typename mpt::make_le<typename std::remove_const<T>::type>::type res{};
res = v;
return res;
}
template <typename T>
MPT_CONSTEXPR20_FUN auto as_be(T v) noexcept -> typename mpt::make_be<typename std::remove_const<T>::type>::type {
typename mpt::make_be<typename std::remove_const<T>::type>::type res{};
res = v;
return res;
}
template <typename Tpacked>
MPT_CONSTEXPR20_FUN Tpacked as_endian(typename Tpacked::base_type v) noexcept {
Tpacked res{};
res = v;
return res;
}
} // namespace MPT_INLINE_NS
} // namespace mpt
namespace std {
template <typename T, typename Tendian>
class numeric_limits<mpt::packed<T, Tendian>> : public std::numeric_limits<T> { };
template <typename T, typename Tendian>
class numeric_limits<const mpt::packed<T, Tendian>> : public std::numeric_limits<const T> { };
} // namespace std
#endif // MPT_ENDIAN_INTEGER_HPP
|