summaryrefslogtreecommitdiff
path: root/src/util.h
blob: b4e191617ae7399ed5b5f805ec87e6c6332589cc (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
#ifndef IMB3_UTIL_H
#define IMB3_UTIL_H

#include <stdbool.h>
#include <stdnoreturn.h>

static inline bool likely(bool cond) { return __builtin_expect(cond, true); }
static inline bool unlikely(bool cond) { return __builtin_expect(cond, false); }

#ifndef NDEBUG
noreturn void assume__failed(const char *file, int line, const char *expr);
#endif // NDEBUG

static inline void assume__impl(bool cond, const char *file, int line,
                                const char *expr) {
#ifndef NDEBUG
  if (unlikely(!cond))
    assume__failed(file, line, expr);
#endif // NDEBUG
  if (unlikely(!cond))
    __builtin_unreachable();
}

#define assume(COND) assume__impl(COND, __FILE__, __LINE__, #COND)

__attribute__((format(printf, 3, 4))) void
debugf__impl(const char *file, int line, const char *fmt, ...);

#define debugf(...) debugf__impl(__FILE__, __LINE__, __VA_ARGS__)

__attribute__((format(printf, 3, 4))) noreturn void
todo__impl(const char *file, int line, const char *fmt, ...);

#define todo(...) todo__impl(__FILE__, __LINE__, __VA_ARGS__)

__attribute__((format(printf, 3, 4))) noreturn void
unreachable__impl(const char *file, int line, const char *fmt, ...);

#define unreachable(...) unreachable__impl(__FILE__, __LINE__, __VA_ARGS__)

#endif // IMB3_UTIL_H