Branch data Line data Source code
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 : : # define bitcoin_builtin_bswap16(x) __builtin_bswap16(x)
18 : : # endif
19 : : # if __has_builtin(__builtin_bswap32)
20 : : # define bitcoin_builtin_bswap32(x) __builtin_bswap32(x)
21 : : # endif
22 : : # if __has_builtin(__builtin_bswap64)
23 : : # define bitcoin_builtin_bswap64(x) __builtin_bswap64(x)
24 : : # endif
25 : : # endif
26 : : #endif
27 : :
28 : 16926648 : inline constexpr uint16_t internal_bswap_16(uint16_t x)
29 : : {
30 : : #ifdef bitcoin_builtin_bswap16
31 [ - + ]: 16926235 : return bitcoin_builtin_bswap16(x);
[ + - + +
+ + + + +
- + + ]
[ + - - +
- - - - +
+ + - + -
+ - - - +
+ - - - -
- - - - -
- - - - -
+ + + + +
+ + + ]
32 : : #else
33 : : return (x >> 8) | (x << 8);
34 : : #endif
35 : : }
36 : :
37 : 6363032915 : inline constexpr uint32_t internal_bswap_32(uint32_t x)
38 : : {
39 : : #ifdef bitcoin_builtin_bswap32
40 [ + + ]: 318061969 : 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 : : }
46 : :
47 : 2234148024 : inline constexpr uint64_t internal_bswap_64(uint64_t x)
48 : : {
49 : : #ifdef bitcoin_builtin_bswap64
50 [ - + ]: 92170247 : 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 : : }
62 : :
63 : : #endif // BITCOIN_COMPAT_BYTESWAP_H
|