/tmp/bitcoin/src/compat/byteswap.h
Line | Count | Source |
1 | | // Copyright (c) 2014-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #ifndef BITCOIN_COMPAT_BYTESWAP_H |
6 | | #define BITCOIN_COMPAT_BYTESWAP_H |
7 | | |
8 | | #include <cstdint> |
9 | | |
10 | | // All internal_bswap_* functions can be replaced with std::byteswap once we |
11 | | // require c++23. Both libstdc++ and libc++ implement std::byteswap via these |
12 | | // builtins. |
13 | | |
14 | | #ifndef DISABLE_BUILTIN_BSWAPS |
15 | | # if defined __has_builtin |
16 | | # if __has_builtin(__builtin_bswap16) |
17 | 27.4k | # define bitcoin_builtin_bswap16(x) __builtin_bswap16(x) |
18 | | # endif |
19 | | # if __has_builtin(__builtin_bswap32) |
20 | 716M | # define bitcoin_builtin_bswap32(x) __builtin_bswap32(x) |
21 | | # endif |
22 | | # if __has_builtin(__builtin_bswap64) |
23 | 1.06G | # define bitcoin_builtin_bswap64(x) __builtin_bswap64(x) |
24 | | # endif |
25 | | # endif |
26 | | #endif |
27 | | |
28 | | inline constexpr uint16_t internal_bswap_16(uint16_t x) |
29 | 27.4k | { |
30 | 27.4k | #ifdef bitcoin_builtin_bswap16 |
31 | 27.4k | return bitcoin_builtin_bswap16(x); |
32 | | #else |
33 | | return (x >> 8) | (x << 8); |
34 | | #endif |
35 | 27.4k | } |
36 | | |
37 | | inline constexpr uint32_t internal_bswap_32(uint32_t x) |
38 | 716M | { |
39 | 716M | #ifdef bitcoin_builtin_bswap32 |
40 | 716M | return bitcoin_builtin_bswap32(x); |
41 | | #else |
42 | | return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) | |
43 | | ((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24)); |
44 | | #endif |
45 | 716M | } |
46 | | |
47 | | inline constexpr uint64_t internal_bswap_64(uint64_t x) |
48 | 1.06G | { |
49 | 1.06G | #ifdef bitcoin_builtin_bswap64 |
50 | 1.06G | return bitcoin_builtin_bswap64(x); |
51 | | #else |
52 | | return (((x & 0xff00000000000000ull) >> 56) |
53 | | | ((x & 0x00ff000000000000ull) >> 40) |
54 | | | ((x & 0x0000ff0000000000ull) >> 24) |
55 | | | ((x & 0x000000ff00000000ull) >> 8) |
56 | | | ((x & 0x00000000ff000000ull) << 8) |
57 | | | ((x & 0x0000000000ff0000ull) << 24) |
58 | | | ((x & 0x000000000000ff00ull) << 40) |
59 | | | ((x & 0x00000000000000ffull) << 56)); |
60 | | #endif |
61 | 1.06G | } |
62 | | |
63 | | #endif // BITCOIN_COMPAT_BYTESWAP_H |