Debian Codegraph @

lib/byteswap.in.h

raw
1 /* byteswap.h - Byte swapping
2 Copyright (C) 2005, 2007, 2009-2024 Free Software Foundation, Inc.
3 Written by Oskar Liljeblad <[email protected]>, 2005.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #ifndef _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 #include <stdint.h>
27
28 _GL_INLINE_HEADER_BEGIN
29 #ifndef _GL_BYTESWAP_INLINE
30 # define _GL_BYTESWAP_INLINE _GL_INLINE
31 #endif
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
38 # define _GL_BYTESWAP_HAS_BUILTIN_BSWAP16 true
39 #elif defined __has_builtin
40 # if __has_builtin (__builtin_bswap16)
41 # define _GL_BYTESWAP_HAS_BUILTIN_BSWAP16 true
42 # endif
43 #endif
44
45 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
46 # define _GL_BYTESWAP_HAS_BUILTIN_BSWAP32 true
47 # define _GL_BYTESWAP_HAS_BUILTIN_BSWAP64 true
48 #elif defined __has_builtin
49 # if __has_builtin (__builtin_bswap32)
50 # define _GL_BYTESWAP_HAS_BUILTIN_BSWAP32 true
51 # endif
52 # if __has_builtin (__builtin_bswap64)
53 # define _GL_BYTESWAP_HAS_BUILTIN_BSWAP64 true
54 # endif
55 #endif
56
57 /* Given an unsigned 16-bit argument X, return the value corresponding to
58 X with reversed byte order. */
59 _GL_BYTESWAP_INLINE uint_least16_t
60 bswap_16 (uint_least16_t x)
61 {
62 #ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP16
63 return __builtin_bswap16 (x);
64 #else
65 uint_fast16_t mask = 0xff;
66 return ( (x & mask << 8 * 1) >> 8 * 1
67 | (x & mask << 8 * 0) << 8 * 1);
68 #endif
69 }
70
71 /* Given an unsigned 32-bit argument X, return the value corresponding to
72 X with reversed byte order. */
73 _GL_BYTESWAP_INLINE uint_least32_t
74 bswap_32 (uint_least32_t x)
75 {
76 #ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP32
77 return __builtin_bswap32 (x);
78 #else
79 uint_fast32_t mask = 0xff;
80 return ( (x & mask << 8 * 3) >> 8 * 3
81 | (x & mask << 8 * 2) >> 8 * 1
82 | (x & mask << 8 * 1) << 8 * 1
83 | (x & mask << 8 * 0) << 8 * 3);
84 #endif
85 }
86
87 #ifdef UINT_LEAST64_MAX
88 /* Given an unsigned 64-bit argument X, return the value corresponding to
89 X with reversed byte order. */
90 _GL_BYTESWAP_INLINE uint_least64_t
91 bswap_64 (uint_least64_t x)
92 {
93 # ifdef _GL_BYTESWAP_HAS_BUILTIN_BSWAP64
94 return __builtin_bswap64 (x);
95 # else
96 uint_fast64_t mask = 0xff;
97 return ( (x & mask << 8 * 7) >> 8 * 7
98 | (x & mask << 8 * 6) >> 8 * 5
99 | (x & mask << 8 * 5) >> 8 * 3
100 | (x & mask << 8 * 4) >> 8 * 1
101 | (x & mask << 8 * 3) << 8 * 1
102 | (x & mask << 8 * 2) << 8 * 3
103 | (x & mask << 8 * 1) << 8 * 5
104 | (x & mask << 8 * 0) << 8 * 7);
105 # endif
106 }
107 #endif
108
109 #ifdef __cplusplus
110 }
111 #endif
112
113 _GL_INLINE_HEADER_END
114
115 #endif /* _GL_BYTESWAP_H */