diff options
Diffstat (limited to 'gl/byteswap.in.h')
| -rw-r--r-- | gl/byteswap.in.h | 106 |
1 files changed, 90 insertions, 16 deletions
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 @@ | |||
| 1 | /* byteswap.h - Byte swapping | 1 | /* byteswap.h - Byte swapping |
| 2 | Copyright (C) 2005, 2007, 2009-2024 Free Software Foundation, Inc. | 2 | Copyright (C) 2005, 2007, 2009-2025 Free Software Foundation, Inc. |
| 3 | Written by Oskar Liljeblad <oskar@osk.mine.nu>, 2005. | 3 | Written by Oskar Liljeblad <oskar@osk.mine.nu>, 2005. |
| 4 | 4 | ||
| 5 | This file is free software: you can redistribute it and/or modify | 5 | This file is free software: you can redistribute it and/or modify |
| @@ -16,29 +16,103 @@ | |||
| 16 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ | 16 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 | 17 | ||
| 18 | #ifndef _GL_BYTESWAP_H | 18 | #ifndef _GL_BYTESWAP_H |
| 19 | #define _GL_BYTESWAP_H | 19 | #define _GL_BYTESWAP_H 1 |
| 20 | |||
| 21 | /* This file uses _GL_INLINE. */ | ||
| 22 | #if !_GL_CONFIG_H_INCLUDED | ||
| 23 | #error "Please include config.h first." | ||
| 24 | #endif | ||
| 25 | |||
| 26 | /* Define this now, rather than after including stdint.h, in case | ||
| 27 | stdint.h recursively includes us. This is for Gnulib endian.h. */ | ||
| 28 | #ifndef _GL_BYTESWAP_INLINE | ||
| 29 | # define _GL_BYTESWAP_INLINE _GL_INLINE | ||
| 30 | #endif | ||
| 31 | |||
| 32 | #include <stdint.h> | ||
| 33 | |||
| 34 | _GL_INLINE_HEADER_BEGIN | ||
| 35 | |||
| 36 | #ifdef __cplusplus | ||
| 37 | extern "C" { | ||
| 38 | #endif | ||
| 39 | |||
| 40 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) | ||
| 41 | # define _GL_BYTESWAP_HAS_BUILTIN_BSWAP16 true | ||
| 42 | #elif defined __has_builtin | ||
| 43 | # if __has_builtin (__builtin_bswap16) | ||
| 44 | # define _GL_BYTESWAP_HAS_BUILTIN_BSWAP16 true | ||
| 45 | # endif | ||
| 46 | #endif | ||
| 47 | |||
| 48 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) | ||
| 49 | # define _GL_BYTESWAP_HAS_BUILTIN_BSWAP32 true | ||
| 50 | # define _GL_BYTESWAP_HAS_BUILTIN_BSWAP64 true | ||
| 51 | #elif defined __has_builtin | ||
| 52 | # if __has_builtin (__builtin_bswap32) | ||
| 53 | # define _GL_BYTESWAP_HAS_BUILTIN_BSWAP32 true | ||
| 54 | # endif | ||
| 55 | # if __has_builtin (__builtin_bswap64) | ||
| 56 | # define _GL_BYTESWAP_HAS_BUILTIN_BSWAP64 true | ||
| 57 | # endif | ||
| 58 | #endif | ||
| 20 | 59 | ||
| 21 | /* Given an unsigned 16-bit argument X, return the value corresponding to | 60 | /* Given an unsigned 16-bit argument X, return the value corresponding to |
| 22 | X with reversed byte order. */ | 61 | X with reversed byte order. */ |
| 23 | #define bswap_16(x) ((((x) & 0x00FF) << 8) | \ | 62 | _GL_BYTESWAP_INLINE uint_least16_t |
| 24 | (((x) & 0xFF00) >> 8)) | 63 | bswap_16 (uint_least16_t x) |
| 64 | { | ||
| 65 | #ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP16 | ||
| 66 | return __builtin_bswap16 (x); | ||
| 67 | #else | ||
| 68 | uint_fast16_t mask = 0xff; | ||
| 69 | return ( (x & mask << 8 * 1) >> 8 * 1 | ||
| 70 | | (x & mask << 8 * 0) << 8 * 1); | ||
| 71 | #endif | ||
| 72 | } | ||
| 25 | 73 | ||
| 26 | /* Given an unsigned 32-bit argument X, return the value corresponding to | 74 | /* Given an unsigned 32-bit argument X, return the value corresponding to |
| 27 | X with reversed byte order. */ | 75 | X with reversed byte order. */ |
| 28 | #define bswap_32(x) ((((x) & 0x000000FF) << 24) | \ | 76 | _GL_BYTESWAP_INLINE uint_least32_t |
| 29 | (((x) & 0x0000FF00) << 8) | \ | 77 | bswap_32 (uint_least32_t x) |
| 30 | (((x) & 0x00FF0000) >> 8) | \ | 78 | { |
| 31 | (((x) & 0xFF000000) >> 24)) | 79 | #ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP32 |
| 80 | return __builtin_bswap32 (x); | ||
| 81 | #else | ||
| 82 | uint_fast32_t mask = 0xff; | ||
| 83 | return ( (x & mask << 8 * 3) >> 8 * 3 | ||
| 84 | | (x & mask << 8 * 2) >> 8 * 1 | ||
| 85 | | (x & mask << 8 * 1) << 8 * 1 | ||
| 86 | | (x & mask << 8 * 0) << 8 * 3); | ||
| 87 | #endif | ||
| 88 | } | ||
| 32 | 89 | ||
| 90 | #ifdef UINT_LEAST64_MAX | ||
| 33 | /* Given an unsigned 64-bit argument X, return the value corresponding to | 91 | /* Given an unsigned 64-bit argument X, return the value corresponding to |
| 34 | X with reversed byte order. */ | 92 | X with reversed byte order. */ |
| 35 | #define bswap_64(x) ((((x) & 0x00000000000000FFULL) << 56) | \ | 93 | _GL_BYTESWAP_INLINE uint_least64_t |
| 36 | (((x) & 0x000000000000FF00ULL) << 40) | \ | 94 | bswap_64 (uint_least64_t x) |
| 37 | (((x) & 0x0000000000FF0000ULL) << 24) | \ | 95 | { |
| 38 | (((x) & 0x00000000FF000000ULL) << 8) | \ | 96 | # ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP64 |
| 39 | (((x) & 0x000000FF00000000ULL) >> 8) | \ | 97 | return __builtin_bswap64 (x); |
| 40 | (((x) & 0x0000FF0000000000ULL) >> 24) | \ | 98 | # else |
| 41 | (((x) & 0x00FF000000000000ULL) >> 40) | \ | 99 | uint_fast64_t mask = 0xff; |
| 42 | (((x) & 0xFF00000000000000ULL) >> 56)) | 100 | return ( (x & mask << 8 * 7) >> 8 * 7 |
| 101 | | (x & mask << 8 * 6) >> 8 * 5 | ||
| 102 | | (x & mask << 8 * 5) >> 8 * 3 | ||
| 103 | | (x & mask << 8 * 4) >> 8 * 1 | ||
| 104 | | (x & mask << 8 * 3) << 8 * 1 | ||
| 105 | | (x & mask << 8 * 2) << 8 * 3 | ||
| 106 | | (x & mask << 8 * 1) << 8 * 5 | ||
| 107 | | (x & mask << 8 * 0) << 8 * 7); | ||
| 108 | # endif | ||
| 109 | } | ||
| 110 | #endif | ||
| 111 | |||
| 112 | #ifdef __cplusplus | ||
| 113 | } | ||
| 114 | #endif | ||
| 115 | |||
| 116 | _GL_INLINE_HEADER_END | ||
| 43 | 117 | ||
| 44 | #endif /* _GL_BYTESWAP_H */ | 118 | #endif /* _GL_BYTESWAP_H */ |
