18 #ifndef __STARTLE_MACROS__ 19 #define __STARTLE_MACROS__ 30 #pragma clang diagnostic ignored "-Wgnu-statement-expression" 38 #define _CONCAT(x, y) x##y 39 #define CONCAT(x, y) _CONCAT(x, y) 41 #define _CONCAT_UNDERSCORE(x, y) x##_##y 42 #define CONCAT_UNDERSCORE(x, y) _CONCAT_UNDERSCORE(x, y) 45 #define UNIQUE CONCAT(__unique_, __LINE__) 48 #define _STRINGIFY(x) #x 49 #define STRINGIFY(x) _STRINGIFY(x) 56 #define sizeof_field(s, f) sizeof(((s *)0)->f) 59 #if !defined(offsetof) 60 #define offsetof(s, f) ((uintptr_t)&(((s *)0)->f)) 64 #define WIDTH(a) (sizeof((a)[0])) 67 #define LENGTH(a) (sizeof(a) / WIDTH(a)) 74 #define RANGEUP(i, lower, upper) for(size_t i = (lower), __upper = (upper); i < __upper; i++) 77 #define RANGEDOWN(i, lower, upper) for(size_t i = (upper), __lower = (lower); i-- > __lower; ) 82 #define COUNTDOWN(i, n) RANGEDOWN(i, 0, n) 85 #define COUNTUP(i, n) RANGEUP(i, 0, n) 88 #define LOOP(n) COUNTDOWN(UNIQUE, n) 91 #define FOREACH(i, a) COUNTUP(i, LENGTH(a)) 94 #define FORMAP(i, m) for(size_t i = 1; i <= *map_cnt(m); i++) 99 #define BITSET(name, size) uint8_t name[((size)+7)/8] = {0} 102 #define BITSET_INDEX(name, array) BITSET(name, LENGTH(array)) 104 #if !defined(static_assert) 105 #define static_assert(expr, msg) _Static_assert(expr, msg) 111 #define SEG(x) {(x), sizeof(x) - 1} 118 #define printseg(pre, seg, fmt, ...) \ 121 printf(pre "%.*s" fmt, (int)__seg.n, __seg.s , ##__VA_ARGS__); \ 131 #define LIST_ADD(f, l, v) \ 133 __typeof__(v) __v = (v); \ 143 #define CONS(f, l, v) \ 145 __typeof__(l) __l = (l); \ 146 __typeof__(v) __v = (v); \ 157 __typeof__(a) __a = (a); \ 158 __typeof__(b) __b = (b); \ 159 __a <= __b ? __a : __b; \ 164 __typeof__(a) __a = (a); \ 165 __typeof__(b) __b = (b); \ 166 __a >= __b ? __a : __b; \ 172 __typeof__(a) _a = (a); \ 173 __typeof__(b) _b = (b); \ 174 _b > _a ? 0 : _a - _b; \ 181 #define zero(a) memset(&(a), 0, sizeof(a)) 186 #define show(x) printf(#x " = %d\n", (int)(x)) 189 #define FLAG(s, flag) (((s).flags & (flag)) != 0) 192 #define NOT_FLAG(s, flag) (((s).flags & (flag)) == 0) 194 #define FLAG_SET(s, flag) ((s).flags |= (flag)) 195 #define FLAG_CLEAR(s, flag) ((s).flags &= ~(flag)) 196 #define FLAG_SET_TO(s, flag, val) ((val) ? FLAG_SET(s, flag) : FLAG_CLEAR(s, flag)) 199 #define ARRAY_SHIFTR(array, offset, length) memmove(&(array) + (offset), &(array), (length) * sizeof(array)) 202 #define ARRAY_SHIFTL(array, offset, length) memmove(&(array), &(array) + (offset), (length) * sizeof(array)) 204 #define ARRAY_COPY(dst, src, length) memcpy(&(dst), &(src), (length) * sizeof(dst)) 206 #if !defined(EMSCRIPTEN) 207 #define COLOR(c, str) "\x1b[" CONCAT(COLOR_, c) "m" str "\x1b[0m" 208 #define COLORs(str) "\x1b[%sm" str "\x1b[0m" 209 #define COLOR_red "37;41" 210 #define COLOR_blue "37;44" 211 #define COLOR_gray "38;5;8" 212 #define COLOR_normal "0" 214 #define COLOR(c, str) "[[;" CONCAT(COLOR_, c) ";]" str "]" 215 #define COLORs(str) "[[;%s;]" str "]" 216 #define COLOR_red "#fff;#f00" 217 #define COLOR_blue "#fff;#00f" 218 #define COLOR_gray "#999;#000" 219 #define COLOR_normal ";" 222 #define MARK(x) COLOR(red, x) 223 #define NOTE(x) COLOR(blue, x) 224 #define FADE(x) COLOR(gray, x) 225 #define TODO MARK("TODO") 226 #define HACK MARK("HACK") 228 #define DISABLE(...) \ 230 LOG(MARK("DISABLED") " %s", __func__); \ 231 return __VA_ARGS__; \ 237 #define unlikely(c) (__builtin_expect((c), 0)) 242 #define likely(c) (__builtin_expect((c), 1)) 245 #define TEST(name) int test_##name() 248 #define FORMAT(name, c) void format_##name(intptr_t i) Function dispatch for variable arguments.