[PATCH 2/7] Add 'likely' and 'unlikely' macros

Keith Packard keithp at keithp.com
Mon May 11 21:23:53 PDT 2015


These two macros provide hints to the compiler about common code paths
to help it optimize a bit better.

Signed-off-by: Keith Packard <keithp at keithp.com>
---
 include/misc.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/include/misc.h b/include/misc.h
index 9b1c03a..74889f5 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -291,6 +291,14 @@ version_compare(uint32_t a_major, uint32_t a_minor,
 #define SwapRestL(stuff) \
     SwapLongs((CARD32 *)(stuff + 1), LengthRestL(stuff))
 
+#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
+#define likely(expr) (__builtin_expect (!!(expr), 1))
+#define unlikely(expr) (__builtin_expect (!!(expr), 0))
+#else
+#define likely(expr) (expr)
+#define unlikely(expr) (expr)
+#endif
+
 #if defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
 void __attribute__ ((error("wrong sized variable passed to swap")))
 wrong_size(void);
-- 
2.1.4



More information about the xorg-devel mailing list