1 // Copyright (c) 2014 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_COMPAT_BYTESWAP_H
6 #define BITCOIN_COMPAT_BYTESWAP_H
8 #if defined(HAVE_CONFIG_H)
9 #include "config/bitcoin-config.h"
14 #if defined(HAVE_BYTESWAP_H)
18 #if HAVE_DECL_BSWAP_16 == 0
19 inline uint16_t bswap_16(uint16_t x
)
21 return (x
>> 8) | ((x
& 0x00ff) << 8);
23 #endif // HAVE_DECL_BSWAP16
25 #if HAVE_DECL_BSWAP_32 == 0
26 inline uint32_t bswap_32(uint32_t x
)
28 return (((x
& 0xff000000U
) >> 24) | ((x
& 0x00ff0000U
) >> 8) |
29 ((x
& 0x0000ff00U
) << 8) | ((x
& 0x000000ffU
) << 24));
31 #endif // HAVE_DECL_BSWAP32
33 #if HAVE_DECL_BSWAP_64 == 0
34 inline uint64_t bswap_64(uint64_t x
)
36 return (((x
& 0xff00000000000000ull
) >> 56)
37 | ((x
& 0x00ff000000000000ull
) >> 40)
38 | ((x
& 0x0000ff0000000000ull
) >> 24)
39 | ((x
& 0x000000ff00000000ull
) >> 8)
40 | ((x
& 0x00000000ff000000ull
) << 8)
41 | ((x
& 0x0000000000ff0000ull
) << 24)
42 | ((x
& 0x000000000000ff00ull
) << 40)
43 | ((x
& 0x00000000000000ffull
) << 56));
45 #endif // HAVE_DECL_BSWAP64
47 #endif // BITCOIN_COMPAT_BYTESWAP_H