Raven Core  3.0.0
P2P Digital Currency
byteswap.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_COMPAT_BYTESWAP_H
7 #define RAVEN_COMPAT_BYTESWAP_H
8 
9 #if defined(HAVE_CONFIG_H)
10 #include "config/raven-config.h"
11 #endif
12 
13 #include <stdint.h>
14 
15 #if defined(HAVE_BYTESWAP_H)
16 #include <byteswap.h>
17 #endif
18 
19 #if defined(__APPLE__)
20 
21 #if !defined(bswap_16)
22 
23 // Mac OS X / Darwin features; we include a check for bswap_16 because if it is already defined, protobuf has
24 // defined these macros for us already; if it isn't, we do it ourselves. In either case, we get the exact same
25 // result regardless which path was taken
26 #include <libkern/OSByteOrder.h>
27 #define bswap_16(x) OSSwapInt16(x)
28 #define bswap_32(x) OSSwapInt32(x)
29 #define bswap_64(x) OSSwapInt64(x)
30 
31 #endif // !defined(bswap_16)
32 
33 #else
34 // Non-Mac OS X / non-Darwin
35 
36 #if HAVE_DECL_BSWAP_16 == 0
37 inline uint16_t bswap_16(uint16_t x)
38 {
39  return (x >> 8) | (x << 8);
40 }
41 #endif // HAVE_DECL_BSWAP16
42 
43 #if HAVE_DECL_BSWAP_32 == 0
44 inline uint32_t bswap_32(uint32_t x)
45 {
46  return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
47  ((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
48 }
49 #endif // HAVE_DECL_BSWAP32
50 
51 #if HAVE_DECL_BSWAP_64 == 0
52 inline uint64_t bswap_64(uint64_t x)
53 {
54  return (((x & 0xff00000000000000ull) >> 56)
55  | ((x & 0x00ff000000000000ull) >> 40)
56  | ((x & 0x0000ff0000000000ull) >> 24)
57  | ((x & 0x000000ff00000000ull) >> 8)
58  | ((x & 0x00000000ff000000ull) << 8)
59  | ((x & 0x0000000000ff0000ull) << 24)
60  | ((x & 0x000000000000ff00ull) << 40)
61  | ((x & 0x00000000000000ffull) << 56));
62 }
63 #endif // HAVE_DECL_BSWAP64
64 
65 #endif // defined(__APPLE__)
66 
67 #endif // RAVEN_COMPAT_BYTESWAP_H
uint32_t bswap_32(uint32_t x)
Definition: byteswap.h:44
uint16_t bswap_16(uint16_t x)
Definition: byteswap.h:37
uint64_t bswap_64(uint64_t x)
Definition: byteswap.h:52