diff options
| author | WormHeamer | 2025-11-01 19:18:21 -0400 |
|---|---|---|
| committer | WormHeamer | 2025-11-01 19:18:21 -0400 |
| commit | e3348515b9bbbea01b11298bb23276d7706c5c20 (patch) | |
| tree | 589c74d6fbbff6a63fdd767268aef4cb1fd96269 | |
| parent | 39f42c4f391af09dcfb3fb0272534caec26495f5 (diff) | |
add wrmr.h: common definitions for almost all programs
| -rw-r--r-- | wrmr.h | 61 |
1 files changed, 61 insertions, 0 deletions
@@ -0,0 +1,61 @@ +/* wrmr.h */ +/* common definitions used on most projects */ + +#ifndef WRMR_H +#define WRMR_H + +#include <stdint.h> +#include <stddef.h> + +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) + +#define FILE_LINE_MSG(msg) (__FILE__ ":" BESTRING(__LINE__) ": " msg "\n") +#define FAIL_WITH_MSG(msg) do { fputs(FILE_LINE_MSG(msg), stderr); abort(); } while(0) + +#ifndef NDEBUG +# define UNREACHABLE_MSG(msg) FAIL_WITH_MSG(msg) +#else + +#if __STDC_VERSION__ >= 202311L +# define UNREACHABLE_MSG(msg) (unreachable()) +#elif defined(__GNUC__) +# 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") + +#ifdef NDEBUG +# if defined(_MSC_VER) +# define ASSUME(...) (__assume(__VA_ARGS__)) +# elif defined(__clang_version__) +# define ASSUME(...) (__builtin_assume(__VA_ARGS__)) +# endif +#endif + +#ifndef ASSUME +# define ASSUME(...) do if (!(__VA_ARGS__)) UNREACHABLE_MSG("assumption failed: " #__VA_ARGS__); while(0); +#endif + +#endif |
