-
Notifications
You must be signed in to change notification settings - Fork 1
/
endian.hpp
464 lines (393 loc) · 11.3 KB
/
endian.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
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
/*
endian.hpp: Simple header-only endianness support library for C++
Copyright (C) 2016-2018 Ivan G. / [email protected]
This file may be modified and distributed under the terms of the MIT license:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// std::le_t<T,A> -- Little Endian
// std::be_t<T,A> -- Big Endian
// std::endian_base<T,A,Native> -- LE/BE implementation (selected by `Native`)
// First template argument is the underlying type (base: arithmetic or enum).
// Second optional template argument is explicit alignment (native by default).
// Alignment may be increased and decreased.
// Setting greater alignment works similar to alignas() and isn't very useful.
// Setting small alignment (especially 1) is the alternative to `#pragma pack`.
// std::be_t<std::uint32_t> -- big endian uint32_t
// std::le_t<std::uint32_t> -- little endian uint32_t
// std::be_t<std::uint32_t, 2> -- big endian uint32_t with enforced alignment 2 (packing)
#pragma once
#include <type_traits>
#include <cstdint>
#include <cstring>
namespace std
{
// Class proposed in https://howardhinnant.github.io/endian.html
enum class endian
{
little,
big,
// Detection from http://stackoverflow.com/questions/4239993/determining-endianness-at-compile-time
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
defined(__BIG_ENDIAN__) || \
defined(__ARMEB__) || \
defined(__THUMBEB__) || \
defined(__AARCH64EB__) || \
defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
native = big,
#ifndef __BIG_ENDIAN__
#define __BIG_ENDIAN__
#endif
#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
defined(__LITTLE_ENDIAN__) || \
defined(__ARMEL__) || \
defined(__THUMBEL__) || \
defined(__AARCH64EL__) || \
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || \
defined(_M_IX86) || defined(_M_X64) || defined(_M_IA64) || defined(_M_ARM)
native = little,
#ifndef __LITTLE_ENDIAN__
#define __LITTLE_ENDIAN__
#endif
#else
#error "Unknown endianness"
#endif
};
template <typename T>
struct has_endianness : std::integral_constant<bool, std::is_arithmetic<T>::value || std::is_enum<T>::value>
{
};
namespace detail
{
using uchar = unsigned char;
// Copy with byteswap (fallback algorithm)
template <std::size_t Size>
inline void revert(uchar* dst, const uchar* src)
{
for (std::size_t i = 0; i < Size; i++)
{
dst[i] = src[Size - 1 - i];
}
}
template <typename T, std::size_t Size = sizeof(T), std::size_t Align = 1>
struct alignas(Align) endian_buffer
{
using type = endian_buffer;
static constexpr bool can_opt = (Size == 2 || Size == 4 || Size == 8) && Align != Size;
static inline void put_re(type& dst, const T& src)
{
if (can_opt)
{
endian_buffer<T, Size, Size> buf_opt;
buf_opt.put_re(buf_opt, src);
std::memcpy(dst.data, &buf_opt, Size);
return;
}
revert<Size>(dst.data, reinterpret_cast<const uchar*>(&src));
}
static inline T get_re(const type& src)
{
if (can_opt)
{
endian_buffer<T, Size, Size> buf_opt;
std::memcpy(&buf_opt, src.data, Size);
return buf_opt.get_re(buf_opt);
}
T dst;
revert<Size>(reinterpret_cast<uchar*>(&dst), src.data);
return dst;
}
static inline void put_ne(type& dst, const T& src)
{
std::memcpy(dst.data, reinterpret_cast<const uchar*>(&src), Size);
}
static inline T get_ne(const type& src)
{
T dst;
std::memcpy(reinterpret_cast<uchar*>(&dst), src.data, Size);
return dst;
}
uchar data[Size];
};
// Optimization helper (B: storage type; Base: CRTP)
template <typename T, typename B, typename Base>
struct endian_buffer_opt
{
static inline void put_re(B& dst, const T& src)
{
dst = Base::swap(reinterpret_cast<const B&>(src));
}
static inline T get_re(const B& src)
{
const B value = Base::swap(src);
return reinterpret_cast<const T&>(value);
}
static inline void put_ne(B& dst, const T& src)
{
dst = reinterpret_cast<const B&>(src);
}
static inline T get_ne(const B& src)
{
return reinterpret_cast<const T&>(src);
}
operator const B&() const
{
return data;
}
operator B&()
{
return data;
}
B data;
};
#if defined(_MSC_VER) || defined(__GNUG__)
// Optional optimization (may be removed)
template <typename T>
struct endian_buffer<T, 2, 2> : endian_buffer_opt<T, std::uint16_t, endian_buffer<T, 2, 2>>
{
static_assert(alignof(std::uint16_t) == 2, "Unexpected std::uint16_t alignment");
using type = std::uint16_t;
static inline type swap(type src)
{
#if defined(__GNUG__)
return __builtin_bswap16(src);
#else
return _byteswap_ushort(src);
#endif
}
};
// Optional optimization (may be removed)
template <typename T>
struct endian_buffer<T, 4, 4> : endian_buffer_opt<T, std::uint32_t, endian_buffer<T, 4, 4>>
{
static_assert(alignof(std::uint32_t) == 4, "Unexpected std::uint32_t alignment");
using type = std::uint32_t;
static inline type swap(type src)
{
#if defined(__GNUG__)
return __builtin_bswap32(src);
#else
return _byteswap_ulong(src);
#endif
}
};
// Optional optimization (may be removed)
template <typename T>
struct endian_buffer<T, 8, 8> : endian_buffer_opt<T, std::uint64_t, endian_buffer<T, 8, 8>>
{
static_assert(alignof(std::uint64_t) == 8, "Unexpected std::uint64_t alignment");
using type = std::uint64_t;
static inline type swap(type src)
{
#if defined(__GNUG__)
return __builtin_bswap64(src);
#else
return _byteswap_uint64(src);
#endif
}
};
#endif
}
#ifdef __BIG_ENDIAN__
#define LE_STORE put_re
#define LE_LOAD get_re
#define BE_STORE put_ne
#define BE_LOAD get_ne
#else
#define LE_STORE put_ne
#define LE_LOAD get_ne
#define BE_STORE put_re
#define BE_LOAD get_re
#endif
template <typename T>
void le_store(void* dst, const T& value)
{
static_assert(has_endianness<T>::value, "le_store<>: invalid type");
using buf = detail::endian_buffer<T>;
buf::LE_STORE(*static_cast<typename buf::type*>(dst), value);
}
template <typename T>
void le_load(T& value, const void* src)
{
static_assert(has_endianness<T>::value, "le_load<>: invalid type");
using buf = detail::endian_buffer<T>;
value = buf::LE_LOAD(*static_cast<const typename buf::type*>(src));
}
template <typename T>
T le_load(const void* src)
{
static_assert(has_endianness<T>::value, "le_load<>: invalid type");
using buf = detail::endian_buffer<T>;
return buf::LE_LOAD(*static_cast<const typename buf::type*>(src));
}
template <typename T>
void be_store(void* dst, const T& value)
{
static_assert(has_endianness<T>::value, "be_store<>: invalid type");
using buf = detail::endian_buffer<T>;
buf::BE_STORE(*static_cast<typename buf::type*>(dst), value);
}
template <typename T>
void be_load(T& value, const void* src)
{
static_assert(has_endianness<T>::value, "be_load<>: invalid type");
using buf = detail::endian_buffer<T>;
value = buf::BE_LOAD(*static_cast<const typename buf::type*>(src));
}
template <typename T>
T be_load(const void* src)
{
static_assert(has_endianness<T>::value, "be_load<>: invalid type");
using buf = detail::endian_buffer<T>;
return buf::BE_LOAD(*static_cast<const typename buf::type*>(src));
}
#undef LE_STORE
#undef LE_LOAD
#undef BE_STORE
#undef BE_LOAD
// Endianness support type
template <typename T, std::size_t Align, bool Native>
class endian_base
{
static_assert(has_endianness<T>::value, "endian_base<>: invalid type");
using buf = detail::endian_buffer<T, sizeof(T), Align>;
using data_t = typename buf::type;
data_t data;
public:
using value_type = T;
endian_base() = default;
endian_base(const T& value)
{
Native ? buf::put_ne(data, value) : buf::put_re(data, value);
}
endian_base& operator=(const endian_base&) = default;
endian_base& operator=(const T& value)
{
Native ? buf::put_ne(data, value) : buf::put_re(data, value);
return *this;
}
operator T() const
{
return Native ? buf::get_ne(data) : buf::get_re(data);
}
T get() const
{
return Native ? buf::get_ne(data) : buf::get_re(data);
}
auto operator++(int)
{
auto val = get();
auto result = val++;
*this = val;
return result; // Forward
}
auto operator--(int)
{
auto val = get();
auto result = val--;
*this = val;
return result; // Forward
}
endian_base& operator++()
{
auto val = get();
++val;
return (*this = val);
}
endian_base& operator--()
{
auto val = get();
--val;
return (*this = val);
}
template <typename T2>
endian_base& operator+=(T2&& rhs)
{
auto val = get();
val += std::forward<T2>(rhs);
return (*this = val);
}
template <typename T2>
endian_base& operator-=(T2&& rhs)
{
auto val = get();
val -= std::forward<T2>(rhs);
return (*this = val);
}
template <typename T2>
endian_base& operator*=(T2&& rhs)
{
auto val = get();
val *= std::forward<T2>(rhs);
return (*this = val);
}
template <typename T2>
endian_base& operator/=(T2&& rhs)
{
auto val = get();
val /= std::forward<T2>(rhs);
return (*this = val);
}
template <typename T2>
endian_base& operator%=(T2&& rhs)
{
auto val = get();
val %= std::forward<T2>(rhs);
return (*this = val);
}
template <typename T2>
endian_base& operator&=(T2&& rhs)
{
auto val = get();
val &= std::forward<T2>(rhs);
return (*this = val);
}
template <typename T2>
endian_base& operator|=(T2&& rhs)
{
auto val = get();
val |= std::forward<T2>(rhs);
return (*this = val);
}
template <typename T2>
endian_base& operator^=(T2&& rhs)
{
auto val = get();
val ^= std::forward<T2>(rhs);
return (*this = val);
}
template <typename T2>
endian_base& operator<<=(T2&& rhs)
{
auto val = get();
val <<= std::forward<T2>(rhs);
return (*this = val);
}
template <typename T2>
endian_base& operator>>=(T2&& rhs)
{
auto val = get();
val >>= std::forward<T2>(rhs);
return (*this = val);
}
};
template <typename T, std::size_t A = alignof(T)>
using le_t = endian_base<T, A, endian::native == endian::little>;
template <typename T, std::size_t A = alignof(T)>
using be_t = endian_base<T, A, endian::native == endian::big>;
}