From 464cd5fc8b4bc327503bd55ccbf0e25c749d2bad Mon Sep 17 00:00:00 2001 From: rdb Date: Mon, 9 Oct 2017 00:05:00 +0200 Subject: [PATCH] Replace __builtin_expect macros with LIKELY/UNLIKELY --- dtool/src/dtoolbase/dtoolbase.h | 8 ++++++++ dtool/src/interrogate/interfaceMakerPython.cxx | 2 +- dtool/src/interrogate/interfaceMakerPythonNative.cxx | 2 +- dtool/src/interrogatedb/py_panda.cxx | 12 ++++++------ dtool/src/interrogatedb/py_panda.h | 4 ++-- dtool/src/prc/notifyCategory.I | 12 ++---------- dtool/src/prc/notifyCategoryProxy.I | 12 ++---------- 7 files changed, 22 insertions(+), 30 deletions(-) diff --git a/dtool/src/dtoolbase/dtoolbase.h b/dtool/src/dtoolbase/dtoolbase.h index 5afa70df03..2029555e3b 100644 --- a/dtool/src/dtoolbase/dtoolbase.h +++ b/dtool/src/dtoolbase/dtoolbase.h @@ -94,6 +94,14 @@ #define RETURNS_ALIGNED(x) #endif +#ifdef __GNUC__ +#define LIKELY(x) __builtin_expect(!!(x), 1) +#define UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +#define LIKELY(x) (x) +#define UNLIKELY(x) (x) +#endif + /* include win32 defns for everything up to WinServer2003, and assume I'm smart enough to use GetProcAddress for backward compat on diff --git a/dtool/src/interrogate/interfaceMakerPython.cxx b/dtool/src/interrogate/interfaceMakerPython.cxx index f799335dfa..37125a7e64 100644 --- a/dtool/src/interrogate/interfaceMakerPython.cxx +++ b/dtool/src/interrogate/interfaceMakerPython.cxx @@ -51,7 +51,7 @@ test_assert(ostream &out, int indent_level) const { indent(out, indent_level) << "Notify *notify = Notify::ptr();\n"; indent(out, indent_level) - << "if (notify->has_assert_failed()) {\n"; + << "if (UNLIKELY(notify->has_assert_failed())) {\n"; indent(out, indent_level + 2) << "PyErr_SetString(PyExc_AssertionError, notify->get_assert_error_message().c_str());\n"; indent(out, indent_level + 2) diff --git a/dtool/src/interrogate/interfaceMakerPythonNative.cxx b/dtool/src/interrogate/interfaceMakerPythonNative.cxx index a5e5295e4e..1d2f2900d8 100644 --- a/dtool/src/interrogate/interfaceMakerPythonNative.cxx +++ b/dtool/src/interrogate/interfaceMakerPythonNative.cxx @@ -6000,7 +6000,7 @@ write_function_instance(ostream &out, FunctionRemap *remap, indent(out, indent_level) << "Notify *notify = Notify::ptr();\n"; indent(out, indent_level) - << "if (notify->has_assert_failed()) {\n"; + << "if (UNLIKELY(notify->has_assert_failed())) {\n"; if (manage_return) { // Output code to delete any temporary object we may have allocated. diff --git a/dtool/src/interrogatedb/py_panda.cxx b/dtool/src/interrogatedb/py_panda.cxx index d629fd6d60..76c4e7cf1b 100644 --- a/dtool/src/interrogatedb/py_panda.cxx +++ b/dtool/src/interrogatedb/py_panda.cxx @@ -322,11 +322,11 @@ PyObject *_Dtool_Raise_BadArgumentsError() { * NULL, otherwise Py_None. */ PyObject *_Dtool_Return_None() { - if (_PyErr_OCCURRED()) { + if (UNLIKELY(_PyErr_OCCURRED())) { return NULL; } #ifndef NDEBUG - if (Notify::ptr()->has_assert_failed()) { + if (UNLIKELY(Notify::ptr()->has_assert_failed())) { return Dtool_Raise_AssertionError(); } #endif @@ -339,11 +339,11 @@ PyObject *_Dtool_Return_None() { * NULL, otherwise the given boolean value as a PyObject *. */ PyObject *Dtool_Return_Bool(bool value) { - if (_PyErr_OCCURRED()) { + if (UNLIKELY(_PyErr_OCCURRED())) { return NULL; } #ifndef NDEBUG - if (Notify::ptr()->has_assert_failed()) { + if (UNLIKELY(Notify::ptr()->has_assert_failed())) { return Dtool_Raise_AssertionError(); } #endif @@ -358,11 +358,11 @@ PyObject *Dtool_Return_Bool(bool value) { * increased. */ PyObject *_Dtool_Return(PyObject *value) { - if (_PyErr_OCCURRED()) { + if (UNLIKELY(_PyErr_OCCURRED())) { return NULL; } #ifndef NDEBUG - if (Notify::ptr()->has_assert_failed()) { + if (UNLIKELY(Notify::ptr()->has_assert_failed())) { return Dtool_Raise_AssertionError(); } #endif diff --git a/dtool/src/interrogatedb/py_panda.h b/dtool/src/interrogatedb/py_panda.h index 21b4a8b53e..c341bb6b33 100644 --- a/dtool/src/interrogatedb/py_panda.h +++ b/dtool/src/interrogatedb/py_panda.h @@ -320,9 +320,9 @@ EXPCL_INTERROGATEDB bool _Dtool_CheckErrorOccurred(); #endif #ifdef NDEBUG -#define Dtool_CheckErrorOccurred() (_PyErr_OCCURRED() != NULL) +#define Dtool_CheckErrorOccurred() (UNLIKELY(_PyErr_OCCURRED() != NULL)) #else -#define Dtool_CheckErrorOccurred() _Dtool_CheckErrorOccurred() +#define Dtool_CheckErrorOccurred() (UNLIKELY(_Dtool_CheckErrorOccurred())) #endif EXPCL_INTERROGATEDB PyObject *Dtool_Raise_AssertionError(); diff --git a/dtool/src/prc/notifyCategory.I b/dtool/src/prc/notifyCategory.I index 07d3634d92..46aca56bb2 100644 --- a/dtool/src/prc/notifyCategory.I +++ b/dtool/src/prc/notifyCategory.I @@ -71,11 +71,7 @@ is_on(NotifySeverity severity) const { INLINE bool NotifyCategory:: is_spam() const { // Instruct the compiler to optimize for the usual case. -#ifdef __GNUC__ - return __builtin_expect(is_on(NS_spam), 0); -#else - return is_on(NS_spam); -#endif + return UNLIKELY(is_on(NS_spam)); } /** @@ -84,11 +80,7 @@ is_spam() const { INLINE bool NotifyCategory:: is_debug() const { // Instruct the compiler to optimize for the usual case. -#ifdef __GNUC__ - return __builtin_expect(is_on(NS_debug), 0); -#else - return is_on(NS_debug); -#endif + return UNLIKELY(is_on(NS_debug)); } #else /** diff --git a/dtool/src/prc/notifyCategoryProxy.I b/dtool/src/prc/notifyCategoryProxy.I index ae829eeedb..9a05f35625 100644 --- a/dtool/src/prc/notifyCategoryProxy.I +++ b/dtool/src/prc/notifyCategoryProxy.I @@ -70,11 +70,7 @@ template INLINE bool NotifyCategoryProxy:: is_spam() { // Instruct the compiler to optimize for the usual case. -#ifdef __GNUC__ - return __builtin_expect(get_unsafe_ptr()->is_spam(), 0); -#else - return get_unsafe_ptr()->is_spam(); -#endif + return UNLIKELY(get_unsafe_ptr()->is_spam()); } #else template @@ -92,11 +88,7 @@ template INLINE bool NotifyCategoryProxy:: is_debug() { // Instruct the compiler to optimize for the usual case. -#ifdef __GNUC__ - return __builtin_expect(get_unsafe_ptr()->is_debug(), 0); -#else - return get_unsafe_ptr()->is_debug(); -#endif + return UNLIKELY(get_unsafe_ptr()->is_debug()); } #else template