summaryrefslogtreecommitdiff
path: root/absl/base/optimization.h
diff options
context:
space:
mode:
Diffstat (limited to 'absl/base/optimization.h')
-rw-r--r--absl/base/optimization.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/absl/base/optimization.h b/absl/base/optimization.h
index 646523b3..1541d7a8 100644
--- a/absl/base/optimization.h
+++ b/absl/base/optimization.h
@@ -178,4 +178,38 @@
#define ABSL_PREDICT_TRUE(x) (x)
#endif
+// ABSL_INTERNAL_ASSUME(cond)
+// Informs the compiler than a condition is always true and that it can assume
+// it to be true for optimization purposes. The call has undefined behavior if
+// the condition is false.
+// In !NDEBUG mode, the condition is checked with an assert().
+// NOTE: The expression must not have side effects, as it will only be evaluated
+// in some compilation modes and not others.
+//
+// Example:
+//
+// int x = ...;
+// ABSL_INTERNAL_ASSUME(x >= 0);
+// // The compiler can optimize the division to a simple right shift using the
+// // assumption specified above.
+// int y = x / 16;
+//
+#if !defined(NDEBUG)
+#define ABSL_INTERNAL_ASSUME(cond) assert(cond)
+#elif ABSL_HAVE_BUILTIN(__builtin_assume)
+#define ABSL_INTERNAL_ASSUME(cond) __builtin_assume(cond)
+#elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
+#define ABSL_INTERNAL_ASSUME(cond) \
+ do { \
+ if (!(cond)) __builtin_unreachable(); \
+ } while (0)
+#elif defined(_MSC_VER)
+#define ABSL_INTERNAL_ASSUME(cond) __assume(cond)
+#else
+#define ABSL_INTERNAL_ASSUME(cond) \
+ do { \
+ static_cast<void>(false && (cond)); \
+ } while (0)
+#endif
+
#endif // ABSL_BASE_OPTIMIZATION_H_