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
|
#ifndef _AL_MACROS_H
#define _AL_MACROS_H
#define AL_PASTER_EVALUATOR(X, Y) X##_##Y
#define AL_PASTER(X, Y) AL_PASTER_EVALUATOR(X, Y)
#if defined LIKELY || defined UNLIKELY
#error "Conflicting definitions for LIKELY/UNLIKELY"
#endif
#ifdef AL_HAVE_BUILTIN_EXPECT
#define LIKELY(x) __builtin_expect((x), 1)
#else
#define LIKELY(x) x
#endif
#ifdef AL_HAVE_BUILTIN_EXPECT
#define UNLIKELY(x) __builtin_expect((x), 0)
#else
#define UNLIKELY(x) x
#endif
// Keep in mind the idea of an unreachable path is not the same
// as "We should never be here". Unreachable is a compiler hint that means
// "It's logically impossible to be here".
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005funreachable
// https://github.com/yehuthi/c_unreachable/blob/1f672a573b2d45cf1479fbb771c17c9ee35bff6b/include/c_unreachable.h#L21C1-L43C7
#ifndef unreachable
#if defined(__GNUC__)
#define unreachable() (__builtin_unreachable())
#elif defined(_MSC_VER)
#define unreachable() (__assume(0))
#endif
#endif
#if defined MIN || defined MAX || defined CLAMP || defined SWAP || defined ARRAY_SIZE || defined STR || defined XSTR || defined BOOLSTR
#error "Conflicting definitions for common macros"
#endif
#ifdef AL_HAVE_GNU_EXTENSIONS
#define MIN(a, b) \
({ \
__typeof__(a) ac = a; \
__typeof__(b) bc = b; \
(void)(&ac == &bc); \
(ac < bc) ? ac : bc; \
})
#define MAX(a, b) \
({ \
__typeof__(a) ac = a; \
__typeof__(b) bc = b; \
(void)(&ac == &bc); \
(ac > bc) ? ac : bc; \
})
#define CLAMP(n, lo, hi) MIN(hi, MAX(lo, n))
#else
/*
#error "MIN/MAX/CLAMP should not be used in this mode as they\
differ in behavior without support for statement expressions"
*/
// Compatible/Pedantic min/max.
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define CLAMP(n, lo, hi) MIN(hi, MAX(lo, n))
#endif
#define SWAP(a, b) \
do { \
__typeof__(a) tmp = a; \
a = b; \
b = tmp; \
} while (0)
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define STR(s) #s
#define XSTR(s) STR(s)
#define BOOLSTR(b) ((b) ? "true" : "false")
#if defined KB || defined MB || defined GB || defined TB
#error "Conflicting definitions for byte size macros"
#endif
#define KB(n) ((n) << 10)
#define MB(n) ((n) << 20)
#define GB(n) ((n) << 30)
#define TB(n) (((u64)n) << 40)
//#if defined __counted_by || defined __sized_by
#if defined __sized_by
#error "Conflicting definitions for common attributes"
#endif
// https://github.com/google/double-conversion/pull/131/files
#ifdef __has_attribute
#define AL_HAS_ATTRIBUTE(x) __has_attribute(x)
#else
#define AL_HAS_ATTRIBUTE(x) 0
#endif
//#if AL_HAS_ATTRIBUTE(__counted_by__)
//#define __counted_by(member) __attribute__((__counted_by__(member)))
//#else
//#define __counted_by(member)
//#endif
#if AL_HAS_ATTRIBUTE(__sized_by__)
#define __sized_by(member) __attribute__((__sized_by__(member)))
#else
#define __sized_by(member)
#endif
#ifdef _MSC_VER
#define __thread __declspec(thread)
#endif
#endif // _AL_MACROS_H
|