Prefer the usual pattern for checking for builtins:
allow __has_builtin for compilers that are not Clang or GCC
where we already have something.
* lib/stdbit.in.h (_GL_HAS_BUILTIN_ASSUME_ALIGNED): New macro.
(_GL_STDBIT_ASSUME_ALIGNED): Use it.
---
 ChangeLog       |  9 +++++++++
 lib/stdbit.in.h | 19 +++++++++++++------
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3caee4a011..ae4a2df749 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2026-03-14  Paul Eggert  <[email protected]>
+
+       stdbit-h: __has_builtin (__builtin_assume_aligned)
+       Prefer the usual pattern for checking for builtins:
+       allow __has_builtin for compilers that are not Clang or GCC
+       where we already have something.
+       * lib/stdbit.in.h (_GL_HAS_BUILTIN_ASSUME_ALIGNED): New macro.
+       (_GL_STDBIT_ASSUME_ALIGNED): Use it.
+
 2026-03-13  Paul Eggert  <[email protected]>
 
        stdbit-h: restore optimization for RISC-V etc
diff --git a/lib/stdbit.in.h b/lib/stdbit.in.h
index 8e24060f3a..57972c2680 100644
--- a/lib/stdbit.in.h
+++ b/lib/stdbit.in.h
@@ -1208,7 +1208,8 @@ stdc_bit_ceil_ull (unsigned long long int n)
      - With MSVC 14: Only c,e are good; d,f medium.
 
    So, we use the following heuristic for getting good code:
-     - gcc >= 4.7, g++ >= 4.9, clang >= 4: Use variant a.
+     - gcc >= 4.7, g++ >= 4.9, clang >= 4, or any other platform
+       with __builtin_assume_aligned: Use variant a.
      - MSVC: Use variant e.
      - Otherwise: Use variant f.
  */
@@ -1217,15 +1218,21 @@ stdc_bit_ceil_ull (unsigned long long int n)
       && (defined __cplusplus \
           ? __GNUC__ + (__GNUC_MINOR__ >= 9) > 4 \
           : __GNUC__ + (__GNUC_MINOR__ >= 7) > 4)))
+# define _GL_HAS_BUILTIN_ASSUME_ALIGNED 1
+#elif defined __has_builtin
+# if __has_builtin (__builtin_assume_aligned)
+#  define _GL_HAS_BUILTIN_ASSUME_ALIGNED 1
+# endif
+#endif
+#ifdef _GL_HAS_BUILTIN_ASSUME_ALIGNED
 # define _GL_STDBIT_ASSUME_ALIGNED(ptr, align) \
     __builtin_assume_aligned (ptr, align)
-# define _GL_STDBIT_OPTIMIZE_VIA_MEMCPY 1
-#elif defined _MSC_VER
-# define _GL_STDBIT_OPTIMIZE_VIA_MEMCPY 1
+#else
+# define _GL_STDBIT_ASSUME_ALIGNED(ptr, align) (ptr)
 #endif
 
-#ifndef _GL_STDBIT_ASSUME_ALIGNED
-# define _GL_STDBIT_ASSUME_ALIGNED(ptr, align) (ptr)
+#if defined _GL_HAS_BUILTIN_ASSUME_ALIGNED || defined _MSC_VER
+# define _GL_STDBIT_OPTIMIZE_VIA_MEMCPY 1
 #endif
 
 #ifndef _GL_STDBIT_OPTIMIZE_VIA_MEMCPY
-- 
2.51.0


Reply via email to