-
Notifications
You must be signed in to change notification settings - Fork 19
/
hton.h
241 lines (186 loc) · 4.75 KB
/
hton.h
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
#include <stdint.h>
#if defined(__linux__) || defined(__CYGWIN__) || defined(__sun)
#include <endian.h>
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) \
|| defined(__DragonFly__)
#include <sys/endian.h>
#elif defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#elif defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
/* Assume Windows is always LE. There seems to be no reliable way
to detect endianness there */
#define __LITTLE_ENDIAN 1234
#define __BIG_ENDIAN 4321
#define __BYTE_ORDER __LITTLE_ENDIAN
#endif
#if defined(_BYTE_ORDER) && !defined(__BYTE_ORDER)
#define __BYTE_ORDER _BYTE_ORDER
#endif
#if defined(BYTE_ORDER) && !defined(__BYTE_ORDER)
#define __BYTE_ORDER BYTE_ORDER
#endif
#if defined(_LITTLE_ENDIAN) && !defined(__LITTLE_ENDIAN)
#define __LITTLE_ENDIAN _LITTLE_ENDIAN
#endif
#if defined(LITTLE_ENDIAN) && !defined(__LITTLE_ENDIAN)
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#endif
#if defined(_BIG_ENDIAN) && !defined(__BIG_ENDIAN)
#define __BIG_ENDIAN _BIG_ENDIAN
#endif
#if defined(BIG_ENDIAN) && !defined(__BIG_ENDIAN)
#define __BIG_ENDIAN BIG_ENDIAN
#endif
#if !defined(__BYTE_ORDER) || !defined(__LITTLE_ENDIAN) \
|| !defined(__BIG_ENDIAN)
#error Cannot determine platform byte order.
#endif
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
#define apg_bswap16(x) __builtin_bswap16(x)
#define apg_bswap32(x) __builtin_bswap32(x)
#define apg_bswap64(x) __builtin_bswap64(x)
#elif defined(_MSC_VER)
#define apg_bswap16(x) _byteswap_ushort(x)
#define apg_bswap32(x) _byteswap_ulong(x)
#define apg_bswap64(x) _byteswap_uint64(x)
#else
static inline uint16_t
apg_bswap16(uint16_t)
{
return ((x << 8) & 0xff00) | (x >> 8) & 0x00ff));
}
static inline uint32_t
apg_bswap32(uint32_t x)
{
return (
((x << 24) & 0xff000000) | ((x << 8) & 0x00ff0000) |
((x >> 8) & 0x0000ff00) | ((x >> 24) & 0x000000ff)
);
}
static inline uint64_t
apg_bswap64(uint64_t x)
{
return (
((x << 56) & 0xff00000000000000ULL) |
((x << 40) & 0x00ff000000000000ULL) |
((x << 24) & 0x0000ff0000000000ULL) |
((x << 8) & 0x000000ff00000000ULL) |
((x >> 8) & 0x00000000ff000000ULL) |
((x >> 24) & 0x0000000000ff0000ULL) |
((x >> 40) & 0x000000000000ff00ULL) |
((x >> 56) & 0x00000000000000ffULL);
);
}
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
#define apg_hton16(x) (x)
#define apg_hton32(x) (x)
#define apg_hton64(x) (x)
#define apg_ntoh16(x) (x)
#define apg_ntoh32(x) (x)
#define apg_ntoh64(x) (x)
#elif __BYTE_ORDER == __LITTLE_ENDIAN
#define apg_hton16(x) apg_bswap16(x)
#define apg_hton32(x) apg_bswap32(x)
#define apg_hton64(x) apg_bswap64(x)
#define apg_ntoh16(x) apg_bswap16(x)
#define apg_ntoh32(x) apg_bswap32(x)
#define apg_ntoh64(x) apg_bswap64(x)
#else
#error Unsupported byte order.
#endif
static inline void
pack_int16(char *buf, int16_t x)
{
uint16_t nx = apg_hton16((uint16_t)x);
/* NOTE: the memcpy below is _important_ to support systems
which disallow unaligned access. On systems, which do
allow unaligned access it will be optimized away by the
compiler
*/
memcpy(buf, &nx, sizeof(uint16_t));
}
static inline void
pack_int32(char *buf, int64_t x)
{
uint32_t nx = apg_hton32((uint32_t)x);
memcpy(buf, &nx, sizeof(uint32_t));
}
static inline void
pack_int64(char *buf, int64_t x)
{
uint64_t nx = apg_hton64((uint64_t)x);
memcpy(buf, &nx, sizeof(uint64_t));
}
static inline uint16_t
unpack_uint16(const char *buf)
{
uint16_t nx;
memcpy((char *)&nx, buf, sizeof(uint16_t));
return apg_ntoh16(nx);
}
static inline int16_t
unpack_int16(const char *buf)
{
return (int16_t)unpack_uint16(buf);
}
static inline uint32_t
unpack_uint32(const char *buf)
{
uint32_t nx;
memcpy((char *)&nx, buf, sizeof(uint32_t));
return apg_ntoh32(nx);
}
static inline int32_t
unpack_int32(const char *buf)
{
return (int32_t)unpack_uint32(buf);
}
static inline uint64_t
unpack_uint64(const char *buf)
{
uint64_t nx;
memcpy((char *)&nx, buf, sizeof(uint64_t));
return apg_ntoh64(nx);
}
static inline int64_t
unpack_int64(const char *buf)
{
return (int64_t)unpack_uint64(buf);
}
union _apg_floatconv {
uint32_t i;
float f;
};
union _apg_doubleconv {
uint64_t i;
double f;
};
static inline void
pack_float(char *buf, float f)
{
union _apg_floatconv v;
v.f = f;
pack_int32(buf, (int32_t)v.i);
}
static inline void
pack_double(char *buf, double f)
{
union _apg_doubleconv v;
v.f = f;
pack_int64(buf, (int64_t)v.i);
}
static inline float
unpack_float(const char *buf)
{
union _apg_floatconv v;
v.i = (uint32_t)unpack_int32(buf);
return v.f;
}
static inline double
unpack_double(const char *buf)
{
union _apg_doubleconv v;
v.i = (uint64_t)unpack_int64(buf);
return v.f;
}