From 3290e4c1956bc6df63af669523391565c67e8c42 Mon Sep 17 00:00:00 2001 From: Alberto Mardegan Date: Mon, 4 Apr 2011 12:28:09 +0200 Subject: Add branch prediction macros This adds support for Q_LIKELY and Q_UNLIKELY macros which instruct the compiler on which branch of an "if" statement is more likely to happen. The current patch only supports GCC's __builtin_expect(). Merge-request: 2580 Reviewed-by: ossi --- .../snippets/code/src_corelib_global_qglobal.cpp | 24 +++++++++++++++ src/corelib/global/qglobal.cpp | 34 ++++++++++++++++++++++ src/corelib/global/qglobal.h | 11 +++++++ 3 files changed, 69 insertions(+) diff --git a/doc/src/snippets/code/src_corelib_global_qglobal.cpp b/doc/src/snippets/code/src_corelib_global_qglobal.cpp index 0b54cef..c79a714 100644 --- a/doc/src/snippets/code/src_corelib_global_qglobal.cpp +++ b/doc/src/snippets/code/src_corelib_global_qglobal.cpp @@ -531,3 +531,27 @@ class MyClass : public QObject //! [47] CApaApplication *myApplicationFactory(); //! [47] + +//! [qlikely] + // the condition inside the "if" will be successful most of the times + for (int i = 1; i <= 365; i++) { + if (Q_LIKELY(isWorkingDay(i))) { + ... + } + ... + } +//! [qlikely] + +//! [qunlikely] +bool readConfiguration(const QFile &file) +{ + // We expect to be asked to read an existing file + if (Q_UNLIKELY(!file.exists())) { + qWarning() << "File not found"; + return false; + } + + ... + return true; +} +//! [qunlikely] diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 54b9d89..b9b875f 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -2870,6 +2870,40 @@ int qrand() */ /*! + \macro Q_LIKELY(expr) + \relates + \since 4.8 + + \brief Hints the compiler that the enclosed condition is likely to evaluate + to \c true. + + Use of this macro can help the compiler to optimize the code. + + Example: + + \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp qlikely + + \sa Q_UNLIKELY() +*/ + +/*! + \macro Q_UNLIKELY(expr) + \relates + \since 4.8 + + \brief Hints the compiler that the enclosed condition is likely to evaluate + to \c false. + + Use of this macro can help the compiler to optimize the code. + + Example: + + \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp qunlikely + + \sa Q_LIKELY() +*/ + +/*! \macro QT_POINTER_SIZE \relates diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 8dd8850..07e0d4c 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -497,6 +497,10 @@ namespace QT_NAMESPACE {} # define Q_TYPEOF(expr) __typeof__(expr) # define Q_DECL_ALIGN(n) __attribute__((__aligned__(n))) # endif +# if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) +# define Q_LIKELY(expr) __builtin_expect(!!(expr), true) +# define Q_UNLIKELY(expr) __builtin_expect(!!(expr), false) +# endif /* GCC 3.1 and GCC 3.2 wrongly define _SB_CTYPE_MACROS on HP-UX */ # if defined(Q_OS_HPUX) && __GNUC__ == 3 && __GNUC_MINOR__ >= 1 # define Q_WRONG_SB_CTYPE_MACROS @@ -801,6 +805,13 @@ namespace QT_NAMESPACE {} # undef Q_NO_PACKED_REFERENCE #endif +#ifndef Q_LIKELY +# define Q_LIKELY(x) (x) +#endif +#ifndef Q_UNLIKELY +# define Q_UNLIKELY(x) (x) +#endif + #ifndef Q_CONSTRUCTOR_FUNCTION # define Q_CONSTRUCTOR_FUNCTION0(AFUNC) \ static const int AFUNC ## __init_variable__ = AFUNC(); -- cgit v0.12