summaryrefslogtreecommitdiff
path: root/wrmr.h
blob: cbf60c0de7f47ee5f63c004e825a6e78154ed7ec (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* 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)

#if defined(__GNUC__)
#	define TRAP() __builtin_trap()
#else
#	define TRAP() (*(volatile int *)NULL = 0)
#endif

#ifndef PUT_ERROR_MSG
#       include <stdio.h>
#       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 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
#else
#	define ASSUME(...) do if (!(__VA_ARGS__)) UNREACHABLE_MSG("assumption failed: " #__VA_ARGS__); while(0);
#endif

#if __STDC_VERSION__ >= 202311L
#	define TYPEOF(...) typeof(__VA_ARGS__)
#else
#	define TYPEOF(...) __typeof__(__VA_ARGS__)
#endif

#define COUNTOF(x) (sizeof(x) / sizeof(*(x)))

#endif