From b0afb8fe0ff1d87165af9df61501197a06240dda Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 28 Dec 2025 12:13:40 +0100 Subject: Sync with Gnulib stable-202507 code (a8ac9f9ce5) --- gl/byteswap.in.h | 106 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 90 insertions(+), 16 deletions(-) (limited to 'gl/byteswap.in.h') diff --git a/gl/byteswap.in.h b/gl/byteswap.in.h index 8e49efad..6b4fbabf 100644 --- a/gl/byteswap.in.h +++ b/gl/byteswap.in.h @@ -1,5 +1,5 @@ /* byteswap.h - Byte swapping - Copyright (C) 2005, 2007, 2009-2024 Free Software Foundation, Inc. + Copyright (C) 2005, 2007, 2009-2025 Free Software Foundation, Inc. Written by Oskar Liljeblad , 2005. This file is free software: you can redistribute it and/or modify @@ -16,29 +16,103 @@ along with this program. If not, see . */ #ifndef _GL_BYTESWAP_H -#define _GL_BYTESWAP_H +#define _GL_BYTESWAP_H 1 + +/* This file uses _GL_INLINE. */ +#if !_GL_CONFIG_H_INCLUDED + #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. */ +#ifndef _GL_BYTESWAP_INLINE +# define _GL_BYTESWAP_INLINE _GL_INLINE +#endif + +#include + +_GL_INLINE_HEADER_BEGIN + +#ifdef __cplusplus +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. */ -#define bswap_16(x) ((((x) & 0x00FF) << 8) | \ - (((x) & 0xFF00) >> 8)) +_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 +} /* Given an unsigned 32-bit argument X, return the value corresponding to X with reversed byte order. */ -#define bswap_32(x) ((((x) & 0x000000FF) << 24) | \ - (((x) & 0x0000FF00) << 8) | \ - (((x) & 0x00FF0000) >> 8) | \ - (((x) & 0xFF000000) >> 24)) +_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 +} +#ifdef UINT_LEAST64_MAX /* Given an unsigned 64-bit argument X, return the value corresponding to X with reversed byte order. */ -#define bswap_64(x) ((((x) & 0x00000000000000FFULL) << 56) | \ - (((x) & 0x000000000000FF00ULL) << 40) | \ - (((x) & 0x0000000000FF0000ULL) << 24) | \ - (((x) & 0x00000000FF000000ULL) << 8) | \ - (((x) & 0x000000FF00000000ULL) >> 8) | \ - (((x) & 0x0000FF0000000000ULL) >> 24) | \ - (((x) & 0x00FF000000000000ULL) >> 40) | \ - (((x) & 0xFF00000000000000ULL) >> 56)) +_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 +} +#endif + +#ifdef __cplusplus +} +#endif + +_GL_INLINE_HEADER_END #endif /* _GL_BYTESWAP_H */ -- cgit v1.2.3-74-g34f1