From b4697f09e79b0265f0be813b5602d87640ff62fe Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Fri, 3 Oct 2025 10:46:07 -0700 Subject: [PATCH] Conditionalize __builtin_prefetch() on it existing!! You have to check that something that isn't standard C actually exists before trying to use it... Cc: Colin Ian King Signed-off-by: H. Peter Anvin (Intel) --- configure.ac | 1 + include/compiler.h | 8 +++++++- nasmlib/hashtbl.c | 4 ++-- nasmlib/strlist.c | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 4926e55d6..fae077b71 100644 --- a/configure.ac +++ b/configure.ac @@ -278,6 +278,7 @@ dnl Some rather useful gcc extensions... dnl PA_HAVE_FUNC(__builtin_constant_p, (0)) PA_HAVE_FUNC(__builtin_choose_expr, (0,1,2)) +PA_HAVE_FUNC(__builtin_prefetch, (NULL)) dnl dnl Check for supported gcc attributes; some compilers (e.g. Sun CC) diff --git a/include/compiler.h b/include/compiler.h index c24012363..0ecd4e8d4 100644 --- a/include/compiler.h +++ b/include/compiler.h @@ -320,7 +320,7 @@ static inline void *mempset(void *dst, int c, size_t n) * Hints to the compiler that a particular branch of code is more or * less likely to be taken. */ -#if HAVE___BUILTIN_EXPECT +#ifdef HAVE___BUILTIN_EXPECT # define likely(x) __builtin_expect(bool(x), true) # define unlikely(x) __builtin_expect(bool(x), false) #else @@ -328,6 +328,12 @@ static inline void *mempset(void *dst, int c, size_t n) # define unlikely(x) bool(x) #endif +#ifdef HAVE___BUILTIN_PREFETCH +# define prefetch(x) __builtin_prefetch(x) +#else +# define prefetch(x) ((void)(x)) +#endif + /* * Attributes */ diff --git a/nasmlib/hashtbl.c b/nasmlib/hashtbl.c index b53ed5b74..5faba36b6 100644 --- a/nasmlib/hashtbl.c +++ b/nasmlib/hashtbl.c @@ -80,7 +80,7 @@ void **hash_findb(struct hash_table *head, const void *key, void **hash_find(struct hash_table *head, const char *key, struct hash_insert *insert) { - __builtin_prefetch(key); + prefetch(key); return hash_findb(head, key, strlen(key)+1, insert); } @@ -126,7 +126,7 @@ void **hash_findib(struct hash_table *head, const void *key, size_t keylen, void **hash_findi(struct hash_table *head, const char *key, struct hash_insert *insert) { - __builtin_prefetch(key); + prefetch(key); return hash_findib(head, key, strlen(key)+1, insert); } diff --git a/nasmlib/strlist.c b/nasmlib/strlist.c index ae6a3f2a0..0da9e67cb 100644 --- a/nasmlib/strlist.c +++ b/nasmlib/strlist.c @@ -48,7 +48,7 @@ strlist_add(struct strlist *list, const char *str) struct hash_insert hi; size_t size; - __builtin_prefetch(str); + prefetch(str); if (!list) return NULL;