-
Notifications
You must be signed in to change notification settings - Fork 49
/
common.h
36 lines (28 loc) · 940 Bytes
/
common.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
#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);
}
/* 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