[vlc-devel] commit: likely, unlikely: convenience portability macros for branch prediction ( Rémi Denis-Courmont )

git version control git at videolan.org
Sun Dec 6 11:23:22 CET 2009


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Dec  6 10:16:59 2009 +0200| [827b111356e910800788245bb1a2a22017fea857] | committer: Rémi Denis-Courmont 

likely, unlikely: convenience portability macros for branch prediction

Those are the same as the Linux kernel macros, and probably a bunch of
other projects. Usage:

    if (likely(condition))
        branch_likely_taken();

    if (unlikely(condition))
        branch_unlikely_taken();

Attention: those two macros convert the predicate to a boolean value.
Therefore you generally cannot use them for assignments like this:
    void *p_sys = likely (malloc (sizeof (*p_sys)));
    if (!p_sys)
        return VLC_ENOMEM;
Instead you can do this:
    void *p_sys = malloc (sizeof (*p_sys));
    if (unlikely (!p_sys))
        return VLC_ENOMEM;

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=827b111356e910800788245bb1a2a22017fea857
---

 include/vlc_common.h |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)

diff --git a/include/vlc_common.h b/include/vlc_common.h
index e4b1cc5..82dad36 100644
--- a/include/vlc_common.h
+++ b/include/vlc_common.h
@@ -69,6 +69,15 @@
 #   define LIBVLC_MALLOC
 #endif
 
+/* Branch prediction */
+#ifdef __GNUC__
+#   define likely(p)   __builtin_expect(!!(p), 1)
+#   define unlikely(p) __builtin_expect(!!(p), 0)
+#else
+#   define likely(p)   (!!(p))
+#   define unlikely(p) (!!(p))
+#endif
+
 /*****************************************************************************
  * Basic types definitions
  *****************************************************************************/




More information about the vlc-devel mailing list