-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathcommon.h
263 lines (227 loc) · 7.53 KB
/
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
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
/*
* rv32emu is freely redistributable under the MIT License. See the file
* "LICENSE" for information on usage and redistribution of this file.
*/
#pragma once
#include <assert.h>
#include <stdint.h>
#include "feature.h"
#include "log.h"
#if defined(__GNUC__) || defined(__clang__)
#define UNUSED __attribute__((unused))
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define FORCE_INLINE static inline __attribute__((always_inline))
#else
#define UNUSED
#define likely(x) (x)
#define unlikely(x) (x)
#if defined(_MSC_VER)
#define FORCE_INLINE static inline __forceinline
#else
#define FORCE_INLINE static inline
#endif
#endif
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
#define MASK(n) (~((~0U << (n))))
#if defined(_MSC_VER)
#include <intrin.h>
static inline int rv_clz(uint32_t v)
{
/* 0 is considered as undefined behavior */
assert(v);
uint32_t leading_zero = 0;
_BitScanReverse(&leading_zero, v);
return 31 - leading_zero;
}
#elif defined(__GNUC__) || defined(__clang__)
static inline int rv_clz(uint32_t v)
{
/* https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html */
/* 0 is considered as undefined behavior */
assert(v);
return __builtin_clz(v);
}
#else /* generic implementation */
static inline int rv_clz(uint32_t v)
{
/* 0 is considered as undefined behavior */
assert(v);
/* http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn */
static const uint8_t mul_debruijn[] = {
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31,
};
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
return mul_debruijn[(uint32_t) (v * 0x07C4ACDDU) >> 27];
}
#endif
#if defined(_MSC_VER)
#include <intrin.h>
static inline int rv_ctz(uint32_t v)
{
/* 0 is considered as undefined behavior */
assert(v);
uint32_t trailing_zero = 0;
_BitScanForward(&trailing_zero, v);
return trailing_zero;
}
#elif defined(__GNUC__) || defined(__clang__)
static inline int rv_ctz(uint32_t v)
{
/* https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html */
/* 0 is considered as undefined behavior */
assert(v);
return __builtin_ctz(v);
}
#else /* generic implementation */
static inline int rv_ctz(uint32_t v)
{
/* 0 is considered as undefined behavior */
assert(v);
/* https://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup
*/
static const int mul_debruijn[32] = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
return mul_debruijn[((uint32_t) ((v & -v) * 0x077CB531U)) >> 27];
}
#endif
#if defined(__GNUC__) || defined(__clang__)
static inline int rv_popcount(uint32_t v)
{
/* https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html */
return __builtin_popcount(v);
}
#else /* generic implementation */
static inline int rv_popcount(uint32_t v)
{
/* https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
*/
v -= (v >> 1) & 0x55555555;
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
v = (v + (v >> 4)) & 0x0f0f0f0f;
return (v * 0x01010101) >> 24;
}
#endif
/*
* Integer log base 2
*
* The input x must not be zero.
* Otherwise, the result is undefined on some platform.
*
*/
static inline uint8_t ilog2(uint32_t x)
{
return 31 - rv_clz(x);
}
/* Alignment macro */
#if defined(__GNUC__) || defined(__clang__)
#define __ALIGNED(x) __attribute__((aligned(x)))
#elif defined(_MSC_VER)
#define __ALIGNED(x) __declspec(align(x))
#else /* unsupported compilers */
#define __ALIGNED(x)
#endif
/* 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
/* Endianness */
#if defined(__GNUC__) || defined(__clang__)
#define bswap16(x) __builtin_bswap16(x)
#define bswap32(x) __builtin_bswap32(x)
#else
#define bswap16(x) ((x & 0xff) << 8) | ((x >> 8) & 0xff)
#define bswap32(x) \
(bswap16(((x & 0xffff) << 16) | ((x >> 16) & 0xffff)) & 0xffff) | \
(bswap16(((x & 0xffff) << 16) | ((x >> 16) & 0xffff)) & 0xffff) << 16
#endif
/* The purpose of __builtin_unreachable() is to assist the compiler in:
* - Eliminating dead code that the programmer knows will never be executed.
* - Linearizing the code by indicating to the compiler that the path is 'cold'
* (a similar effect can be achieved by calling a noreturn function).
*/
#if defined(__GNUC__) || defined(__clang__)
#define __UNREACHABLE __builtin_unreachable()
#elif defined(_MSC_VER)
#define __UNREACHABLE __assume(false)
#else /* unspported compilers */
/* clang-format off */
#define __UNREACHABLE do { /* nop */ } while (0)
/* clang-format on */
#endif
/* Non-optimized builds do not have tail-call optimization (TCO). To work
* around this, the compiler attribute 'musttail' is used, which forces TCO
* even without optimizations enabled.
*/
#if defined(__has_attribute) && __has_attribute(musttail)
#define MUST_TAIL __attribute__((musttail))
#else
#define MUST_TAIL
#endif
/* Assume that all POSIX-compatible environments provide mmap system call. */
#if defined(_WIN32)
#define HAVE_MMAP 0
#else
/* Assume POSIX-compatible runtime */
#define HAVE_MMAP 1
#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
/* Accept any number of args >= N, but expand to just the Nth one. The macro
* that calls the function still only supports 4 args, but the set of values
* that might need to be returned is 1 larger, so N is increased to 6.
*/
#define _GET_NTH_ARG(_1, _2, _3, _4, _5, N, ...) N
/* Count how many args are in a variadic macro. The GCC/Clang extension is used
* to handle the case where ... expands to nothing. A placeholder arg is added
* before ##VA_ARGS (its value is irrelevant but necessary to preserve the
* shifting offset).
* Additionally, 0 is added as a valid value in the N position.
*/
#define COUNT_VARARGS(...) _GET_NTH_ARG("ignored", ##__VA_ARGS__, 4, 3, 2, 1, 0)
/* As of C23, typeof is now included as part of the C standard. */
#if defined(__GNUC__) || defined(__clang__) || \
(defined(__STDC__) && defined(__STDC_VERSION__) && \
(__STDC_VERSION__ >= 202000L)) /* C2x/C23 ?*/
#define __HAVE_TYPEOF 1
#endif
/**
* container_of() - Calculate address of object that contains address ptr
* @ptr: pointer to member variable
* @type: type of the structure containing ptr
* @member: name of the member variable in struct @type
*
* Return: @type pointer of object containing ptr
*/
#ifndef container_of
#ifdef __HAVE_TYPEOF
#define container_of(ptr, type, member) \
__extension__({ \
const __typeof__(((type *) 0)->member) *__pmember = (ptr); \
(type *) ((char *) __pmember - offsetof(type, member)); \
})
#else
#define container_of(ptr, type, member) \
((type *) ((char *) (ptr) - (offsetof(type, member))))
#endif
#endif