libreplace: added likely()/unlikely() macros for gcc
authorAndrew Tridgell <tridge@samba.org>
Thu, 17 Sep 2009 16:07:17 +0000 (09:07 -0700)
committerAndrew Tridgell <tridge@samba.org>
Thu, 17 Sep 2009 22:19:25 +0000 (15:19 -0700)
These macros allow the compile to better optimise code that has a lot
of if statements. I particularly want to use this for our low level
generated NDR code.

lib/replace/replace.h

index 2db6aa1226cbe796666371151dc89d7656bac78e..6424d10c0f9b9ed6f53f1740be6c97cf020e747b 100644 (file)
@@ -704,4 +704,23 @@ char *ufc_crypt(const char *key, const char *salt);
 #endif
 #endif
 
+/* these macros gain us a few percent of speed on gcc */
+#if (__GNUC__ >= 3)
+/* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
+   as its first argument */
+#ifndef likely
+#define likely(x)   __builtin_expect(!!(x), 1)
+#endif
+#ifndef unlikely
+#define unlikely(x) __builtin_expect(!!(x), 0)
+#endif
+#else
+#ifndef likely
+#define likely(x) (x)
+#endif
+#ifndef unlikely
+#define unlikely(x) (x)
+#endif
+#endif
+
 #endif /* _LIBREPLACE_REPLACE_H */