-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
300 lines (248 loc) · 7.74 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
#ifndef UTIL_H
#define UTIL_H
// TODO: seperate bool/noreturn and other version-dependent stuff into its own
// header
#include <assert.h>
#include <math.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <limits.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) [gcc::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
#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
#define ROUND_UP(value, pow2) (((value) + ((pow2) - 1ull)) & ~((pow2) - 1ull))
#if STDC_C23 && __GNUC__
#define UNUSED_ARG(arg) [gcc::unused] arg
#define UNUSED [gcc::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
}
static inline unsigned long long rotateright64(unsigned long long value, unsigned int amount) {
#if HAS_BUILTIN(__builtin_rotateright64)
return __builtin_rotateright64(value, amount);
#else
todo("rotaterightl not implemented outside of `__builtin_popcountll`");
#endif
}
static inline int popcntl(unsigned long long l) {
#if HAS_BUILTIN(__builtin_popcountll)
return __builtin_popcountll(l);
#else
todo("lzcnt not implemented outside of `__builtin_popcountll`");
#endif
}
static inline int tzcnt(unsigned long long l) {
#if HAS_BUILTIN(__builtin_clzll)
return __builtin_ctzll(l);
#else
todo("lzcnt not implemented outside of `__builtin_clzll`");
#endif
}
static inline int lzcnt(unsigned long long l) {
#if HAS_BUILTIN(__builtin_clzll)
return __builtin_clzll(l);
#else
todo("lzcnt not implemented outside of `__builtin_clzll`");
#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 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__
#define START_NO_UNUSED_ARGS \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wunused-parameter\"")
#endif
#if __clang__
#define END_NO_UNUSED_ARGS _Pragma("clang diagnostic pop")
#elif __GNUC__
#define END_NO_UNUSED_ARGS _Pragma("GCC diagnostic pop")
#endif
#define TODO_FUNC(sig) \
START_NO_UNUSED_ARGS \
sig { todo(__func__); } \
END_NO_UNUSED_ARGS
PRINTF_ARGS(0) NORETURN static inline void todo(const char *msg, ...) {
FMTPRINT(stderr, "`todo` hit, program exiting: ", msg);
EXIT_FAIL(-2);
}
NORETURN static inline void unreachable(void) {
fprintf(stderr, "`unreachable` hit, program exiting");
EXIT_FAIL(-2);
}
PRINTF_ARGS(0) NORETURN static inline void bug(const char *msg, ...) {
FMTPRINT(stderr, "`bug` hit, program exiting: ", 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);
}
}
static inline void breakpoint(void) { raise(SIGINT); }
#if NDEBUG
PRINTF_ARGS(1) static inline void debug_assert(bool, const char *, ...) {}
#else
PRINTF_ARGS(1) static inline void debug_assert(bool b, const char *msg, ...) {
if (!b) {
FMTPRINT(stderr, "debug_assertion failed, program exiting: ", msg);
EXIT_FAIL(-1);
}
}
#endif
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 char *malloc_strcpy(const char *s) {
size_t len = strlen(s);
char *cp = nonnull_malloc(len * sizeof(*s));
// use memcpy as we already have len
memcpy(cp, s, len * sizeof(*s));
return cp;
}
#endif