this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#pragma once
3
4#include <stdint.h>
5
6// Branch prediction hints for the compiler. Use in performance critial code
7// which almost always branches one way.
8#define LIKELY(x) __builtin_expect(!!(x), 1)
9#define UNLIKELY(x) __builtin_expect(!!(x), 0)
10
11#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
12#define DCHECK_IS_ON() 0
13#else
14#define DCHECK_IS_ON() 1
15#endif
16
17#define CHECK(expr, ...) \
18 do { \
19 if (UNLIKELY(!(expr))) { \
20 checkFailed(__FILE__, __LINE__, __func__, "check '" #expr "' failed", \
21 __VA_ARGS__); \
22 } \
23 } while (0)
24
25#define CHECK_BOUND(val, high) CHECK_RANGE(val, 0, high)
26
27#define CHECK_INDEX(index, high) \
28 do { \
29 if (UNLIKELY(!((index >= 0) && (index < high)))) { \
30 checkIndexFailed(__FILE__, __LINE__, __func__, (intptr_t)(index), \
31 (intptr_t)(high)); \
32 } \
33 } while (0)
34
35#define CHECK_RANGE(val, low, high) \
36 do { \
37 if (UNLIKELY(!((val >= low) && (val <= high)))) { \
38 checkBoundFailed(__FILE__, __LINE__, __func__, (intptr_t)(val), \
39 (intptr_t)(low), (intptr_t)(high)); \
40 } \
41 } while (0)
42
43#if DCHECK_IS_ON()
44#define DCHECK(...) CHECK(__VA_ARGS__)
45#define DCHECK_BOUND(val, high) CHECK_BOUND(val, high)
46#define DCHECK_INDEX(index, high) CHECK_INDEX(index, high)
47#define DCHECK_RANGE(val, low, high) CHECK_RANGE(val, low, high)
48#else
49#define DCHECK(...) \
50 if (false) { \
51 CHECK(__VA_ARGS__); \
52 }
53#define DCHECK_BOUND(val, high) DCHECK_RANGE(val, 0, high)
54#define DCHECK_INDEX(index, high) \
55 if (false) { \
56 CHECK_INDEX(index, high); \
57 }
58#define DCHECK_RANGE(val, low, high) \
59 if (false) { \
60 CHECK_RANGE(val, low, high); \
61 }
62#endif
63
64#define UNIMPLEMENTED(...) \
65 checkFailed(__FILE__, __LINE__, __func__, "unimplemented", __VA_ARGS__)
66
67#define UNREACHABLE(...) \
68 checkFailed(__FILE__, __LINE__, __func__, "unreachable", __VA_ARGS__)
69
70// From https://github.com/Moonstroke/PUCA
71#if defined(__cplusplus) && __cplusplus >= 201103L /* ISO C++11 */
72#define NORETURN [[noreturn]]
73#else /* C++11 */
74#ifdef __GNUC__
75#define NORETURN __attribute__((__noreturn__))
76#else /* __GNUC__ */
77#ifdef _MSC_VER
78#define NORETURN __declspec(noreturn)
79#else /* _MSC_VER */
80#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L /* ISO C11 */
81#define NORETURN _Noreturn
82#else /* C11 */
83#define NORETURN
84#endif /* C11 */
85#endif /* _MSC_VER */
86#endif /* __GNUC__ */
87#endif /* C++11 */
88
89#ifdef __cplusplus
90extern "C" {
91#endif
92
93NORETURN void checkFailed(const char *file, int line, const char *func,
94 const char *expr, ...);
95NORETURN void checkIndexFailed(const char *file, int line, const char *func,
96 intptr_t index, intptr_t high);
97NORETURN void checkBoundFailed(const char *file, int line, const char *func,
98 intptr_t value, intptr_t low, intptr_t high);
99
100#ifdef __cplusplus
101}
102#endif