]> granicus.if.org Git - fribidi/commitdiff
Copy LIKELY/UNLIKELY from GLib
authorKhaled Hosny <khaledhosny@eglug.org>
Sat, 11 Nov 2017 21:42:52 +0000 (23:42 +0200)
committerKhaled Hosny <khaledhosny@eglug.org>
Sat, 11 Nov 2017 22:28:22 +0000 (00:28 +0200)
lib/common.h

index 739e6473c80a4bce06264d30742daf3cf4a34928..8ef427e22c3961e68b9000a665a1b033a23b0782 100644 (file)
 /* LIKEYLY and UNLIKELY are used to give a hint on branch prediction to the
  * compiler. */
 #ifndef LIKELY
-# define LIKELY
-# define UNLIKELY
+# if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
+#  define FRIBIDI_BOOLEAN_EXPR(expr)              \
+   __extension__ ({                               \
+     int fribidi_bool_var;                        \
+     if (expr)                                    \
+        fribidi_bool_var = 1;                     \
+     else                                         \
+        fribidi_bool_var = 0;                     \
+     fribidi_bool_var;                            \
+   })
+#  define LIKELY(expr) (__builtin_expect (FRIBIDI_BOOLEAN_EXPR(expr), 1))
+#  define UNLIKELY(expr) (__builtin_expect (FRIBIDI_BOOLEAN_EXPR(expr), 0))
+# else
+#  define LIKELY
+#  define UNLIKELY
+# endif /* _GNUC_ */
 #endif /* !LIKELY */
 
 #ifndef FRIBIDI_EMPTY_STMT