summaryrefslogtreecommitdiff
path: root/lib/byteswap.in.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/byteswap.in.h')
-rw-r--r--lib/byteswap.in.h59
1 files changed, 7 insertions, 52 deletions
diff --git a/lib/byteswap.in.h b/lib/byteswap.in.h
index b2b26af8b03..02433d30da4 100644
--- a/lib/byteswap.in.h
+++ b/lib/byteswap.in.h
@@ -23,13 +23,14 @@
#error "Please include config.h first."
#endif
-/* Define this now, rather than after including stdint.h, in case
- stdint.h recursively includes us. This is for Gnulib endian.h. */
+/* Define this now, rather than after including stdbit.h, in case stdbit.h
+ recursively includes us via stdint.h. This is for Gnulib endian.h. */
#ifndef _GL_BYTESWAP_INLINE
# define _GL_BYTESWAP_INLINE _GL_INLINE
#endif
-#include <stdint.h>
+#include <stdbit.h> /* for stdc_memreverse8u* */
+#include <stdint.h> /* for UINT_LEAST64_MAX */
_GL_INLINE_HEADER_BEGIN
@@ -37,38 +38,12 @@ _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 uint_least16_t
bswap_16 (uint_least16_t x)
{
-#ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP16
- return __builtin_bswap16 (x);
-#else
- uint_fast16_t mask = 0xff;
- return ( (x & mask << 8 * 1) >> 8 * 1
- | (x & mask << 8 * 0) << 8 * 1);
-#endif
+ return stdc_memreverse8u16 (x);
}
/* Given an unsigned 32-bit argument X, return the value corresponding to
@@ -76,15 +51,7 @@ bswap_16 (uint_least16_t x)
_GL_BYTESWAP_INLINE uint_least32_t
bswap_32 (uint_least32_t x)
{
-#ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP32
- return __builtin_bswap32 (x);
-#else
- 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
+ return stdc_memreverse8u32 (x);
}
#ifdef UINT_LEAST64_MAX
@@ -93,19 +60,7 @@ bswap_32 (uint_least32_t x)
_GL_BYTESWAP_INLINE uint_least64_t
bswap_64 (uint_least64_t x)
{
-# ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP64
- return __builtin_bswap64 (x);
-# else
- 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
+ return stdc_memreverse8u64 (x);
}
#endif