Skip to content

Commit 432b18c

Browse files
committed
serialization: detect byteswap builtins without autoconf tests
Rather than a complicated set of tests to decide which bswap functions to use, always prefer the compiler built-ins when available. These builtins and fallbacks can all be removed once we're using c++23, which adds std::byteswap.
1 parent 297367b commit 432b18c

File tree

4 files changed

+59
-44
lines changed

4 files changed

+59
-44
lines changed

configure.ac

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ if test "$TARGET_OS" = "darwin"; then
975975
AX_CHECK_LINK_FLAG([-Wl,-fixup_chains], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-fixup_chains"], [], [$LDFLAG_WERROR])
976976
fi
977977

978-
AC_CHECK_HEADERS([endian.h sys/endian.h byteswap.h sys/select.h sys/prctl.h sys/sysctl.h vm/vm_param.h sys/vmmeter.h sys/resources.h])
978+
AC_CHECK_HEADERS([endian.h sys/endian.h sys/select.h sys/prctl.h sys/sysctl.h vm/vm_param.h sys/vmmeter.h sys/resources.h])
979979

980980
AC_CHECK_DECLS([getifaddrs, freeifaddrs],[CHECK_SOCKET],,
981981
[#include <sys/types.h>
@@ -997,11 +997,6 @@ AC_CHECK_DECLS([le16toh, le32toh, le64toh, htole16, htole32, htole64, be16toh, b
997997
#include <sys/endian.h>
998998
#endif])
999999

1000-
AC_CHECK_DECLS([bswap_16, bswap_32, bswap_64],,,
1001-
[#if HAVE_BYTESWAP_H
1002-
#include <byteswap.h>
1003-
#endif])
1004-
10051000
AC_MSG_CHECKING([for __builtin_clzl])
10061001
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]], [[
10071002
(void) __builtin_clzl(0);

src/compat/byteswap.h

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,66 @@
55
#ifndef BITCOIN_COMPAT_BYTESWAP_H
66
#define BITCOIN_COMPAT_BYTESWAP_H
77

8-
#if defined(HAVE_CONFIG_H)
9-
#include <config/bitcoin-config.h>
8+
#include <cstdint>
9+
#ifdef _MSC_VER
10+
#include <cstdlib>
1011
#endif
1112

12-
#include <cstdint>
1313

14-
#if defined(HAVE_BYTESWAP_H)
15-
#include <byteswap.h>
16-
#endif
14+
// All internal_bswap_* functions can be replaced with std::byteswap once we
15+
// require c++23. Both libstdc++ and libc++ implement std::byteswap via these
16+
// builtins.
1717

18-
#if defined(MAC_OSX)
18+
#ifndef DISABLE_BUILTIN_BSWAPS
19+
# if defined __has_builtin
20+
# if __has_builtin(__builtin_bswap16)
21+
# define bitcoin_builtin_bswap16(x) __builtin_bswap16(x)
22+
# endif
23+
# if __has_builtin(__builtin_bswap32)
24+
# define bitcoin_builtin_bswap32(x) __builtin_bswap32(x)
25+
# endif
26+
# if __has_builtin(__builtin_bswap64)
27+
# define bitcoin_builtin_bswap64(x) __builtin_bswap64(x)
28+
# endif
29+
# elif defined(_MSC_VER)
30+
# define bitcoin_builtin_bswap16(x) _byteswap_ushort(x)
31+
# define bitcoin_builtin_bswap32(x) _byteswap_ulong(x)
32+
# define bitcoin_builtin_bswap64(x) _byteswap_uint64(x)
33+
# endif
34+
#endif
1935

20-
#include <libkern/OSByteOrder.h>
21-
#define bswap_16(x) OSSwapInt16(x)
22-
#define bswap_32(x) OSSwapInt32(x)
23-
#define bswap_64(x) OSSwapInt64(x)
36+
// MSVC's _byteswap_* functions are not constexpr
2437

38+
#ifndef _MSC_VER
39+
#define BSWAP_CONSTEXPR constexpr
2540
#else
26-
// Non-MacOS / non-Darwin
41+
#define BSWAP_CONSTEXPR
42+
#endif
2743

28-
#if HAVE_DECL_BSWAP_16 == 0
29-
inline uint16_t bswap_16(uint16_t x)
44+
inline BSWAP_CONSTEXPR uint16_t internal_bswap_16(uint16_t x)
3045
{
46+
#ifdef bitcoin_builtin_bswap16
47+
return bitcoin_builtin_bswap16(x);
48+
#else
3149
return (x >> 8) | (x << 8);
50+
#endif
3251
}
33-
#endif // HAVE_DECL_BSWAP16 == 0
3452

35-
#if HAVE_DECL_BSWAP_32 == 0
36-
inline uint32_t bswap_32(uint32_t x)
53+
inline BSWAP_CONSTEXPR uint32_t internal_bswap_32(uint32_t x)
3754
{
55+
#ifdef bitcoin_builtin_bswap32
56+
return bitcoin_builtin_bswap32(x);
57+
#else
3858
return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
3959
((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
60+
#endif
4061
}
41-
#endif // HAVE_DECL_BSWAP32 == 0
4262

43-
#if HAVE_DECL_BSWAP_64 == 0
44-
inline uint64_t bswap_64(uint64_t x)
63+
inline BSWAP_CONSTEXPR uint64_t internal_bswap_64(uint64_t x)
4564
{
65+
#ifdef bitcoin_builtin_bswap64
66+
return bitcoin_builtin_bswap64(x);
67+
#else
4668
return (((x & 0xff00000000000000ull) >> 56)
4769
| ((x & 0x00ff000000000000ull) >> 40)
4870
| ((x & 0x0000ff0000000000ull) >> 24)
@@ -51,9 +73,7 @@ inline uint64_t bswap_64(uint64_t x)
5173
| ((x & 0x0000000000ff0000ull) << 24)
5274
| ((x & 0x000000000000ff00ull) << 40)
5375
| ((x & 0x00000000000000ffull) << 56));
76+
#endif
5477
}
55-
#endif // HAVE_DECL_BSWAP64 == 0
56-
57-
#endif // defined(MAC_OSX)
5878

5979
#endif // BITCOIN_COMPAT_BYTESWAP_H

src/compat/endian.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ inline uint16_t htobe16(uint16_t host_16bits)
7676
#if HAVE_DECL_HTOLE16 == 0
7777
inline uint16_t htole16(uint16_t host_16bits)
7878
{
79-
return bswap_16(host_16bits);
79+
return internal_bswap_16(host_16bits);
8080
}
8181
#endif // HAVE_DECL_HTOLE16
8282

@@ -90,7 +90,7 @@ inline uint16_t be16toh(uint16_t big_endian_16bits)
9090
#if HAVE_DECL_LE16TOH == 0
9191
inline uint16_t le16toh(uint16_t little_endian_16bits)
9292
{
93-
return bswap_16(little_endian_16bits);
93+
return internal_bswap_16(little_endian_16bits);
9494
}
9595
#endif // HAVE_DECL_LE16TOH
9696

@@ -104,7 +104,7 @@ inline uint32_t htobe32(uint32_t host_32bits)
104104
#if HAVE_DECL_HTOLE32 == 0
105105
inline uint32_t htole32(uint32_t host_32bits)
106106
{
107-
return bswap_32(host_32bits);
107+
return internal_bswap_32(host_32bits);
108108
}
109109
#endif // HAVE_DECL_HTOLE32
110110

@@ -118,7 +118,7 @@ inline uint32_t be32toh(uint32_t big_endian_32bits)
118118
#if HAVE_DECL_LE32TOH == 0
119119
inline uint32_t le32toh(uint32_t little_endian_32bits)
120120
{
121-
return bswap_32(little_endian_32bits);
121+
return internal_bswap_32(little_endian_32bits);
122122
}
123123
#endif // HAVE_DECL_LE32TOH
124124

@@ -132,7 +132,7 @@ inline uint64_t htobe64(uint64_t host_64bits)
132132
#if HAVE_DECL_HTOLE64 == 0
133133
inline uint64_t htole64(uint64_t host_64bits)
134134
{
135-
return bswap_64(host_64bits);
135+
return internal_bswap_64(host_64bits);
136136
}
137137
#endif // HAVE_DECL_HTOLE64
138138

@@ -146,7 +146,7 @@ inline uint64_t be64toh(uint64_t big_endian_64bits)
146146
#if HAVE_DECL_LE64TOH == 0
147147
inline uint64_t le64toh(uint64_t little_endian_64bits)
148148
{
149-
return bswap_64(little_endian_64bits);
149+
return internal_bswap_64(little_endian_64bits);
150150
}
151151
#endif // HAVE_DECL_LE64TOH
152152

@@ -155,7 +155,7 @@ inline uint64_t le64toh(uint64_t little_endian_64bits)
155155
#if HAVE_DECL_HTOBE16 == 0
156156
inline uint16_t htobe16(uint16_t host_16bits)
157157
{
158-
return bswap_16(host_16bits);
158+
return internal_bswap_16(host_16bits);
159159
}
160160
#endif // HAVE_DECL_HTOBE16
161161

@@ -169,7 +169,7 @@ inline uint16_t htole16(uint16_t host_16bits)
169169
#if HAVE_DECL_BE16TOH == 0
170170
inline uint16_t be16toh(uint16_t big_endian_16bits)
171171
{
172-
return bswap_16(big_endian_16bits);
172+
return internal_bswap_16(big_endian_16bits);
173173
}
174174
#endif // HAVE_DECL_BE16TOH
175175

@@ -183,7 +183,7 @@ inline uint16_t le16toh(uint16_t little_endian_16bits)
183183
#if HAVE_DECL_HTOBE32 == 0
184184
inline uint32_t htobe32(uint32_t host_32bits)
185185
{
186-
return bswap_32(host_32bits);
186+
return internal_bswap_32(host_32bits);
187187
}
188188
#endif // HAVE_DECL_HTOBE32
189189

@@ -197,7 +197,7 @@ inline uint32_t htole32(uint32_t host_32bits)
197197
#if HAVE_DECL_BE32TOH == 0
198198
inline uint32_t be32toh(uint32_t big_endian_32bits)
199199
{
200-
return bswap_32(big_endian_32bits);
200+
return internal_bswap_32(big_endian_32bits);
201201
}
202202
#endif // HAVE_DECL_BE32TOH
203203

@@ -211,7 +211,7 @@ inline uint32_t le32toh(uint32_t little_endian_32bits)
211211
#if HAVE_DECL_HTOBE64 == 0
212212
inline uint64_t htobe64(uint64_t host_64bits)
213213
{
214-
return bswap_64(host_64bits);
214+
return internal_bswap_64(host_64bits);
215215
}
216216
#endif // HAVE_DECL_HTOBE64
217217

@@ -225,7 +225,7 @@ inline uint64_t htole64(uint64_t host_64bits)
225225
#if HAVE_DECL_BE64TOH == 0
226226
inline uint64_t be64toh(uint64_t big_endian_64bits)
227227
{
228-
return bswap_64(big_endian_64bits);
228+
return internal_bswap_64(big_endian_64bits);
229229
}
230230
#endif // HAVE_DECL_BE64TOH
231231

src/test/bswap_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ BOOST_AUTO_TEST_CASE(bswap_tests)
1616
uint16_t e1 = 0x3412;
1717
uint32_t e2 = 0xbc9a7856;
1818
uint64_t e3 = 0xbc9a78563412f0de;
19-
BOOST_CHECK(bswap_16(u1) == e1);
20-
BOOST_CHECK(bswap_32(u2) == e2);
21-
BOOST_CHECK(bswap_64(u3) == e3);
19+
BOOST_CHECK(internal_bswap_16(u1) == e1);
20+
BOOST_CHECK(internal_bswap_32(u2) == e2);
21+
BOOST_CHECK(internal_bswap_64(u3) == e3);
2222
}
2323

2424
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)