mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Add branch prediction to Ensures / Expects
This commit is contained in:
commit
0edabdba0c
@ -38,6 +38,14 @@
|
|||||||
#define GSL_STRINGIFY_DETAIL(x) #x
|
#define GSL_STRINGIFY_DETAIL(x) #x
|
||||||
#define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(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
|
// GSL.assert: assertions
|
||||||
//
|
//
|
||||||
@ -53,19 +61,19 @@ struct fail_fast : public std::runtime_error
|
|||||||
#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
|
#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
|
||||||
|
|
||||||
#define Expects(cond) \
|
#define Expects(cond) \
|
||||||
if (!(cond)) \
|
if (GSL_UNLIKELY(!(cond))) \
|
||||||
throw gsl::fail_fast("GSL: Precondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__));
|
throw gsl::fail_fast("GSL: Precondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__));
|
||||||
#define Ensures(cond) \
|
#define Ensures(cond) \
|
||||||
if (!(cond)) \
|
if (GSL_UNLIKELY(!(cond))) \
|
||||||
throw gsl::fail_fast("GSL: Postcondition failure at " __FILE__ \
|
throw gsl::fail_fast("GSL: Postcondition failure at " __FILE__ \
|
||||||
": " GSL_STRINGIFY(__LINE__));
|
": " GSL_STRINGIFY(__LINE__));
|
||||||
|
|
||||||
#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
|
#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
|
||||||
|
|
||||||
#define Expects(cond) \
|
#define Expects(cond) \
|
||||||
if (!(cond)) std::terminate();
|
if (GSL_UNLIKELY(!(cond))) std::terminate();
|
||||||
#define Ensures(cond) \
|
#define Ensures(cond) \
|
||||||
if (!(cond)) std::terminate();
|
if (GSL_UNLIKELY(!(cond))) std::terminate();
|
||||||
|
|
||||||
#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION)
|
#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user