]> Savannah Git Hosting - gnulib.git/commitdiff
byteswap: Use __has_builtin portably.
authorCollin Funk <collin.funk1@gmail.com>
Fri, 17 May 2024 06:18:16 +0000 (23:18 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Fri, 17 May 2024 06:18:16 +0000 (23:18 -0700)
Reported by Paul Eggert in
<https://lists.gnu.org/archive/html/bug-gnulib/2024-05/msg00249.html>.

* lib/byteswap.in.h (_GL_BYTESWAP_HAS_BUILTIN_BSWAP16)
(_GL_BYTESWAP_HAS_BUILTIN_BSWAP32)
(_GL_BYTESWAP_HAS_BUILTIN_BSWAP64): Define using the GCC version or
__has_builtin after checking that it is defined.
(bswap_16, bswap_32, bswap_64): Use the macros.
* modules/byteswap (Depends-on): Add stdbool as a conditional
dependency.

ChangeLog
lib/byteswap.in.h
modules/byteswap

index 5238067cf986ed64417f2428b4e099d3d889131b..742a20d0120203fa74f13f18d2a815ef2bb098e5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2024-05-16  Collin Funk  <collin.funk1@gmail.com>
+
+       byteswap: Use __has_builtin portably.
+       Reported by Paul Eggert in
+       <https://lists.gnu.org/archive/html/bug-gnulib/2024-05/msg00249.html>.
+       * lib/byteswap.in.h (_GL_BYTESWAP_HAS_BUILTIN_BSWAP16)
+       (_GL_BYTESWAP_HAS_BUILTIN_BSWAP32)
+       (_GL_BYTESWAP_HAS_BUILTIN_BSWAP64): Define using the GCC version or
+       __has_builtin after checking that it is defined.
+       (bswap_16, bswap_32, bswap_64): Use the macros.
+       * modules/byteswap (Depends-on): Add stdbool as a conditional
+       dependency.
+
 2024-05-16  Paul Eggert  <eggert@cs.ucla.edu>
 
        putenv-tests: pacify gcc -Wdiscarded-qualifiers
index cfa3f04031b9232bd4022246ca98837b349c7160..ae05d59441f5db1325b3a283c1d0ad9a7ecff28e 100644 (file)
@@ -34,13 +34,32 @@ _GL_INLINE_HEADER_BEGIN
 extern "C" {
 #endif
 
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
+# define _GL_BYTESWAP_HAS_BUILTIN_BSWAP16 true
+#elif defined __has_builtin
+# if __has_builtin (__builtin_bswap16)
+#  define _GL_BYTESWAP_HAS_BUILTIN_BSWAP16 true
+# endif
+#endif
+
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
+# define _GL_BYTESWAP_HAS_BUILTIN_BSWAP32 true
+# define _GL_BYTESWAP_HAS_BUILTIN_BSWAP64 true
+#elif defined __has_builtin
+# if __has_builtin (__builtin_bswap32)
+#  define _GL_BYTESWAP_HAS_BUILTIN_BSWAP32 true
+# endif
+# if __has_builtin (__builtin_bswap64)
+#  define _GL_BYTESWAP_HAS_BUILTIN_BSWAP64 true
+# endif
+#endif
+
 /* Given an unsigned 16-bit argument X, return the value corresponding to
    X with reversed byte order.  */
 _GL_BYTESWAP_INLINE uint16_t
 bswap_16 (uint16_t x)
 {
-#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))            \
-     || (defined __has_builtin && __has_builtin (__builtin_bswap16))
+#ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP16
   return __builtin_bswap16 (x);
 #else
   return (((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)));
@@ -52,8 +71,7 @@ bswap_16 (uint16_t x)
 _GL_BYTESWAP_INLINE uint32_t
 bswap_32 (uint32_t x)
 {
-#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))            \
-     || (defined __has_builtin && __has_builtin (__builtin_bswap32))
+#ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP32
   return __builtin_bswap32 (x);
 #else
   return ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8)
@@ -66,8 +84,7 @@ bswap_32 (uint32_t x)
 _GL_BYTESWAP_INLINE uint64_t
 bswap_64 (uint64_t x)
 {
-#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))            \
-     || (defined __has_builtin && __has_builtin (__builtin_bswap64))
+#ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP64
   return __builtin_bswap64 (x);
 #else
   return ((((x) & 0xff00000000000000ull) >> 56)
index 82fe424a614ab74a9ff37cb3c88c6ca83a4af23b..6e777a699ba1d8056cea58a8134710e4627df444 100644 (file)
@@ -9,6 +9,7 @@ m4/byteswap.m4
 Depends-on:
 gen-header
 extern-inline           [$GL_GENERATE_BYTESWAP_H]
+stdbool                 [$GL_GENERATE_BYTESWAP_H]
 stdint                  [$GL_GENERATE_BYTESWAP_H]
 
 configure.ac: