/* wrmr.h */ /* common definitions used on most projects */ #ifndef WRMR_H #define WRMR_H #include #include typedef int8_t i8; typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef size_t usize; typedef ptrdiff_t isize; typedef intptr_t iptr; typedef uintptr_t uptr; #define BESTRING_(x) #x #define BESTRING(x) BESTRING_(x) #ifdef __has_builtin #define HAS_BUILTIN(x) __has_builtin(x) #else #define HAS_BUILTIN(x) 0 #endif #if defined(__i368__) || defined(__x86_64__) /* nop so gdb will break on the trap, not the line after */ # define TRAP() __asm__ volatile ("int3; nop") #elif HAS_BUILTIN(__builtin_debugtrap) # define TRAP() __builtin_debugtrap() #elif HAS_BUILTIN(__builtin_trap) && defined(__STDC_HOSTED__) && (__STDC_HOSTED__ == 0) # define TRAP() __builtin_trap() #elif defined(_MSC_VER) # define TRAP() __debugbreak() #else # define TRAP() (*(volatile int *)NULL = 0) #endif #ifndef PUT_ERROR_MSG # include # define PUT_ERROR_MSG(msg) fputs(msg, stderr) #endif #define FILE_LINE_MSG(msg) (__FILE__ ":" BESTRING(__LINE__) ": " msg "\n") #define FAIL_WITH_MSG(msg) do { PUT_ERROR_MSG(FILE_LINE_MSG(msg)); TRAP(); } while(0) #ifdef NDEBUG # define ASSERT(...) do {} while(0) #else # define ASSERT(...) do {\ if (!(__VA_ARGS__)) FAIL_WITH_MSG("Assertion failed: " #__VA_ARGS__);\ } while(0) #endif #ifndef NDEBUG # define UNREACHABLE_MSG(msg) FAIL_WITH_MSG(msg) #else # if __STDC_VERSION__ >= 202311L # define UNREACHABLE_MSG(msg) (unreachable()) # elif HAS_BUILTIN(__builtin_unreachable) # define UNREACHABLE_MSG(msg) (__builtin_unreachable()) # elif defined(_MSC_VER) # define UNREACHABLE_MSG(msg) (__assume(0)) # else # define UNREACHABLE_MSG(msg) do {} while(0) # endif #endif #define UNREACHABLE UNREACHABLE_MSG("unreachable code") #define ASSUME(...) do if (!(__VA_ARGS__)) UNREACHABLE_MSG("assumption failed: " #__VA_ARGS__); while(0) #ifdef NDEBUG # if defined(_MSC_VER) # undef ASSUME # define ASSUME(...) (__assume(__VA_ARGS__)) # elif HAS_BUILTIN(__builtin_assume) # undef ASSUME # define ASSUME(...) (__builtin_assume(__VA_ARGS__)) # endif #endif #if __STDC_VERSION__ >= 202311L # define TYPEOF(...) typeof(__VA_ARGS__) #else # define TYPEOF(...) __typeof__(__VA_ARGS__) #endif #define COUNTOF(x) (sizeof(x) / sizeof(*(x))) #define MOVE(a0, a1, n) memmove(1?(a0):(a1), a1, sizeof(*(a0)) * (n)); #define COPY(a0, a1, n) memmove(1?(a0):(a1), a1, sizeof(*(a0)) * (n)); #endif