Skip to content

Commit

Permalink
Add support for __builtin_expect extension
Browse files Browse the repository at this point in the history
And H5_LIKELY / H5_UNLIKELY macros to wrap it

Signed-off-by: Quincey Koziol <[email protected]>
  • Loading branch information
qkoziol committed Apr 19, 2024
1 parent 84e75e2 commit 551b681
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
1 change: 1 addition & 0 deletions config/cmake/ConfigureChecks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ endif ()
if (MINGW OR NOT WINDOWS)
foreach (other_test
HAVE_ATTRIBUTE
HAVE_BUILTIN_EXPECT
SYSTEM_SCOPE_THREADS
HAVE_SOCKLEN_T
)
Expand Down
3 changes: 3 additions & 0 deletions config/cmake/H5pubconf.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@
/* Define to 1 if you have the <szlib.h> header file. */
#cmakedefine H5_HAVE_SZLIB_H @H5_HAVE_SZLIB_H@

/* Define to 1 if the compiler supports the __builtin_expect() extension */
#cmakedefine H5_HAVE_BUILTIN_EXPECT @H5_HAVE_BUILTIN_EXPECT@

#if defined(_WIN32) && !defined(H5_BUILT_AS_DYNAMIC_LIB)
/* Not supported on WIN32 platforms with static linking */
/* #undef H5_HAVE_THREADSAFE */
Expand Down
15 changes: 15 additions & 0 deletions config/cmake/HDFTests.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@
#define SIMPLE_TEST(x) int main(void){ x; return 0; }


#ifdef HAVE_BUILTIN_EXPECT

int
main ()
{
void *ptr = (void*) 0;

if (__builtin_expect (ptr != (void*) 0, 1))
return 0;

return 0;
}

#endif /* HAVE_BUILTIN_EXPECT */

#ifdef HAVE_ATTRIBUTE

int
Expand Down
8 changes: 8 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2417,6 +2417,14 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]],[[int __attribute__((unused)) x]])],
AC_MSG_RESULT([yes])],
[AC_MSG_RESULT([no])])

AC_MSG_CHECKING([if compiler supports the __builtin_expect() extension])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]],[[void *ptr = (void*) 0;
if (__builtin_expect (ptr != (void*) 0, 1)) return 0;]])],
[AC_DEFINE([HAVE_BUILTIN_EXPECT], [1],
[Define if supports __builtin_expect() extension])
AC_MSG_RESULT([yes])],
[AC_MSG_RESULT([no])])

## ----------------------------------------------------------------------
## Remove old ways of determining debug/production build.
## These were used in 1.8.x and earlier. We should probably keep these checks
Expand Down
31 changes: 16 additions & 15 deletions src/H5private.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@
#include "uthash.h"

/*
* NT doesn't define SIGBUS, but since NT only runs on processors
* that do not have alignment constraints a SIGBUS would never be
* raised, so we just replace it with SIGILL (which also should
* never be raised by the hdf5 library).
* Does the compiler support the __builtin_expect() syntax?
* It's not a problem if not.
*/
#ifndef SIGBUS
#define SIGBUS SIGILL
#if H5_HAVE_BUILTIN_EXPECT
#define H5_LIKELY(expression) __builtin_expect(!!(expression), 1)
#define H5_UNLIKELY(expression) __builtin_expect(!!(expression), 0)
#else
#define H5_LIKELY(expression) (expression)
#define H5_UNLIKELY(expression) (expression)
#endif

/*
Expand Down Expand Up @@ -1217,7 +1219,7 @@ H5_DLL herr_t H5CX_pop(bool update_dxpl_props);
{ \
static bool func_check = false; \
\
if (!func_check) { \
if (H5_UNLIKELY(!func_check)) { \
/* Check function naming status */ \
assert(asrt && \
"Function naming conventions are incorrect - check H5_IS_API|PUB|PRIV|PKG macros in " \
Expand Down Expand Up @@ -1253,8 +1255,8 @@ H5_DLL herr_t H5CX_pop(bool update_dxpl_props);

#define FUNC_ENTER_API_INIT(err) \
/* Initialize the library */ \
if (!H5_INIT_GLOBAL && !H5_TERM_GLOBAL) { \
if (H5_init_library() < 0) \
if (H5_UNLIKELY(!H5_INIT_GLOBAL && !H5_TERM_GLOBAL)) { \
if (H5_UNLIKELY(H5_init_library() < 0)) \
HGOTO_ERROR(H5E_FUNC, H5E_CANTINIT, err, "library initialization failed"); \
}

Expand All @@ -1263,7 +1265,7 @@ H5_DLL herr_t H5CX_pop(bool update_dxpl_props);
H5_PUSH_FUNC \
\
/* Push the API context */ \
if (H5CX_push() < 0) \
if (H5_UNLIKELY(H5CX_push() < 0)) \
HGOTO_ERROR(H5E_FUNC, H5E_CANTSET, err, "can't set API context"); \
else \
api_ctx_pushed = true;
Expand Down Expand Up @@ -1412,7 +1414,6 @@ H5_DLL herr_t H5CX_pop(bool update_dxpl_props);
#define FUNC_ENTER_NOAPI_NOFS \
{ \
FUNC_ENTER_COMMON(!H5_IS_API(__func__)); \
\
{

/*
Expand Down Expand Up @@ -1517,12 +1518,12 @@ H5_DLL herr_t H5CX_pop(bool update_dxpl_props);
#define FUNC_LEAVE_API(ret_value) \
; \
} /*end scope from end of FUNC_ENTER*/ \
if (api_ctx_pushed) { \
if (H5_LIKELY(api_ctx_pushed)) { \
(void)H5CX_pop(true); \
api_ctx_pushed = false; \
} \
H5_POP_FUNC \
if (err_occurred) \
if (H5_UNLIKELY(err_occurred)) \
(void)H5E_dump_api_stack(true); \
FUNC_LEAVE_API_THREADSAFE \
return (ret_value); \
Expand All @@ -1534,7 +1535,7 @@ H5_DLL herr_t H5CX_pop(bool update_dxpl_props);
; \
} /*end scope from end of FUNC_ENTER*/ \
H5_POP_FUNC \
if (err_occurred) \
if (H5_UNLIKELY(err_occurred)) \
(void)H5E_dump_api_stack(true); \
FUNC_LEAVE_API_THREADSAFE \
return (ret_value); \
Expand All @@ -1557,7 +1558,7 @@ H5_DLL herr_t H5CX_pop(bool update_dxpl_props);
#define FUNC_LEAVE_API_NOPUSH(ret_value) \
; \
} /*end scope from end of FUNC_ENTER*/ \
if (err_occurred) \
if (H5_UNLIKELY(err_occurred)) \
(void)H5E_dump_api_stack(true); \
FUNC_LEAVE_API_THREADSAFE \
return (ret_value); \
Expand Down

0 comments on commit 551b681

Please sign in to comment.