|
5 | 5 | #ifndef BITCOIN_COMPAT_ENDIAN_H |
6 | 6 | #define BITCOIN_COMPAT_ENDIAN_H |
7 | 7 |
|
8 | | -#if defined(HAVE_CONFIG_H) |
9 | | -#include <config/bitcoin-config.h> |
10 | | -#endif |
11 | | - |
12 | 8 | #include <compat/byteswap.h> |
13 | 9 |
|
| 10 | +#include <bit> |
14 | 11 | #include <cstdint> |
15 | 12 |
|
16 | | -#if defined(HAVE_ENDIAN_H) |
17 | | -#include <endian.h> |
18 | | -#elif defined(HAVE_SYS_ENDIAN_H) |
19 | | -#include <sys/endian.h> |
20 | | -#endif |
21 | | - |
22 | | -#ifndef HAVE_CONFIG_H |
23 | | -// While not technically a supported configuration, defaulting to defining these |
24 | | -// DECLs when we were compiled without autotools makes it easier for other build |
25 | | -// systems to build things like libbitcoinconsensus for strange targets. |
26 | | -#ifdef htobe16 |
27 | | -#define HAVE_DECL_HTOBE16 1 |
28 | | -#endif |
29 | | -#ifdef htole16 |
30 | | -#define HAVE_DECL_HTOLE16 1 |
31 | | -#endif |
32 | | -#ifdef be16toh |
33 | | -#define HAVE_DECL_BE16TOH 1 |
34 | | -#endif |
35 | | -#ifdef le16toh |
36 | | -#define HAVE_DECL_LE16TOH 1 |
37 | | -#endif |
38 | | - |
39 | | -#ifdef htobe32 |
40 | | -#define HAVE_DECL_HTOBE32 1 |
41 | | -#endif |
42 | | -#ifdef htole32 |
43 | | -#define HAVE_DECL_HTOLE32 1 |
44 | | -#endif |
45 | | -#ifdef be32toh |
46 | | -#define HAVE_DECL_BE32TOH 1 |
47 | | -#endif |
48 | | -#ifdef le32toh |
49 | | -#define HAVE_DECL_LE32TOH 1 |
50 | | -#endif |
51 | | - |
52 | | -#ifdef htobe64 |
53 | | -#define HAVE_DECL_HTOBE64 1 |
54 | | -#endif |
55 | | -#ifdef htole64 |
56 | | -#define HAVE_DECL_HTOLE64 1 |
57 | | -#endif |
58 | | -#ifdef be64toh |
59 | | -#define HAVE_DECL_BE64TOH 1 |
60 | | -#endif |
61 | | -#ifdef le64toh |
62 | | -#define HAVE_DECL_LE64TOH 1 |
63 | | -#endif |
64 | | - |
65 | | -#endif // HAVE_CONFIG_H |
66 | | - |
67 | | -#if defined(WORDS_BIGENDIAN) |
68 | | - |
69 | | -#if HAVE_DECL_HTOBE16 == 0 |
70 | | -inline uint16_t htobe16(uint16_t host_16bits) |
71 | | -{ |
72 | | - return host_16bits; |
73 | | -} |
74 | | -#endif // HAVE_DECL_HTOBE16 |
75 | | - |
76 | | -#if HAVE_DECL_HTOLE16 == 0 |
77 | | -inline uint16_t htole16(uint16_t host_16bits) |
78 | | -{ |
79 | | - return internal_bswap_16(host_16bits); |
80 | | -} |
81 | | -#endif // HAVE_DECL_HTOLE16 |
82 | | - |
83 | | -#if HAVE_DECL_BE16TOH == 0 |
84 | | -inline uint16_t be16toh(uint16_t big_endian_16bits) |
85 | | -{ |
86 | | - return big_endian_16bits; |
87 | | -} |
88 | | -#endif // HAVE_DECL_BE16TOH |
89 | | - |
90 | | -#if HAVE_DECL_LE16TOH == 0 |
91 | | -inline uint16_t le16toh(uint16_t little_endian_16bits) |
92 | | -{ |
93 | | - return internal_bswap_16(little_endian_16bits); |
94 | | -} |
95 | | -#endif // HAVE_DECL_LE16TOH |
96 | | - |
97 | | -#if HAVE_DECL_HTOBE32 == 0 |
98 | | -inline uint32_t htobe32(uint32_t host_32bits) |
99 | | -{ |
100 | | - return host_32bits; |
101 | | -} |
102 | | -#endif // HAVE_DECL_HTOBE32 |
103 | | - |
104 | | -#if HAVE_DECL_HTOLE32 == 0 |
105 | | -inline uint32_t htole32(uint32_t host_32bits) |
106 | | -{ |
107 | | - return internal_bswap_32(host_32bits); |
108 | | -} |
109 | | -#endif // HAVE_DECL_HTOLE32 |
110 | | - |
111 | | -#if HAVE_DECL_BE32TOH == 0 |
112 | | -inline uint32_t be32toh(uint32_t big_endian_32bits) |
113 | | -{ |
114 | | - return big_endian_32bits; |
115 | | -} |
116 | | -#endif // HAVE_DECL_BE32TOH |
117 | | - |
118 | | -#if HAVE_DECL_LE32TOH == 0 |
119 | | -inline uint32_t le32toh(uint32_t little_endian_32bits) |
| 13 | +inline BSWAP_CONSTEXPR uint16_t htobe16_internal(uint16_t host_16bits) |
120 | 14 | { |
121 | | - return internal_bswap_32(little_endian_32bits); |
| 15 | + if constexpr (std::endian::native == std::endian::little) return internal_bswap_16(host_16bits); |
| 16 | + else return host_16bits; |
122 | 17 | } |
123 | | -#endif // HAVE_DECL_LE32TOH |
124 | | - |
125 | | -#if HAVE_DECL_HTOBE64 == 0 |
126 | | -inline uint64_t htobe64(uint64_t host_64bits) |
127 | | -{ |
128 | | - return host_64bits; |
129 | | -} |
130 | | -#endif // HAVE_DECL_HTOBE64 |
131 | | - |
132 | | -#if HAVE_DECL_HTOLE64 == 0 |
133 | | -inline uint64_t htole64(uint64_t host_64bits) |
134 | | -{ |
135 | | - return internal_bswap_64(host_64bits); |
136 | | -} |
137 | | -#endif // HAVE_DECL_HTOLE64 |
138 | | - |
139 | | -#if HAVE_DECL_BE64TOH == 0 |
140 | | -inline uint64_t be64toh(uint64_t big_endian_64bits) |
141 | | -{ |
142 | | - return big_endian_64bits; |
143 | | -} |
144 | | -#endif // HAVE_DECL_BE64TOH |
145 | | - |
146 | | -#if HAVE_DECL_LE64TOH == 0 |
147 | | -inline uint64_t le64toh(uint64_t little_endian_64bits) |
148 | | -{ |
149 | | - return internal_bswap_64(little_endian_64bits); |
150 | | -} |
151 | | -#endif // HAVE_DECL_LE64TOH |
152 | | - |
153 | | -#else // WORDS_BIGENDIAN |
154 | | - |
155 | | -#if HAVE_DECL_HTOBE16 == 0 |
156 | | -inline uint16_t htobe16(uint16_t host_16bits) |
| 18 | +inline BSWAP_CONSTEXPR uint16_t htole16_internal(uint16_t host_16bits) |
157 | 19 | { |
158 | | - return internal_bswap_16(host_16bits); |
| 20 | + if constexpr (std::endian::native == std::endian::big) return internal_bswap_16(host_16bits); |
| 21 | + else return host_16bits; |
159 | 22 | } |
160 | | -#endif // HAVE_DECL_HTOBE16 |
161 | | - |
162 | | -#if HAVE_DECL_HTOLE16 == 0 |
163 | | -inline uint16_t htole16(uint16_t host_16bits) |
| 23 | +inline BSWAP_CONSTEXPR uint16_t be16toh_internal(uint16_t big_endian_16bits) |
164 | 24 | { |
165 | | - return host_16bits; |
| 25 | + if constexpr (std::endian::native == std::endian::little) return internal_bswap_16(big_endian_16bits); |
| 26 | + else return big_endian_16bits; |
166 | 27 | } |
167 | | -#endif // HAVE_DECL_HTOLE16 |
168 | | - |
169 | | -#if HAVE_DECL_BE16TOH == 0 |
170 | | -inline uint16_t be16toh(uint16_t big_endian_16bits) |
| 28 | +inline BSWAP_CONSTEXPR uint16_t le16toh_internal(uint16_t little_endian_16bits) |
171 | 29 | { |
172 | | - return internal_bswap_16(big_endian_16bits); |
| 30 | + if constexpr (std::endian::native == std::endian::big) return internal_bswap_16(little_endian_16bits); |
| 31 | + else return little_endian_16bits; |
173 | 32 | } |
174 | | -#endif // HAVE_DECL_BE16TOH |
175 | | - |
176 | | -#if HAVE_DECL_LE16TOH == 0 |
177 | | -inline uint16_t le16toh(uint16_t little_endian_16bits) |
| 33 | +inline BSWAP_CONSTEXPR uint32_t htobe32_internal(uint32_t host_32bits) |
178 | 34 | { |
179 | | - return little_endian_16bits; |
| 35 | + if constexpr (std::endian::native == std::endian::little) return internal_bswap_32(host_32bits); |
| 36 | + else return host_32bits; |
180 | 37 | } |
181 | | -#endif // HAVE_DECL_LE16TOH |
182 | | - |
183 | | -#if HAVE_DECL_HTOBE32 == 0 |
184 | | -inline uint32_t htobe32(uint32_t host_32bits) |
185 | | -{ |
186 | | - return internal_bswap_32(host_32bits); |
187 | | -} |
188 | | -#endif // HAVE_DECL_HTOBE32 |
189 | | - |
190 | | -#if HAVE_DECL_HTOLE32 == 0 |
191 | | -inline uint32_t htole32(uint32_t host_32bits) |
| 38 | +inline BSWAP_CONSTEXPR uint32_t htole32_internal(uint32_t host_32bits) |
192 | 39 | { |
193 | | - return host_32bits; |
| 40 | + if constexpr (std::endian::native == std::endian::big) return internal_bswap_32(host_32bits); |
| 41 | + else return host_32bits; |
194 | 42 | } |
195 | | -#endif // HAVE_DECL_HTOLE32 |
196 | | - |
197 | | -#if HAVE_DECL_BE32TOH == 0 |
198 | | -inline uint32_t be32toh(uint32_t big_endian_32bits) |
| 43 | +inline BSWAP_CONSTEXPR uint32_t be32toh_internal(uint32_t big_endian_32bits) |
199 | 44 | { |
200 | | - return internal_bswap_32(big_endian_32bits); |
| 45 | + if constexpr (std::endian::native == std::endian::little) return internal_bswap_32(big_endian_32bits); |
| 46 | + else return big_endian_32bits; |
201 | 47 | } |
202 | | -#endif // HAVE_DECL_BE32TOH |
203 | | - |
204 | | -#if HAVE_DECL_LE32TOH == 0 |
205 | | -inline uint32_t le32toh(uint32_t little_endian_32bits) |
| 48 | +inline BSWAP_CONSTEXPR uint32_t le32toh_internal(uint32_t little_endian_32bits) |
206 | 49 | { |
207 | | - return little_endian_32bits; |
| 50 | + if constexpr (std::endian::native == std::endian::big) return internal_bswap_32(little_endian_32bits); |
| 51 | + else return little_endian_32bits; |
208 | 52 | } |
209 | | -#endif // HAVE_DECL_LE32TOH |
210 | | - |
211 | | -#if HAVE_DECL_HTOBE64 == 0 |
212 | | -inline uint64_t htobe64(uint64_t host_64bits) |
| 53 | +inline BSWAP_CONSTEXPR uint64_t htobe64_internal(uint64_t host_64bits) |
213 | 54 | { |
214 | | - return internal_bswap_64(host_64bits); |
| 55 | + if constexpr (std::endian::native == std::endian::little) return internal_bswap_64(host_64bits); |
| 56 | + else return host_64bits; |
215 | 57 | } |
216 | | -#endif // HAVE_DECL_HTOBE64 |
217 | | - |
218 | | -#if HAVE_DECL_HTOLE64 == 0 |
219 | | -inline uint64_t htole64(uint64_t host_64bits) |
| 58 | +inline BSWAP_CONSTEXPR uint64_t htole64_internal(uint64_t host_64bits) |
220 | 59 | { |
221 | | - return host_64bits; |
| 60 | + if constexpr (std::endian::native == std::endian::big) return internal_bswap_64(host_64bits); |
| 61 | + else return host_64bits; |
222 | 62 | } |
223 | | -#endif // HAVE_DECL_HTOLE64 |
224 | | - |
225 | | -#if HAVE_DECL_BE64TOH == 0 |
226 | | -inline uint64_t be64toh(uint64_t big_endian_64bits) |
| 63 | +inline BSWAP_CONSTEXPR uint64_t be64toh_internal(uint64_t big_endian_64bits) |
227 | 64 | { |
228 | | - return internal_bswap_64(big_endian_64bits); |
| 65 | + if constexpr (std::endian::native == std::endian::little) return internal_bswap_64(big_endian_64bits); |
| 66 | + else return big_endian_64bits; |
229 | 67 | } |
230 | | -#endif // HAVE_DECL_BE64TOH |
231 | | - |
232 | | -#if HAVE_DECL_LE64TOH == 0 |
233 | | -inline uint64_t le64toh(uint64_t little_endian_64bits) |
| 68 | +inline BSWAP_CONSTEXPR uint64_t le64toh_internal(uint64_t little_endian_64bits) |
234 | 69 | { |
235 | | - return little_endian_64bits; |
| 70 | + if constexpr (std::endian::native == std::endian::big) return internal_bswap_64(little_endian_64bits); |
| 71 | + else return little_endian_64bits; |
236 | 72 | } |
237 | | -#endif // HAVE_DECL_LE64TOH |
238 | | - |
239 | | -#endif // WORDS_BIGENDIAN |
240 | 73 |
|
241 | 74 | #endif // BITCOIN_COMPAT_ENDIAN_H |
0 commit comments