forked from user1095108/generic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap.hpp
225 lines (190 loc) · 3.77 KB
/
swap.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
#ifndef GNR_SWAP_HPP
# define GNR_SWAP_HPP
# pragma once
#include <cstdint>
#include <cstring>
#include <type_traits>
#if defined(_MSC_VER)
# include <stdlib.h>
#endif // _MSC_VER
namespace gnr
{
template <class Dst, class Src>
inline Dst bit_cast(Src const src) noexcept
{
Dst dst;
std::memcpy(&dst, &src, sizeof(dst));
return dst;
}
template <class Dst, class Src>
inline Dst bit_cast(Src* const src) noexcept
{
Dst dst;
std::memcpy(&dst, src, sizeof(dst));
return dst;
}
#if defined(__GNUC__)
template <typename T>
constexpr inline std::enable_if_t<
std::is_integral<T>{} &&
(sizeof(std::int64_t) == sizeof(T)),
T
>
swap(T const v) noexcept
{
return __builtin_bswap64(v);
}
template <typename T>
constexpr inline std::enable_if_t<
std::is_integral<T>{} &&
(sizeof(std::int32_t) == sizeof(T)),
T
>
swap(T const v) noexcept
{
return __builtin_bswap32(v);
}
template <typename T>
constexpr inline std::enable_if_t<
std::is_integral<T>{} &&
(sizeof(std::int16_t) == sizeof(T)),
T
>
swap(T const v) noexcept
{
return __builtin_bswap16(v);
}
template <typename T>
constexpr inline std::enable_if_t<
std::is_integral<T>{} &&
(sizeof(std::int8_t) == sizeof(T)),
T
>
swap(T const v) noexcept
{
return v;
}
#elif defined(_MSC_VER)
template <typename T>
constexpr inline std::enable_if_t<
std::is_integral<T>{} &&
(sizeof(__int64) == sizeof(T)),
T
>
swap(T const v) noexcept
{
return _byteswap_uint64(i);
}
template <typename T>
constexpr inline std::enable_if_t<
std::is_integral<T>{} &&
(sizeof(unsigned long) == sizeof(T)),
T
>
swap(T const v) noexcept
{
return _byteswap_ulong(v);
}
template <typename T>
constexpr inline std::enable_if_t<
std::is_integral<T>{} &&
(sizeof(unsigned short) == sizeof(T)),
T
>
swap(T const v) noexcept
{
return _byteswap_ushort(v)
}
template <typename T>
constexpr inline std::enable_if_t<
std::is_integral<T>{} &&
(sizeof(std::int8_t) == sizeof(T)),
T
>
swap(T const v) noexcept
{
return v;
}
#else
namespace
{
template<class T, std::size_t ...I>
constexpr inline T swap_impl(T v, std::index_sequence<I...> const) noexcept
{
return (
(
((v >> (I * std::numeric_limits<char>::digits)) & std::uint8_t(~0)) <<
((sizeof(T) - 1 - I) * std::numeric_limits<char>::digits)
) | ...
);
};
}
template <typename T>
constexpr inline std::enable_if_t<
std::is_integral<T>{},
T
>
swap(T const v) noexcept
{
return swap_impl<T>(v, std::make_index_sequence<sizeof(T)>{});
}
#endif
#if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) || \
(defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)) || \
(defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)) || \
defined(__ARMEB__) || \
defined(__THUMBEB__) || \
defined(__AARCH64EB__) || \
defined(_MIPSEB) || \
defined(__MIPSEB) || \
defined(__MIPSEB__)
template <typename T>
constexpr inline std::enable_if_t<
std::is_integral<T>{},
T
>
from_be(T const i) noexcept
{
return i;
}
template <typename T>
inline std::enable_if_t<
std::is_integral<T>{},
T
>
from_le(T const i) noexcept
{
return swap(i);
}
#elif (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
(defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
(defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)) || \
defined(__ARMEL__) || \
defined(__THUMBEL__) || \
defined(__AARCH64EL__) || \
defined(_MIPSEL) || \
defined(__MIPSEL) || \
defined(__MIPSEL__)
template <typename T>
constexpr inline std::enable_if_t<
std::is_integral<T>{},
T
>
from_le(T const i) noexcept
{
return i;
}
template <typename T>
inline std::enable_if_t<
std::is_integral<T>{},
T
>
from_be(T const i) noexcept
{
return swap(i);
}
#else
#error "I don't know what architecture this is!"
#endif
}
#endif // GNR_SWAP_HPP