summaryrefslogtreecommitdiff
path: root/wrmr.h
blob: 068cd8a99aed1b0b2f3cdf90cbd6cb43fd156629 (plain)
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
/* wrmr.h */
/* common definitions used on most projects */

#ifndef WRMR_H
#define WRMR_H

#include <stdint.h>
#include <stddef.h>
#include <stdio.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