-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathx.h
333 lines (279 loc) · 7.44 KB
/
x.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
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
#ifndef __X_H__
#define __X_H__
#ifdef __cplusplus
extern "C" {
#endif
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <unistd.h>
#include <libusb.h>
/*
* byteorder
*/
static inline uint16_t __swab16(uint16_t x)
{
return ((x << 8) | ( x>> 8));
}
static inline uint32_t __swab32(uint32_t x)
{
return ((x << 24) | (x >> 24) | \
((x & (uint32_t)0x0000ff00UL)<<8) | \
((x & (uint32_t)0x00ff0000UL)>>8));
}
static inline uint64_t __swab64(uint64_t x)
{
return ((x << 56) | (x >> 56) | \
((x & (uint64_t)0x000000000000ff00ULL)<<40) | \
((x & (uint64_t)0x0000000000ff0000ULL)<<24) | \
((x & (uint64_t)0x00000000ff000000ULL)<< 8) | \
((x & (uint64_t)0x000000ff00000000ULL)>> 8) | \
((x & (uint64_t)0x0000ff0000000000ULL)>>24) | \
((x & (uint64_t)0x00ff000000000000ULL)>>40));
}
static inline uint32_t __swahw32(uint32_t x)
{
return (((x & (uint32_t)0x0000ffffUL) << 16) | ((x & (uint32_t)0xffff0000UL) >> 16));
}
static inline uint32_t __swahb32(uint32_t x)
{
return (((x & (uint32_t)0x00ff00ffUL) << 8) | ((x & (uint32_t)0xff00ff00UL) >> 8));
}
static inline int cpu_is_big_endian(void)
{
const uint16_t endian = 256;
return (*(const uint8_t *)&endian) ? 1 : 0;
}
static inline uint64_t cpu_to_le64(uint64_t x)
{
if(cpu_is_big_endian())
return __swab64(x);
return x;
}
static inline uint64_t le64_to_cpu(uint64_t x)
{
if(cpu_is_big_endian())
return __swab64(x);
return x;
}
static inline uint32_t cpu_to_le32(uint32_t x)
{
if(cpu_is_big_endian())
return __swab32(x);
return x;
}
static inline uint32_t le32_to_cpu(uint32_t x)
{
if(cpu_is_big_endian())
return __swab32(x);
return x;
}
static inline uint16_t cpu_to_le16(uint16_t x)
{
if(cpu_is_big_endian())
return __swab16(x);
return x;
}
static inline uint16_t le16_to_cpu(uint16_t x)
{
if(cpu_is_big_endian())
return __swab16(x);
return x;
}
static inline uint64_t cpu_to_be64(uint64_t x)
{
if(cpu_is_big_endian())
return x;
return __swab64(x);
}
static inline uint64_t be64_to_cpu(uint64_t x)
{
if(cpu_is_big_endian())
return x;
return __swab64(x);
}
static inline uint32_t cpu_to_be32(uint32_t x)
{
if(cpu_is_big_endian())
return x;
return __swab32(x);
}
static inline uint32_t be32_to_cpu(uint32_t x)
{
if(cpu_is_big_endian())
return x;
return __swab32(x);
}
static inline uint16_t cpu_to_be16(uint16_t x)
{
if(cpu_is_big_endian())
return x;
return __swab16(x);
}
static inline uint16_t be16_to_cpu(uint16_t x)
{
if(cpu_is_big_endian())
return x;
return __swab16(x);
}
/*
* Get unaligned
*/
static inline uint16_t __get_unaligned_le16(const uint8_t * p)
{
return (p[0] << 0) | (p[1] << 8);
}
static inline uint16_t __get_unaligned_be16(const uint8_t * p)
{
return (p[0] << 8) | (p[1] << 0);
}
static inline uint32_t __get_unaligned_le32(const uint8_t * p)
{
return (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}
static inline uint32_t __get_unaligned_be32(const uint8_t * p)
{
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
}
static inline uint64_t __get_unaligned_le64(const uint8_t * p)
{
return ((uint64_t)__get_unaligned_le32(p + 4) << 32) | __get_unaligned_le32(p);
}
static inline uint64_t __get_unaligned_be64(const uint8_t * p)
{
return ((uint64_t)__get_unaligned_be32(p) << 32) | __get_unaligned_be32(p + 4);
}
static inline uint16_t get_unaligned_le16(const void * p)
{
return __get_unaligned_le16((const uint8_t *)p);
}
static inline uint16_t get_unaligned_be16(const void * p)
{
return __get_unaligned_be16((const uint8_t *)p);
}
static inline uint32_t get_unaligned_le32(const void * p)
{
return __get_unaligned_le32((const uint8_t *)p);
}
static inline uint32_t get_unaligned_be32(const void * p)
{
return __get_unaligned_be32((const uint8_t *)p);
}
static inline uint64_t get_unaligned_le64(const void * p)
{
return __get_unaligned_le64((const uint8_t *)p);
}
static inline uint64_t get_unaligned_be64(const void * p)
{
return __get_unaligned_be64((const uint8_t *)p);
}
/*
* Put unaligned
*/
static inline void __put_unaligned_le16(uint8_t * p, uint16_t val)
{
*p++ = val;
*p++ = val >> 8;
}
static inline void __put_unaligned_be16(uint8_t * p, uint16_t val)
{
*p++ = val >> 8;
*p++ = val;
}
static inline void __put_unaligned_le32(uint8_t * p, uint32_t val)
{
__put_unaligned_le16(p + 2, val >> 16);
__put_unaligned_le16(p, val);
}
static inline void __put_unaligned_be32(uint8_t * p, uint32_t val)
{
__put_unaligned_be16(p, val >> 16);
__put_unaligned_be16(p + 2, val);
}
static inline void __put_unaligned_le64(uint8_t * p, uint64_t val)
{
__put_unaligned_le32(p + 4, val >> 32);
__put_unaligned_le32(p, val);
}
static inline void __put_unaligned_be64(uint8_t * p, uint64_t val)
{
__put_unaligned_be32(p, val >> 32);
__put_unaligned_be32(p + 4, val);
}
static inline void put_unaligned_le16(void * p, uint16_t val)
{
__put_unaligned_le16((uint8_t *)p, val);
}
static inline void put_unaligned_be16(void * p, uint16_t val)
{
__put_unaligned_be16((uint8_t *)p, val);
}
static inline void put_unaligned_le32(void * p, uint32_t val)
{
__put_unaligned_le32((uint8_t *)p, val);
}
static inline void put_unaligned_be32(void * p, uint32_t val)
{
__put_unaligned_be32((uint8_t *)p, val);
}
static inline void put_unaligned_le64(void * p, uint64_t val)
{
__put_unaligned_le64((uint8_t *)p, val);
}
static inline void put_unaligned_be64(void * p, uint64_t val)
{
__put_unaligned_be64((uint8_t *)p, val);
}
/*
* xdef
*/
#if !(defined(NULL))
#if defined(__cplusplus)
#define NULL (0)
#else
#define NULL ((void *)0)
#endif
#endif
#if !(defined(offsetof))
#if (defined(__GNUC__) && (__GNUC__ >= 4))
#define offsetof(type, member) __builtin_offsetof(type, member)
#else
#define offsetof(type, field) ((size_t)(&((type *)0)->field))
#endif
#endif
#if !(defined(container_of))
#define container_of(ptr, type, member) ({const typeof(((type *)0)->member) *__mptr = (ptr); (type *)((char *)__mptr - offsetof(type,member));})
#endif
#if !(defined(likely))
#if (defined(__GNUC__) && (__GNUC__ >= 3))
#define likely(expr) (__builtin_expect(!!(expr), 1))
#else
#define likely(expr) (!!(expr))
#endif
#endif
#if !(defined(unlikely))
#if (defined(__GNUC__) && (__GNUC__ >= 3))
#define unlikely(expr) (__builtin_expect(!!(expr), 0))
#else
#define unlikely(expr) (!!(expr))
#endif
#endif
#define XMAP(x, ia, ib, oa, ob) ((x - ia) * (ob - oa) / (ib - ia) + oa)
#define XMIN(a, b) ({typeof(a) _amin = (a); typeof(b) _bmin = (b); (void)(&_amin == &_bmin); _amin < _bmin ? _amin : _bmin;})
#define XMAX(a, b) ({typeof(a) _amax = (a); typeof(b) _bmax = (b); (void)(&_amax == &_bmax); _amax > _bmax ? _amax : _bmax;})
#define XCLAMP(v, a, b) XMIN(XMAX(a, v), b)
#define XFLOOR(x) ((x) > 0 ? (int)(x) : (int)((x) - 0.9999999999))
#define XROUND(x) ((x) > 0 ? (int)((x) + 0.5) : (int)((x) - 0.5))
#define XCEIL(x) ((x) > 0 ? (int)((x) + 0.9999999999) : (int)(x))
#define XDIV255(x) ((((int)(x) + 1) * 257) >> 16)
#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
#define X(...) ("" #__VA_ARGS__ "")
#ifdef __cplusplus
}
#endif
#endif /* __X_H__ */