aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeil MacIntosh <neilmac@microsoft.com>2016-09-29 12:58:55 -0700
committerGitHub <noreply@github.com>2016-09-29 12:58:55 -0700
commit0edabdba0c717e4d87d9385616c3965c478d0d6f (patch)
treed31f4974f995f81398451c6c3e962a46f56b5891
parent3e5af6dc4028416603e5d7a4adc93995eb34345f (diff)
parent3161d6133fb0c05785e657acd4b178d0253e8319 (diff)
downloadplatform_external_Microsoft-GSL-0edabdba0c717e4d87d9385616c3965c478d0d6f.tar.gz
platform_external_Microsoft-GSL-0edabdba0c717e4d87d9385616c3965c478d0d6f.tar.bz2
platform_external_Microsoft-GSL-0edabdba0c717e4d87d9385616c3965c478d0d6f.zip
Add branch prediction to Ensures / Expects
-rw-r--r--gsl/gsl_assert16
1 files changed, 12 insertions, 4 deletions
diff --git a/gsl/gsl_assert b/gsl/gsl_assert
index 10de31a..6d8760d 100644
--- a/gsl/gsl_assert
+++ b/gsl/gsl_assert
@@ -38,6 +38,14 @@
#define GSL_STRINGIFY_DETAIL(x) #x
#define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)
+#if defined(__clang__) || defined(__GNUC__)
+#define GSL_LIKELY(x) __builtin_expect (!!(x), 1)
+#define GSL_UNLIKELY(x) __builtin_expect (!!(x), 0)
+#else
+#define GSL_LIKELY(x) (x)
+#define GSL_UNLIKELY(x) (x)
+#endif
+
//
// GSL.assert: assertions
//
@@ -53,19 +61,19 @@ struct fail_fast : public std::runtime_error
#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
#define Expects(cond) \
- if (!(cond)) \
+ if (GSL_UNLIKELY(!(cond))) \
throw gsl::fail_fast("GSL: Precondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__));
#define Ensures(cond) \
- if (!(cond)) \
+ if (GSL_UNLIKELY(!(cond))) \
throw gsl::fail_fast("GSL: Postcondition failure at " __FILE__ \
": " GSL_STRINGIFY(__LINE__));
#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
#define Expects(cond) \
- if (!(cond)) std::terminate();
+ if (GSL_UNLIKELY(!(cond))) std::terminate();
#define Ensures(cond) \
- if (!(cond)) std::terminate();
+ if (GSL_UNLIKELY(!(cond))) std::terminate();
#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION)