util: Implement assume() for clang.
authorMatt Turner <mattst88@gmail.com>
Fri, 21 Nov 2014 21:50:14 +0000 (13:50 -0800)
committerMatt Turner <mattst88@gmail.com>
Mon, 24 Nov 2014 22:09:23 +0000 (14:09 -0800)
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
src/util/macros.h

index da5daff9bf8283e6796f85420dc1b170c95a3e24..5fc6729536cd6027dbd8aa84247c2f338b7f8084 100644 (file)
 #  define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
 #endif
 
+/* For compatibility with Clang's __has_builtin() */
+#ifndef __has_builtin
+#  define __has_builtin(x) 0
+#endif
 
 /**
  * __builtin_expect macros
@@ -85,7 +89,13 @@ do {                        \
  * Assume macro. Useful for expressing our assumptions to the compiler,
  * typically for purposes of silencing warnings.
  */
-#ifdef HAVE___BUILTIN_UNREACHABLE
+#if __has_builtin(__builtin_assume)
+#define assume(expr)       \
+do {                       \
+   assert(expr);           \
+   __builtin_assume(expr); \
+} while (0)
+#elif defined HAVE___BUILTIN_UNREACHABLE
 #define assume(expr) ((expr) ? ((void) 0) \
                              : (assert(!"assumption failed"), \
                                 __builtin_unreachable()))