Skip to content

Commit 70b3c11

Browse files
committed
Add RBS_LIKELY and RBS_UNLIKELY
1 parent 3872428 commit 70b3c11

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

include/rbs/defines.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@
3232
#define RBS_ATTRIBUTE_FORMAT(string_index, argument_index)
3333
#endif
3434

35+
/**
36+
* Support RBS_LIKELY and RBS_UNLIKELY to help the compiler optimize its
37+
* branch predication.
38+
*/
39+
#if defined(__GNUC__) || defined(__clang__)
40+
/** The compiler should predicate that this branch will be taken. */
41+
#define RBS_LIKELY(x) __builtin_expect(!!(x), 1)
42+
43+
/** The compiler should predicate that this branch will not be taken. */
44+
#define RBS_UNLIKELY(x) __builtin_expect(!!(x), 0)
45+
#else
46+
/** Void because this platform does not support branch prediction hints. */
47+
#define RBS_LIKELY(x) (x)
48+
49+
/** Void because this platform does not support branch prediction hints. */
50+
#define RBS_UNLIKELY(x) (x)
51+
#endif
52+
3553
/**
3654
* We use -Wimplicit-fallthrough to guard potentially unintended fall-through between cases of a switch.
3755
* Use RBS_FALLTHROUGH to explicitly annotate cases where the fallthrough is intentional.

0 commit comments

Comments
 (0)