]> Savannah Git Hosting - gnulib.git/commitdiff
byteswap: pacify GCC 4.4.7 and older
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 18 May 2024 14:48:47 +0000 (07:48 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 18 May 2024 14:56:27 +0000 (07:56 -0700)
Problem reported by Bruno Haible in:
https://lists.gnu.org/r/bug-gnulib/2024-05/msg00277.html
* lib/byteswap.in.h (bswap_16, bswap_32, bswap_64):
Compute the mask rather than using long constants like
0xff00000000000000 that may generate bogus warnings.

ChangeLog
lib/byteswap.in.h

index 29adf56ab7204366db4a2cc62ff8dff6d849bb61..79f813e1b2cfd8001a6e2ea5cbaf00c9864a1c9e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-05-18  Paul Eggert  <eggert@cs.ucla.edu>
+
+       byteswap: pacify GCC 4.4.7 and older
+       Problem reported by Bruno Haible in:
+       https://lists.gnu.org/r/bug-gnulib/2024-05/msg00277.html
+       * lib/byteswap.in.h (bswap_16, bswap_32, bswap_64):
+       Compute the mask rather than using long constants like
+       0xff00000000000000 that may generate bogus warnings.
+
 2024-05-18  Bruno Haible  <bruno@clisp.org>
 
        endian tests: Verify that it can be used from C++.
index 0e760005c21304022c5ad391e65209211db0f2f1..4be335d9158b2c46dd173bc7152f0e00ff062849 100644 (file)
@@ -62,8 +62,9 @@ bswap_16 (uint_least16_t x)
 #ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP16
   return __builtin_bswap16 (x);
 #else
-  return (  (x & 0xff00) >> 8
-          | (x & 0x00ff) << 8);
+  uint_fast16_t mask = 0xff;
+  return (  (x & mask << 8 * 1) >> 8 * 1
+          | (x & mask << 8 * 0) << 8 * 1);
 #endif
 }
 
@@ -75,10 +76,11 @@ bswap_32 (uint_least32_t x)
 #ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP32
   return __builtin_bswap32 (x);
 #else
-  return (  (x & 0xff000000) >> 24
-          | (x & 0x00ff0000) >>  8
-          | (x & 0x0000ff00) <<  8
-          | (x & 0x000000ff) << 24);
+  uint_fast32_t mask = 0xff;
+  return (  (x & mask << 8 * 3) >> 8 * 3
+          | (x & mask << 8 * 2) >> 8 * 1
+          | (x & mask << 8 * 1) << 8 * 1
+          | (x & mask << 8 * 0) << 8 * 3);
 #endif
 }
 
@@ -91,14 +93,15 @@ bswap_64 (uint_least64_t x)
 # ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP64
   return __builtin_bswap64 (x);
 # else
-  return (  (x & 0xff00000000000000) >> 56
-          | (x & 0x00ff000000000000) >> 40
-          | (x & 0x0000ff0000000000) >> 24
-          | (x & 0x000000ff00000000) >>  8
-          | (x & 0x00000000ff000000) <<  8
-          | (x & 0x0000000000ff0000) << 24
-          | (x & 0x000000000000ff00) << 40
-          | (x & 0x00000000000000ff) << 56);
+  uint_fast64_t mask = 0xff;
+  return (  (x & mask << 8 * 7) >> 8 * 7
+          | (x & mask << 8 * 6) >> 8 * 5
+          | (x & mask << 8 * 5) >> 8 * 3
+          | (x & mask << 8 * 4) >> 8 * 1
+          | (x & mask << 8 * 3) << 8 * 1
+          | (x & mask << 8 * 2) << 8 * 3
+          | (x & mask << 8 * 1) << 8 * 5
+          | (x & mask << 8 * 0) << 8 * 7);
 # endif
 }
 #endif