forked from sunflyer/vlmcsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endian.h
296 lines (203 loc) · 5.98 KB
/
endian.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
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
#ifndef __endian_h
#define __endian_h
#ifndef CONFIG
#define CONFIG "config.h"
#endif // CONFIG
#include CONFIG
//
// Unaligned access
//
#define UAA16(p, i) (((PACKED16*)p)->val[i])
#define UAA32(p, i) (((PACKED32*)p)->val[i])
#define UAA64(p, i) (((PACKED64*)p)->val[i])
#define UA64(p) UAA64(p, 0)
#define UA32(p) UAA32(p, 0)
#define UA16(p) UAA16(p, 0)
//
//Byteswap: Use compiler support if available
//
#ifdef __has_builtin // Clang supports this
#if __has_builtin(__builtin_bswap16)
#define BS16(x) __builtin_bswap16(x)
#endif
#if __has_builtin(__builtin_bswap32)
#define BS32(x) __builtin_bswap32(x)
#endif
#if __has_builtin(__builtin_bswap64)
#define BS64(x) __builtin_bswap64(x)
#endif
#endif // has_builtin
#ifdef __GNUC__ // GNU C >= 4.3 has bswap32 and bswap64. GNU C >= 4.8 also has bswap16
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)
#ifndef BS32
#define BS32(x) __builtin_bswap32(x)
#endif
#ifndef BS64
#define BS64(x) __builtin_bswap64(x)
#endif
#if (__GNUC__ > 4) || (__GNUC_MINOR__ > 7)
#ifndef BS16
#define BS16(x) __builtin_bswap16(x)
#endif
#endif // GNU C > 4.7
#endif // __GNUC__ > 4
#endif // __GNUC__
//
// Byteorder
//
#if defined(__linux__) || defined(__GLIBC__) || defined(__CYGWIN__)
#include <endian.h>
#include <byteswap.h>
#ifndef BS16
#define BS16(x) bswap_16(x)
#endif
#ifndef BS32
#define BS32(x) bswap_32(x)
#endif
#ifndef BS64
#define BS64(x) bswap_64(x)
#endif
#elif defined(__sun__)
#include <sys/byteorder.h>
#ifndef BS16
#define BS16(x) BSWAP_16(x)
#endif
#ifndef BS32
#define BS32(x) BSWAP_32(x)
#endif
#ifndef BS64
#define BS64(x) BSWAP_64(x)
#endif
#define __LITTLE_ENDIAN 1234
#define __BIG_ENDIAN 4321
#ifdef _LITTLE_ENDIAN
#define __BYTE_ORDER __LITTLE_ENDIAN
#else
#define __BYTE_ORDER __BIG_ENDIAN
#endif
#elif __minix__ || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
#include <sys/types.h>
#include <sys/endian.h>
#define __BYTE_ORDER _BYTE_ORDER
#define __LITTLE_ENDIAN _LITTLE_ENDIAN
#define __BIG_ENDIAN _BIG_ENDIAN
#ifdef __OpenBSD__
#ifndef BS16
#define BS16 swap16
#endif
#ifndef BS32
#define BS32 swap32
#endif
#ifndef BS64
#define BS64 swap64
#endif
#else // !__OpenBSD__
#ifndef BS16
#define BS16 bswap16
#endif
#ifndef BS32
#define BS32 bswap32
#endif
#ifndef BS64
#define BS64 bswap64
#endif
#endif // !__OpenBSD__
#elif defined(__APPLE__)
#include <sys/types.h>
#include <machine/endian.h>
#include <libkern/OSByteOrder.h>
#define __BYTE_ORDER _BYTE_ORDER
#define __LITTLE_ENDIAN _LITTLE_ENDIAN
#define __BIG_ENDIAN _BIG_ENDIAN
#ifndef BS16
#define BS16 OSSwapInt16
#endif
#ifndef BS32
#define BS32 OSSwapInt32
#endif
#ifndef BS64
#define BS64 OSSwapInt64
#endif
#elif defined(_WIN32)
#define __LITTLE_ENDIAN 1234
#define __BIG_ENDIAN 4321
#define __BYTE_ORDER __LITTLE_ENDIAN
#include <stdlib.h>
#ifndef BS16
#define BS16 _byteswap_ushort
#endif
#ifndef BS32
#define BS32 _byteswap_ulong
#endif
#ifndef BS64
#define BS64 _byteswap_uint64
#endif
#endif // Byteorder in different OS
#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && defined(__LITTLE_ENDIAN) \
&& defined(BS16) && defined(BS32) && defined(BS64)
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define __BE16(x) BS16(x)
#define __LE16(x) (x)
#define __BE32(x) BS32(x)
#define __LE32(x) (x)
#define __BE64(x) BS64(x)
#define __LE64(x) (x)
#else // __BYTE_ORDER == __BIG_ENDIAN
#define __BE16(x) (x)
#define __LE16(x) BS16(x)
#define __BE32(x) (x)
#define __LE32(x) BS32(x)
#define __BE64(x) (x)
#define __LE64(x) BS64(x)
#endif // __BYTE_ORDER
#define PUT_UAA64BE(p, v, i) ( UAA64(p, i) = __BE64(v) )
#define PUT_UAA32BE(p, v, i) ( UAA32(p, i) = __BE32(v) )
#define PUT_UAA16BE(p, v, i) ( UAA16(p, i) = __BE16(v) )
#define PUT_UAA64LE(p, v, i) ( UAA64(p, i) = __LE64(v) )
#define PUT_UAA32LE(p, v, i) ( UAA32(p, i) = __LE32(v) )
#define PUT_UAA16LE(p, v, i) ( UAA16(p, i) = __LE16(v) )
#define GET_UAA64BE(p, i) __BE64(UAA64(p, i))
#define GET_UAA32BE(p, i) __BE32(UAA32(p, i))
#define GET_UAA16BE(p, i) __BE16(UAA16(p, i))
#define GET_UAA64LE(p, i) __LE64(UAA64(p, i))
#define GET_UAA32LE(p, i) __LE32(UAA32(p, i))
#define GET_UAA16LE(p, i) __LE16(UAA16(p, i))
#define BE16(x) __BE16(x)
#define LE16(x) __LE16(x)
#define BE32(x) __BE32(x)
#define LE32(x) __LE32(x)
#define BE64(x) __BE64(x)
#define LE64(x) __LE64(x)
#else // ! defined(__BYTE_ORDER)
extern void PUT_UAA64BE(void *p, unsigned long long v, unsigned int i);
extern void PUT_UAA32BE(void *p, unsigned int v, unsigned int i);
extern void PUT_UAA16BE(void *p, unsigned short v, unsigned int i);
extern void PUT_UAA64LE(void *p, unsigned long long v, unsigned int i);
extern void PUT_UAA32LE(void *p, unsigned int v, unsigned int i);
extern void PUT_UAA16LE(void *p, unsigned short v, unsigned int i);
extern unsigned long long GET_UAA64BE(void *p, unsigned int i);
extern unsigned int GET_UAA32BE(void *p, unsigned int i);
extern unsigned short GET_UAA16BE(void *p, unsigned int i);
extern unsigned long long GET_UAA64LE(void *p, unsigned int i);
extern unsigned int GET_UAA32LE(void *p, unsigned int i);
extern unsigned short GET_UAA16LE(void *p, unsigned int i);
extern unsigned short BE16(unsigned short x);
extern unsigned short LE16(unsigned short x);
extern unsigned int BE32(unsigned int x);
extern unsigned int LE32(unsigned int x);
extern unsigned long long BE64(unsigned long long x);
extern unsigned long long LE64(unsigned long long x);
#endif // defined(__BYTE_ORDER)
#define PUT_UA64BE(p, v) PUT_UAA64BE(p, v, 0)
#define PUT_UA32BE(p, v) PUT_UAA32BE(p, v, 0)
#define PUT_UA16BE(p, v) PUT_UAA16BE(p, v, 0)
#define PUT_UA64LE(p, v) PUT_UAA64LE(p, v, 0)
#define PUT_UA32LE(p, v) PUT_UAA32LE(p, v, 0)
#define PUT_UA16LE(p, v) PUT_UAA16LE(p, v, 0)
#define GET_UA64BE(p) GET_UAA64BE(p, 0)
#define GET_UA32BE(p) GET_UAA32BE(p, 0)
#define GET_UA16BE(p) GET_UAA16BE(p, 0)
#define GET_UA64LE(p) GET_UAA64LE(p, 0)
#define GET_UA32LE(p) GET_UAA32LE(p, 0)
#define GET_UA16LE(p) GET_UAA16LE(p, 0)
#endif // __endian_h