-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathcommon.h
More file actions
54 lines (41 loc) · 1.39 KB
/
common.h
File metadata and controls
54 lines (41 loc) · 1.39 KB
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
#pragma once
#include "feature.h"
#define unlikely(x) __builtin_expect((x), 0)
#define likely(x) __builtin_expect((x), 1)
#define UNUSED __attribute__((unused))
#define MASK(n) (~((~0U << (n))))
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
/* Ensure that __builtin_clz is never called with 0 argument */
static inline int ilog2(int x)
{
return 31 - __builtin_clz(x | 1);
}
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
/* Range check
* For any variable range checking:
* if (x >= minx && x <= maxx) ...
* it is faster to use bit operation:
* if ((signed)((x - minx) | (maxx - x)) >= 0) ...
*/
#define RANGE_CHECK(x, minx, size) \
((int32_t) ((x - minx) | (minx + size - 1 - x)) >= 0)
/* Packed macro */
#if defined(__GNUC__) || defined(__clang__)
#define PACKED(name) name __attribute__((packed))
#elif defined(_MSC_VER)
#define PACKED(name) __pragma(pack(push, 1)) name __pragma(pack(pop))
#else /* unsupported compilers */
#define PACKED(name)
#endif
/* Pattern Matching for C macros.
* https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms
*/
/* In Visual Studio, __VA_ARGS__ is treated as a separate parameter. */
#define FIX_VC_BUG(x) x
/* catenate */
#define PRIMITIVE_CAT(a, ...) FIX_VC_BUG(a##__VA_ARGS__)
#define IIF(c) PRIMITIVE_CAT(IIF_, c)
/* run the 2nd parameter */
#define IIF_0(t, ...) __VA_ARGS__
/* run the 1st parameter */
#define IIF_1(t, ...) t