-
Notifications
You must be signed in to change notification settings - Fork 0
/
endian.hpp
120 lines (106 loc) · 3.63 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
#ifndef DIGESTIVE_ENDIAN_HPP
#define DIGESTIVE_ENDIAN_HPP
#include <cstdint>
#ifndef _MSC_VER
#include <endian.h>
#endif
namespace digestive {
namespace detail {
using std::uint64_t;
using std::uint32_t;
enum class endian {
little,
big,
host,
network = big,
unknown
};
alignas (uint32_t) static constexpr const char testval[] = "\xAA\xBB\xCC\xDD";
static const uint32_t endianness_test_value
= *reinterpret_cast<const uint32_t*>(testval);
static const endian endianness =
endianness_test_value == 0xAABBCCDD
? endian::big
: endianness_test_value == 0xDDCCBBAA
? endian::little
: endian::unknown;
inline uint8_t endian_swap(uint8_t x)
{
return x;
}
inline uint16_t endian_swap(uint16_t x)
{
#if defined(__GNUC__) || defined(__clang__)
return __builtin_bswap16(x);
#elif defined(MSC_VER)
return _byteswap_uint16(x);
#else
return (x >> 8) | (x << 8);
#endif
}
inline uint32_t endian_swap(uint32_t x)
{
#if defined(__GNUC__) || defined(__clang__)
return __builtin_bswap32(x);
#elif defined(MSC_VER)
return _byteswap_uint32(x);
#else
return (x >> 24) |
((x >> 8) & 0x0000FF00ULL) |
((x << 8) & 0x00FF0000ULL) |
(x << 24);
#endif
}
inline uint64_t endian_swap(uint64_t x)
{
#if defined(__GNUC__) || defined(__clang__)
return __builtin_bswap64(x);
#elif defined(MSC_VER)
return _byteswap_uint64(x);
#else
return (x >> 56) |
((x >> 40) & 0x000000000000FF00ULL) |
((x >> 24) & 0x0000000000FF0000ULL) |
((x >> 8) & 0x00000000FF000000ULL) |
((x << 8) & 0x000000FF00000000ULL) |
((x << 24) & 0x0000FF0000000000ULL) |
((x << 40) & 0x00FF000000000000ULL) |
(x << 56);
#endif
}
template <endian From, endian To>
struct endian_converter {};
inline uint64_t endian_convert(uint64_t);
template <> struct endian_converter<endian::host, endian::little> {
template <typename T> inline T operator()(T x)
{ return (endianness == endian::little) ? x : endian_swap(x); } };
template <> struct endian_converter<endian::host, endian::big > {
template <typename T> inline T operator()(T x)
{ return (endianness == endian::big) ? x : endian_swap(x); } };
template <> struct endian_converter<endian::little, endian::host > {
template <typename T> inline T operator()(T x)
{ return (endianness == endian::little) ? x : endian_swap(x); } };
template <> struct endian_converter<endian::big, endian::host > {
template <typename T> inline T operator()(T x)
{ return (endianness == endian::big) ? x : endian_swap(x); } };
template <> struct endian_converter<endian::big, endian::little> {
template <typename T> inline T operator()(T x)
{ return endian_swap(x); } };
template <> struct endian_converter<endian::little, endian::big > {
template <typename T> inline T operator()(T x)
{ return endian_swap(x); } };
template <typename T> inline T endian_host_to_little(T x)
{ return endian_converter<endian::host, endian::little>()(x); }
template <typename T> inline T endian_host_to_big (T x)
{ return endian_converter<endian::host, endian::big >()(x); }
template <typename T> inline T endian_little_to_host(T x)
{ return endian_converter<endian::little, endian::host >()(x); }
template <typename T> inline T endian_big_to_host (T x)
{ return endian_converter<endian::big, endian::host >()(x); }
template <typename T> inline T endian_big_to_little (T x)
{ return endian_converter<endian::big, endian::little>()(x); }
template <typename T> inline T endian_little_to_big (T x)
{ return endian_converter<endian::little, endian::big >()(x); }
} // namespace detail
} // namespace digestive
#endif // DIGESTIVE_ENDIAN_HPP