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.
4 
5 #ifndef BITCOIN_COMPAT_BYTESWAP_H
6 #define BITCOIN_COMPAT_BYTESWAP_H
7 
8 #if defined(HAVE_CONFIG_H)
9 #include "config/bitcoin-config.h"
10 #endif
11 
12 #include <stdint.h>
13 
14 #if defined(HAVE_BYTESWAP_H)
15 #include <byteswap.h>
16 #endif
17 
18 #if HAVE_DECL_BSWAP_16 == 0
bswap_16(uint16_t x)19 inline uint16_t bswap_16(uint16_t x)
20 {
21     return (x >> 8) | ((x & 0x00ff) << 8);
22 }
23 #endif // HAVE_DECL_BSWAP16
24 
25 #if HAVE_DECL_BSWAP_32 == 0
bswap_32(uint32_t x)26 inline uint32_t bswap_32(uint32_t x)
27 {
28     return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >>  8) |
29             ((x & 0x0000ff00U) <<  8) | ((x & 0x000000ffU) << 24));
30 }
31 #endif // HAVE_DECL_BSWAP32
32 
33 #if HAVE_DECL_BSWAP_64 == 0
bswap_64(uint64_t x)34 inline uint64_t bswap_64(uint64_t x)
35 {
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));
44 }
45 #endif // HAVE_DECL_BSWAP64
46 
47 #endif // BITCOIN_COMPAT_BYTESWAP_H
48