Fix protection for __builtin_expect() in H5public.h (#5840)

This feature is required to be in H5public.h, but is protected by
an HDF5 feature test macro that will soon be private. There is also
no guarantee that the compiler used to build the software has the
same support for __builtin_expect() that the application compiler
has.

This PR updates the feature test checks for __builtin_expect()

Partial fix for #5819
This commit is contained in:
Dana Robinson
2025-09-16 22:05:55 -05:00
committed by GitHub
parent 6caaf23f6c
commit c656ed8a27
3 changed files with 16 additions and 18 deletions
-15
View File
@@ -27,21 +27,6 @@ main ()
#endif /* HAVE___FLOAT128 */
#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
-3
View File
@@ -307,9 +307,6 @@
/* 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@
/* Define if we have thread support */
# cmakedefine H5_HAVE_THREADS @H5_HAVE_THREADS@
+16
View File
@@ -461,6 +461,22 @@ typedef void (*H5_atclose_func_t)(void *ctx);
* Does the compiler support the __builtin_expect() syntax?
* It's not a problem if not.
*/
/* clang-format off */
#if defined(__has_builtin)
/* clang extension to check for builtins. Do this first, because clang
* also defines __GNUC__ and didn't support __builtin_expect() until
* more recently.
*/
# if __has_builtin(__builtin_expect)
# define H5_HAVE_BUILTIN_EXPECT 1
# endif
#elif defined(__GNUC__)
/* __builtin_expect() has been supported since 2.95 or 2.96 (circa 2000) */
# define H5_HAVE_BUILTIN_EXPECT 1
#endif
/* clang-format on */
#if H5_HAVE_BUILTIN_EXPECT
#define H5_LIKELY(expression) __builtin_expect(!!(expression), 1)
#define H5_UNLIKELY(expression) __builtin_expect(!!(expression), 0)