mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-10 03:47:29 -03:00
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.
This commit is contained in:
parent
297367b3bb
commit
432b18ca8d
4 changed files with 60 additions and 45 deletions
|
@ -975,7 +975,7 @@ if test "$TARGET_OS" = "darwin"; then
|
|||
AX_CHECK_LINK_FLAG([-Wl,-fixup_chains], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-fixup_chains"], [], [$LDFLAG_WERROR])
|
||||
fi
|
||||
|
||||
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])
|
||||
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])
|
||||
|
||||
AC_CHECK_DECLS([getifaddrs, freeifaddrs],[CHECK_SOCKET],,
|
||||
[#include <sys/types.h>
|
||||
|
@ -997,11 +997,6 @@ AC_CHECK_DECLS([le16toh, le32toh, le64toh, htole16, htole32, htole64, be16toh, b
|
|||
#include <sys/endian.h>
|
||||
#endif])
|
||||
|
||||
AC_CHECK_DECLS([bswap_16, bswap_32, bswap_64],,,
|
||||
[#if HAVE_BYTESWAP_H
|
||||
#include <byteswap.h>
|
||||
#endif])
|
||||
|
||||
AC_MSG_CHECKING([for __builtin_clzl])
|
||||
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]], [[
|
||||
(void) __builtin_clzl(0);
|
||||
|
|
|
@ -5,44 +5,66 @@
|
|||
#ifndef BITCOIN_COMPAT_BYTESWAP_H
|
||||
#define BITCOIN_COMPAT_BYTESWAP_H
|
||||
|
||||
#if defined(HAVE_CONFIG_H)
|
||||
#include <config/bitcoin-config.h>
|
||||
#endif
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#if defined(HAVE_BYTESWAP_H)
|
||||
#include <byteswap.h>
|
||||
#ifdef _MSC_VER
|
||||
#include <cstdlib>
|
||||
#endif
|
||||
|
||||
#if defined(MAC_OSX)
|
||||
|
||||
#include <libkern/OSByteOrder.h>
|
||||
#define bswap_16(x) OSSwapInt16(x)
|
||||
#define bswap_32(x) OSSwapInt32(x)
|
||||
#define bswap_64(x) OSSwapInt64(x)
|
||||
// All internal_bswap_* functions can be replaced with std::byteswap once we
|
||||
// require c++23. Both libstdc++ and libc++ implement std::byteswap via these
|
||||
// builtins.
|
||||
|
||||
#ifndef DISABLE_BUILTIN_BSWAPS
|
||||
# if defined __has_builtin
|
||||
# if __has_builtin(__builtin_bswap16)
|
||||
# define bitcoin_builtin_bswap16(x) __builtin_bswap16(x)
|
||||
# endif
|
||||
# if __has_builtin(__builtin_bswap32)
|
||||
# define bitcoin_builtin_bswap32(x) __builtin_bswap32(x)
|
||||
# endif
|
||||
# if __has_builtin(__builtin_bswap64)
|
||||
# define bitcoin_builtin_bswap64(x) __builtin_bswap64(x)
|
||||
# endif
|
||||
# elif defined(_MSC_VER)
|
||||
# define bitcoin_builtin_bswap16(x) _byteswap_ushort(x)
|
||||
# define bitcoin_builtin_bswap32(x) _byteswap_ulong(x)
|
||||
# define bitcoin_builtin_bswap64(x) _byteswap_uint64(x)
|
||||
# endif
|
||||
#endif
|
||||
|
||||
// MSVC's _byteswap_* functions are not constexpr
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#define BSWAP_CONSTEXPR constexpr
|
||||
#else
|
||||
// Non-MacOS / non-Darwin
|
||||
#define BSWAP_CONSTEXPR
|
||||
#endif
|
||||
|
||||
#if HAVE_DECL_BSWAP_16 == 0
|
||||
inline uint16_t bswap_16(uint16_t x)
|
||||
inline BSWAP_CONSTEXPR uint16_t internal_bswap_16(uint16_t x)
|
||||
{
|
||||
#ifdef bitcoin_builtin_bswap16
|
||||
return bitcoin_builtin_bswap16(x);
|
||||
#else
|
||||
return (x >> 8) | (x << 8);
|
||||
#endif
|
||||
}
|
||||
#endif // HAVE_DECL_BSWAP16 == 0
|
||||
|
||||
#if HAVE_DECL_BSWAP_32 == 0
|
||||
inline uint32_t bswap_32(uint32_t x)
|
||||
inline BSWAP_CONSTEXPR uint32_t internal_bswap_32(uint32_t x)
|
||||
{
|
||||
#ifdef bitcoin_builtin_bswap32
|
||||
return bitcoin_builtin_bswap32(x);
|
||||
#else
|
||||
return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
|
||||
((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
|
||||
#endif
|
||||
}
|
||||
#endif // HAVE_DECL_BSWAP32 == 0
|
||||
|
||||
#if HAVE_DECL_BSWAP_64 == 0
|
||||
inline uint64_t bswap_64(uint64_t x)
|
||||
inline BSWAP_CONSTEXPR uint64_t internal_bswap_64(uint64_t x)
|
||||
{
|
||||
#ifdef bitcoin_builtin_bswap64
|
||||
return bitcoin_builtin_bswap64(x);
|
||||
#else
|
||||
return (((x & 0xff00000000000000ull) >> 56)
|
||||
| ((x & 0x00ff000000000000ull) >> 40)
|
||||
| ((x & 0x0000ff0000000000ull) >> 24)
|
||||
|
@ -51,9 +73,7 @@ inline uint64_t bswap_64(uint64_t x)
|
|||
| ((x & 0x0000000000ff0000ull) << 24)
|
||||
| ((x & 0x000000000000ff00ull) << 40)
|
||||
| ((x & 0x00000000000000ffull) << 56));
|
||||
#endif
|
||||
}
|
||||
#endif // HAVE_DECL_BSWAP64 == 0
|
||||
|
||||
#endif // defined(MAC_OSX)
|
||||
|
||||
#endif // BITCOIN_COMPAT_BYTESWAP_H
|
||||
|
|
|
@ -76,7 +76,7 @@ inline uint16_t htobe16(uint16_t host_16bits)
|
|||
#if HAVE_DECL_HTOLE16 == 0
|
||||
inline uint16_t htole16(uint16_t host_16bits)
|
||||
{
|
||||
return bswap_16(host_16bits);
|
||||
return internal_bswap_16(host_16bits);
|
||||
}
|
||||
#endif // HAVE_DECL_HTOLE16
|
||||
|
||||
|
@ -90,7 +90,7 @@ inline uint16_t be16toh(uint16_t big_endian_16bits)
|
|||
#if HAVE_DECL_LE16TOH == 0
|
||||
inline uint16_t le16toh(uint16_t little_endian_16bits)
|
||||
{
|
||||
return bswap_16(little_endian_16bits);
|
||||
return internal_bswap_16(little_endian_16bits);
|
||||
}
|
||||
#endif // HAVE_DECL_LE16TOH
|
||||
|
||||
|
@ -104,7 +104,7 @@ inline uint32_t htobe32(uint32_t host_32bits)
|
|||
#if HAVE_DECL_HTOLE32 == 0
|
||||
inline uint32_t htole32(uint32_t host_32bits)
|
||||
{
|
||||
return bswap_32(host_32bits);
|
||||
return internal_bswap_32(host_32bits);
|
||||
}
|
||||
#endif // HAVE_DECL_HTOLE32
|
||||
|
||||
|
@ -118,7 +118,7 @@ inline uint32_t be32toh(uint32_t big_endian_32bits)
|
|||
#if HAVE_DECL_LE32TOH == 0
|
||||
inline uint32_t le32toh(uint32_t little_endian_32bits)
|
||||
{
|
||||
return bswap_32(little_endian_32bits);
|
||||
return internal_bswap_32(little_endian_32bits);
|
||||
}
|
||||
#endif // HAVE_DECL_LE32TOH
|
||||
|
||||
|
@ -132,7 +132,7 @@ inline uint64_t htobe64(uint64_t host_64bits)
|
|||
#if HAVE_DECL_HTOLE64 == 0
|
||||
inline uint64_t htole64(uint64_t host_64bits)
|
||||
{
|
||||
return bswap_64(host_64bits);
|
||||
return internal_bswap_64(host_64bits);
|
||||
}
|
||||
#endif // HAVE_DECL_HTOLE64
|
||||
|
||||
|
@ -146,7 +146,7 @@ inline uint64_t be64toh(uint64_t big_endian_64bits)
|
|||
#if HAVE_DECL_LE64TOH == 0
|
||||
inline uint64_t le64toh(uint64_t little_endian_64bits)
|
||||
{
|
||||
return bswap_64(little_endian_64bits);
|
||||
return internal_bswap_64(little_endian_64bits);
|
||||
}
|
||||
#endif // HAVE_DECL_LE64TOH
|
||||
|
||||
|
@ -155,7 +155,7 @@ inline uint64_t le64toh(uint64_t little_endian_64bits)
|
|||
#if HAVE_DECL_HTOBE16 == 0
|
||||
inline uint16_t htobe16(uint16_t host_16bits)
|
||||
{
|
||||
return bswap_16(host_16bits);
|
||||
return internal_bswap_16(host_16bits);
|
||||
}
|
||||
#endif // HAVE_DECL_HTOBE16
|
||||
|
||||
|
@ -169,7 +169,7 @@ inline uint16_t htole16(uint16_t host_16bits)
|
|||
#if HAVE_DECL_BE16TOH == 0
|
||||
inline uint16_t be16toh(uint16_t big_endian_16bits)
|
||||
{
|
||||
return bswap_16(big_endian_16bits);
|
||||
return internal_bswap_16(big_endian_16bits);
|
||||
}
|
||||
#endif // HAVE_DECL_BE16TOH
|
||||
|
||||
|
@ -183,7 +183,7 @@ inline uint16_t le16toh(uint16_t little_endian_16bits)
|
|||
#if HAVE_DECL_HTOBE32 == 0
|
||||
inline uint32_t htobe32(uint32_t host_32bits)
|
||||
{
|
||||
return bswap_32(host_32bits);
|
||||
return internal_bswap_32(host_32bits);
|
||||
}
|
||||
#endif // HAVE_DECL_HTOBE32
|
||||
|
||||
|
@ -197,7 +197,7 @@ inline uint32_t htole32(uint32_t host_32bits)
|
|||
#if HAVE_DECL_BE32TOH == 0
|
||||
inline uint32_t be32toh(uint32_t big_endian_32bits)
|
||||
{
|
||||
return bswap_32(big_endian_32bits);
|
||||
return internal_bswap_32(big_endian_32bits);
|
||||
}
|
||||
#endif // HAVE_DECL_BE32TOH
|
||||
|
||||
|
@ -211,7 +211,7 @@ inline uint32_t le32toh(uint32_t little_endian_32bits)
|
|||
#if HAVE_DECL_HTOBE64 == 0
|
||||
inline uint64_t htobe64(uint64_t host_64bits)
|
||||
{
|
||||
return bswap_64(host_64bits);
|
||||
return internal_bswap_64(host_64bits);
|
||||
}
|
||||
#endif // HAVE_DECL_HTOBE64
|
||||
|
||||
|
@ -225,7 +225,7 @@ inline uint64_t htole64(uint64_t host_64bits)
|
|||
#if HAVE_DECL_BE64TOH == 0
|
||||
inline uint64_t be64toh(uint64_t big_endian_64bits)
|
||||
{
|
||||
return bswap_64(big_endian_64bits);
|
||||
return internal_bswap_64(big_endian_64bits);
|
||||
}
|
||||
#endif // HAVE_DECL_BE64TOH
|
||||
|
||||
|
|
|
@ -16,9 +16,9 @@ BOOST_AUTO_TEST_CASE(bswap_tests)
|
|||
uint16_t e1 = 0x3412;
|
||||
uint32_t e2 = 0xbc9a7856;
|
||||
uint64_t e3 = 0xbc9a78563412f0de;
|
||||
BOOST_CHECK(bswap_16(u1) == e1);
|
||||
BOOST_CHECK(bswap_32(u2) == e2);
|
||||
BOOST_CHECK(bswap_64(u3) == e3);
|
||||
BOOST_CHECK(internal_bswap_16(u1) == e1);
|
||||
BOOST_CHECK(internal_bswap_32(u2) == e2);
|
||||
BOOST_CHECK(internal_bswap_64(u3) == e3);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
|
Loading…
Reference in a new issue