Added BOOST_NOINLINE, BOOST_LIKELY and BOOST_UNLIKELY macros.

[SVN r84679]
This commit is contained in:
Andrey Semashev
2013-06-07 20:05:18 +00:00
parent 8e79155e47
commit f419d4ffd5
5 changed files with 79 additions and 3 deletions

View File

@@ -1003,6 +1003,34 @@ Note that use of this macro can lead to cryptic error messages with some compile
Consider defining it to `inline` before including the Boost.Config header in order to be
able to debug errors more easily.
]]
[[`BOOST_NOINLINE`][
This macro can be used in place of the `inline` keyword to instruct the compiler
that a function should never be inlined. One should typically use this macro
to mark functions that are unlikely to be called, such as error handling routines.
Usage example:
``
BOOST_NOINLINE void handle_error(const char* descr)
{
// ...
}
``
]]
[[`BOOST_LIKELY(X)`
`BOOST_UNLIKELY(X)`][
These macros communicate to the compiler that the conditional expression `X` is likely
or unlikely to yield a positive result. The expression should result in a boolean value.
The result of the macro is an integer or boolean value equivalent to the result of `X`.
The macros are intended to be used in branching statements. The additional hint they provide
can be used by the compiler to arrange the compiled code of the branches more effectively.
Usage example:
``
if (BOOST_UNLIKELY(ptr == NULL))
handle_error("ptr is NULL");
``
]]
]
[endsect]

View File

@@ -26,6 +26,14 @@
#define BOOST_HAS_NRVO
// Branch prediction hints
#if defined(__has_builtin)
#if __has_builtin(__builtin_expect)
#define BOOST_LIKELY(x) __builtin_expect(x, 1)
#define BOOST_UNLIKELY(x) __builtin_expect(x, 0)
#endif
#endif
// Clang supports "long long" in all compilation modes.
#define BOOST_HAS_LONG_LONG

View File

@@ -118,6 +118,10 @@
#define BOOST_HAS_NRVO
#endif
// Branch prediction hints
#define BOOST_LIKELY(x) __builtin_expect(x, 1)
#define BOOST_UNLIKELY(x) __builtin_expect(x, 0)
//
// Dynamic shared object (DSO) and dynamic-link library (DLL) support
//

View File

@@ -154,6 +154,14 @@ template<> struct assert_intrinsic_wchar_t<unsigned short> {};
# define BOOST_HAS_NRVO
#endif
// Branch prediction hints
// I'm not sure 8.0 was the first version to support these builtins,
// update the condition if the version is not accurate. (Andrey Semashev)
#if defined(__GNUC__) && BOOST_INTEL_CXX_VERSION >= 800
#define BOOST_LIKELY(x) __builtin_expect(x, 1)
#define BOOST_UNLIKELY(x) __builtin_expect(x, 0)
#endif
//
// versions check:
// we don't support Intel prior to version 5.0:

View File

@@ -648,6 +648,34 @@ namespace std{ using ::type_info; }
# endif
#endif
// BOOST_NOINLINE ---------------------------------------------//
// Macro to use in place of 'inline' to prevent a function to be inlined
#if !defined(BOOST_NOINLINE)
# if defined(_MSC_VER)
# define BOOST_NOINLINE __declspec(noinline)
# elif defined(__GNUC__) && __GNUC__ > 3
// Clang also defines __GNUC__ (as 4)
# define BOOST_NOINLINE __attribute__ ((__noinline__))
# else
# define BOOST_NOINLINE
# endif
#endif
// Branch prediction hints
// These macros are intended to wrap conditional expressions that yield true or false
//
// if (BOOST_LIKELY(var == 10))
// {
// // the most probable code here
// }
//
#if !defined(BOOST_LIKELY)
# define BOOST_LIKELY(x) x
#endif
#if !defined(BOOST_UNLIKELY)
# define BOOST_UNLIKELY(x) x
#endif
//
// Set BOOST_NO_DECLTYPE_N3276 when BOOST_NO_DECLTYPE is defined
//