From 4afc10ef28d9df8b212dbeb28d0ea4dab6519699 Mon Sep 17 00:00:00 2001 From: Matevz Kovacic <117923752+matevz-kovacic@users.noreply.github.com> Date: Thu, 6 Aug 2026 08:49:52 +0200 Subject: [PATCH] compiler: honour LIKELY/UNLIKELY under clang-cl Same __GNUC__ gate as TARGET_ATTRIBUTE. clang-cl supports __builtin_expect, so test for it with __has_builtin and keep __GNUC__ as a fallback. Without this, every branch hint in the library is discarded under clang-cl. --- lib/common/compiler.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/common/compiler.h b/lib/common/compiler.h index 135149599..2cb8b1615 100644 --- a/lib/common/compiler.h +++ b/lib/common/compiler.h @@ -201,7 +201,16 @@ * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc * and clang, please do. */ -#if defined(__GNUC__) +#if defined(__has_builtin) +# if __has_builtin(__builtin_expect) +# define ZSTD_HAS_BUILTIN_EXPECT 1 +# endif +#endif +#if !defined(ZSTD_HAS_BUILTIN_EXPECT) && defined(__GNUC__) +# define ZSTD_HAS_BUILTIN_EXPECT 1 +#endif + +#if defined(ZSTD_HAS_BUILTIN_EXPECT) #define LIKELY(x) (__builtin_expect((x), 1)) #define UNLIKELY(x) (__builtin_expect((x), 0)) #else