diff options
author | Cory Fields <cory-nospam-@coryfields.com> | 2024-02-27 18:29:24 +0000 |
---|---|---|
committer | Cory Fields <cory-nospam-@coryfields.com> | 2024-02-28 13:42:38 +0000 |
commit | 432b18ca8d0654318a8d882b28b20af2cb2d2e5d (patch) | |
tree | c3ff23d3ca9195fa94b4a5ad757ac2e2682e63d5 /src/compat/byteswap.h | |
parent | 297367b3bb062c57142747719ac9bf2e12717ce9 (diff) |
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.
Diffstat (limited to 'src/compat/byteswap.h')
-rw-r--r-- | src/compat/byteswap.h | 66 |
1 files changed, 43 insertions, 23 deletions
diff --git a/src/compat/byteswap.h b/src/compat/byteswap.h index 9ee71ef267..0d0b079230 100644 --- a/src/compat/byteswap.h +++ b/src/compat/byteswap.h @@ -5,44 +5,66 @@ #ifndef BITCOIN_COMPAT_BYTESWAP_H #define BITCOIN_COMPAT_BYTESWAP_H -#if defined(HAVE_CONFIG_H) -#include <config/bitcoin-config.h> +#include <cstdint> +#ifdef _MSC_VER +#include <cstdlib> #endif -#include <cstdint> -#if defined(HAVE_BYTESWAP_H) -#include <byteswap.h> -#endif +// 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. -#if defined(MAC_OSX) +#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 -#include <libkern/OSByteOrder.h> -#define bswap_16(x) OSSwapInt16(x) -#define bswap_32(x) OSSwapInt32(x) -#define bswap_64(x) OSSwapInt64(x) +// 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 |