Skip to content

Commit c656ed8

Browse files
authored
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
1 parent 6caaf23 commit c656ed8

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

config/HDFTests.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,6 @@ main ()
2727

2828
#endif /* HAVE___FLOAT128 */
2929

30-
#ifdef HAVE_BUILTIN_EXPECT
31-
32-
int
33-
main ()
34-
{
35-
void *ptr = (void*) 0;
36-
37-
if (__builtin_expect (ptr != (void*) 0, 1))
38-
return 0;
39-
40-
return 0;
41-
}
42-
43-
#endif /* HAVE_BUILTIN_EXPECT */
44-
4530
#ifdef HAVE_ATTRIBUTE
4631

4732
int

src/H5pubconf.h.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,6 @@
307307
/* Define to 1 if you have the <szlib.h> header file. */
308308
#cmakedefine H5_HAVE_SZLIB_H @H5_HAVE_SZLIB_H@
309309

310-
/* Define to 1 if the compiler supports the __builtin_expect() extension */
311-
#cmakedefine H5_HAVE_BUILTIN_EXPECT @H5_HAVE_BUILTIN_EXPECT@
312-
313310
/* Define if we have thread support */
314311
# cmakedefine H5_HAVE_THREADS @H5_HAVE_THREADS@
315312

src/H5public.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,22 @@ typedef void (*H5_atclose_func_t)(void *ctx);
461461
* Does the compiler support the __builtin_expect() syntax?
462462
* It's not a problem if not.
463463
*/
464+
465+
/* clang-format off */
466+
#if defined(__has_builtin)
467+
/* clang extension to check for builtins. Do this first, because clang
468+
* also defines __GNUC__ and didn't support __builtin_expect() until
469+
* more recently.
470+
*/
471+
# if __has_builtin(__builtin_expect)
472+
# define H5_HAVE_BUILTIN_EXPECT 1
473+
# endif
474+
#elif defined(__GNUC__)
475+
/* __builtin_expect() has been supported since 2.95 or 2.96 (circa 2000) */
476+
# define H5_HAVE_BUILTIN_EXPECT 1
477+
#endif
478+
/* clang-format on */
479+
464480
#if H5_HAVE_BUILTIN_EXPECT
465481
#define H5_LIKELY(expression) __builtin_expect(!!(expression), 1)
466482
#define H5_UNLIKELY(expression) __builtin_expect(!!(expression), 0)

0 commit comments

Comments
 (0)