-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
424 lines (360 loc) · 10.3 KB
/
util.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#ifndef UTIL_H
#define UTIL_H
// TODO: seperate bool/noreturn and other version-dependent stuff into its own
// header
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __cplusplus
#error "do not compile jcc as C++"
#endif
#if __STDC_VERSION__ >= 202311L
#define STDC_C23 1
#elif __STDC_VERSION__ >= 201710L
#define STDC_C18 1
#elif __STDC_VERSION__ == 201112L
#define STDC_C11 1
#else
#error "jcc only supports C11 or later"
#endif
#ifdef UTIL_MACRO_DEBUG
#ifdef __clang__
#pragma message "Compiler is clang"
#elif __GNUC__
#warn "Compiler is GCC"
#else
#pragma message "unrecognised compiler"
#endif
#if STDC_C23
#pragma message "C version is C23"
#elif STDC_C18
#pragma message "C version is C28"
#elif STDC_C11
#pragma message "C version is C11"
#else
#define EXPAND_INNER(x) "unrecognised C version '" #x "'"
#define EXPAND(x) EXPAND_INNER(x)
#pragma message(EXPAND(__STDC_VERSION__))
#undef EXPAND
#undef EXPAND_INNER
#endif
#endif
#ifdef INT128_C
#define HAS_INT128 1
#elif __clang__ || __GNUC__
#define HAS_INT128 1
typedef __int128 int128_t;
typedef unsigned __int128 uint128_t;
#elif STDC_C23 && BITINT_MAXWIDTH >= 128
typedef _BitInt(128) int128_t;
typedef unsigned _BitInt(128) uint128_t;
#define HAS_INT128 1
#endif
#if STDC_C23 && __GNUC__
#define PRINTF_ARGS(idx) [gnu::format(printf, idx + 1, idx + 2)]
#elif __GNUC__
#define PRINTF_ARGS(idx) __attribute__((format(printf, idx + 1, idx + 2)))
#else
#define PRINTF_ARGS(idx)
#endif
#ifdef __has_feature
#define HAS_FEATURE(name) __has_feature(name)
#else
#define HAS_FEATURE(name) 0
#endif
#ifdef __has_builtin
#define HAS_BUILTIN(name) __has_builtin(name)
#else
#define HAS_BUILTIN(name) 0
#endif
#ifdef __has_attribute
#define HAS_ATTRIBUTE(name) __has_attribute(name)
#else
#define HAS_ATTRIBUTE(name) 0
#endif
#ifdef __has_c_attribute
#define HAS_C_ATTRIBUTE(name) __has_c_attribute(name)
#else
#define HAS_C_ATTRIBUTE(name) 0
#endif
#if STDC_C23 && HAS_C_ATTRIBUTE(flag_enum)
#define FLAG_ENUM [gnu::flag_enum]
#elif HAS_ATTRIBUTE(flag_enum)
#define FLAG_ENUM __attribute__((flag_enum))
#else
#define FLAG_ENUM
#endif
#if HAS_FEATURE(memory_sanitizer) || defined(MEMORY_SANITIZER) || \
defined(__SANITIZE_MEMORY__)
#define MSAN 1
#else
#define MSAN 0
#endif
#if HAS_FEATURE(address_sanitizer) || defined(ADDRESS_SANITIZER) || \
defined(__SANITIZE_ADDRESS__)
#define ASAN 1
#else
#define ASAN 0
#endif
#if HAS_FEATURE(hwaddress_sanitizer) || defined(HWADDRESS_SANITIZER) || \
defined(__SANITIZE_HWADDRESS__)
#define HWASAN 1
#else
#define HWASAN 0
#endif
#if HAS_FEATURE(thread_sanitizer) || defined(THREAD_SANITIZER) || \
defined(__SANITIZE_THREAD__)
#define TSAN 1
#else
#define TSAN 0
#endif
#if HAS_FEATURE(undefined_behavior_sanitizer) || \
defined(UNDEFINED_BEHAVIOR_SANITIZER)
#define UBSAN 1
#else
#define UBSAN 0
#endif
#if ASAN || MSAN || TSAN || UBSAN
#define SANITIZER_PRINT_STACK_TRACE
#include "sanitizer/common_interface_defs.h" // __sanitizer_print_stack_trace
#endif
#if STDC_C23
#define NORETURN [[noreturn]]
#else
#include <stdnoreturn.h>
#define NORETURN noreturn
#endif
#if __GNUC__ && STDC_32
#define TRYFORCEINLINE [[gnu::always_inline]] inline
#elif __GNUC__
#define TRYFORCEINLINE __attribute__((always_inline)) inline
#else
#define TRYFORCEINLINE inline
#endif
#define ROUND_UP(value, pow2) (((value) + ((pow2) - 1ull)) & ~((pow2) - 1ull))
#if STDC_C23 && __GNUC__
#define UNUSED_ARG(arg) [gnu::unused] arg
#define UNUSED [gnu::unused]
#elif __GNUC__
#define UNUSED_ARG(arg) __attribute__((__unused__)) arg
#define UNUSED __attribute__((__unused__))
#else
#define UNUSED_ARG(arg)
#define UNUSED
#endif
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ARR_LENGTH(a) (sizeof((a)) / sizeof((a)[0]))
static inline size_t num_digits(size_t num) {
return (num ? (size_t)log10(num) : 0) + 1;
}
static inline void debug_print_stack_trace(void) {
#ifdef SANITIZER_PRINT_STACK_TRACE
__sanitizer_print_stack_trace();
#endif
}
#define FMTPRINT(file, message, format) \
do { \
va_list v; \
va_start(v, format); \
fprintf(file, message); \
vfprintf(file, format, v); \
fprintf(file, "\n"); \
va_end(v); \
} while (0);
#define MACRO_FMTPRINT(file, message, ...) \
do { \
fprintf(file, message); \
fprintf(file, __VA_ARGS__); \
fprintf(file, "\n"); \
} while (0);
#define EXIT_FAIL(code) \
debug_print_stack_trace(); \
raise(SIGINT); \
exit(code);
#if __clang__
#define START_NO_UNUSED_ARGS \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wunused-parameter\"")
#elif __GNUC__ && 0
#define START_NO_UNUSED_ARGS \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wunused-parameter\"")
#else
#define START_NO_UNUSED_ARGS
#endif
#if __clang__
#define END_NO_UNUSED_ARGS _Pragma("clang diagnostic pop")
#elif __GNUC__ && 0
#define END_NO_UNUSED_ARGS _Pragma("GCC diagnostic pop")
#else
#define END_NO_UNUSED_ARGS
#endif
#ifdef SIZE_T_MAX
#undef SIZE_T_MAX
#define SIZE_T_MAX (static_assert(false, "use SIZE_MAX instead"))
#endif
#define TODO_FUNC(sig) \
START_NO_UNUSED_ARGS \
sig { TODO(__func__); } \
END_NO_UNUSED_ARGS
#define TODO(...) \
do { \
MACRO_FMTPRINT(stderr, "`todo` hit, program exiting: ", __VA_ARGS__); \
EXIT_FAIL(-2); \
} while (0);
#define BUG(...) \
do { \
MACRO_FMTPRINT(stderr, "`bug` hit, program exiting: ", __VA_ARGS__); \
EXIT_FAIL(-2); \
} while (0);
#if NDEBUG
#define DEBUG_ASSERT(...)
#else
#define DEBUG_ASSERT(b, ...) util_debug_assert(b, __VA_ARGS__)
#endif
TRYFORCEINLINE PRINTF_ARGS(1) static void util_debug_assert(bool b, const char *msg, ...) {
if (!b) {
FMTPRINT(stderr, "debug_assertion failed, program exiting: ", msg);
EXIT_FAIL(-1);
}
}
NORETURN static inline void unreachable(void) {
fprintf(stderr, "`unreachable` hit, program exiting");
EXIT_FAIL(-2);
}
PRINTF_ARGS(0) NORETURN static inline void unsupported(const char *msg, ...) {
FMTPRINT(stderr, "", msg);
EXIT_FAIL(-2);
}
// present in all mode, always causes program exit if fails
PRINTF_ARGS(1)
static inline void invariant_assert(bool b, const char *msg, ...) {
if (!b) {
FMTPRINT(stderr, "invariant_assertion failed, program exiting: ", msg);
EXIT_FAIL(-1);
}
}
#if HAS_BUILTIN(__builtin_debugtrap)
#define BREAKPOINT() __builtin_debugtrap()
#else
#define raise(SIGINT)
#endif
static inline unsigned long long rotateright64(unsigned long long value,
unsigned int amount) {
#if HAS_BUILTIN(__builtin_rotateright64)
return __builtin_rotateright64(value, amount);
#else
amount %= sizeof(value) * 8; // Ensure amount is within [0, 63]
return (value >> amount) | (value << ((sizeof(value) * 8) - amount));
#endif
}
static inline int popcntl(unsigned long long l) {
#if HAS_BUILTIN(__builtin_popcountll)
return __builtin_popcountll(l);
#else
int count = 0;
while (l) {
count += l & 1;
l >>= 1;
}
return count;
#endif
}
static inline int tzcnt(unsigned long long l) {
#if HAS_BUILTIN(__builtin_ctzll)
return __builtin_ctzll(l);
#else
if (l == 0) return sizeof(l) * 8;
int count = 0;
while ((l & 1) == 0) {
count++;
l >>= 1;
}
return count;
#endif
}
static inline int lzcnt(unsigned long long l) {
#if HAS_BUILTIN(__builtin_clzll)
return __builtin_clzll(l);
#else
if (l == 0) return sizeof(l * 8);
int count = 0;
for (int i = (sizeof(l) * 8) - 1; i >= 0; i--) {
if ((l >> i) & 1) {
break;
}
count++;
}
return count;
#endif
}
static inline unsigned long long ilog2(unsigned long long num) {
return (sizeof(num) * 8) - lzcnt(num) - 1;
}
static inline void *nonnull_malloc(size_t size) {
if (size == 0) {
return NULL;
}
void *ptr = malloc(size);
invariant_assert(ptr, "`malloc` returned NULL (out-of-memory likely)");
return ptr;
}
static inline void *nonnull_realloc(void *p, size_t size) {
void *ptr = realloc(p, size);
invariant_assert(ptr, "`realloc` returned NULL (out-of-memory likely)");
return ptr;
}
static inline void fprint_str(FILE *file, const char *input) {
DEBUG_ASSERT(file, "null arg");
if (!input) {
fprintf(file, "(null)");
return;
}
fputc('"', file);
while (*input) {
switch (*input) {
case '\\':
fputs("\\\\", file);
break;
case '\"':
fputs("\\\"", file);
break;
case '\n':
fputs("\\n", file);
break;
case '\t':
fputs("\\t", file);
break;
case '\r':
fputs("\\r", file);
break;
case '\b':
fputs("\\b", file);
break;
case '\f':
fputs("\\f", file);
break;
case '\v':
fputs("\\v", file);
break;
default:
if (isprint((unsigned char)*input)) {
fputc(*input, file);
} else {
fprintf(file, "\\x%02x", (unsigned char)*input);
}
break;
}
input++;
}
fputc('"', file);
}
#endif