Commit b148a64b authored by David Goodell's avatar David Goodell
Browse files

[svn-r7372] add likely/unlikely macros to mpiutil.h

On Blue Gene (and some other platforms) the compiler occasionally generates
suboptimal code if it guesses the likelihood of a branch incorrectly.
These macros allow us to help the compiler out in cases that it guesses
poorly.

No reviewer.
parent 946fa0a8
......@@ -172,4 +172,27 @@ int MPIR_Assert_fail_fmt(const char *cond, const char *file_name, int line_num,
#define MPIU_UNIQUE_IMPL2_(prefix_,line_) MPIU_UNIQUE_IMPL3_(prefix_,line_)
#define MPIU_UNIQUE_IMPL3_(prefix_,line_) prefix_##line_
/* These likely/unlikely macros provide static branch prediction hints to the
* compiler, if such hints are available. Simply wrap the relevant expression in
* the macro, like this:
*
* if (unlikely(ptr == NULL)) {
* // ... some unlikely code path ...
* }
*
* They should be used sparingly, especially in upper-level code. It's easy to
* incorrectly estimate branching likelihood, while the compiler can often do a
* decent job if left to its own devices.
*
* These macros are not namespaced because the namespacing is cumbersome.
*/
/* safety guard for now, add a configure check in the future */
#if defined(__GNUC__) && (__GNUC__ >= 3)
# define unlikely(x_) __builtin_expect(!!(x_),0)
# define likely(x_) __builtin_expect(!!(x_),1)
#else
# define unlikely(x_) (x_)
# define likely(x_) (x_)
#endif
#endif /* !defined(MPIUTIL_H_INCLUDED) */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment