util: Add assume() macro.
authorMatt Turner <mattst88@gmail.com>
Tue, 30 Sep 2014 23:24:39 +0000 (16:24 -0700)
committerMatt Turner <mattst88@gmail.com>
Thu, 23 Oct 2014 23:20:07 +0000 (16:20 -0700)
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
src/util/macros.h

index 40ebf02da84cc86c22cbd930d29c8f07185604e4..ff37a7d63e6dce4ab660524d114703f5cd4965ff 100644 (file)
@@ -75,6 +75,20 @@ do {                        \
 #define unreachable(str)
 #endif
 
+/**
+ * Assume macro. Useful for expressing our assumptions to the compiler,
+ * typically for purposes of silencing warnings.
+ */
+#ifdef HAVE___BUILTIN_UNREACHABLE
+#define assume(expr) ((expr) ? ((void) 0) \
+                             : (assert(!"assumption failed"), \
+                                __builtin_unreachable()))
+#elif _MSC_VER >= 1200
+#define assume(expr) __assume(expr)
+#else
+#define assume(expr) assert(expr)
+#endif
+
 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
 #define FLATTEN __attribute__((__flatten__))
 #else