Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
endian.h
Go to the documentation of this file.
1// Copyright (c) 2014-2016 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_ENDIAN_H
6#define BITCOIN_COMPAT_ENDIAN_H
7
8#if defined(HAVE_CONFIG_H)
9#include <config/bitcoin-config.h>
10#endif
11
12#include <compat/byteswap.h>
13
14#include <cstdint>
15
16#if defined(HAVE_ENDIAN_H)
17#include <endian.h>
18#elif defined(HAVE_SYS_ENDIAN_H)
19#include <sys/endian.h>
20#endif
21
22#ifndef HAVE_CONFIG_H
23// While not technically a supported configuration, defaulting to defining these
24// DECLs when we were compiled without autotools makes it easier for other build
25// systems to build things like libbitcoinconsensus for strange targets.
26#ifdef htobe16
27#define HAVE_DECL_HTOBE16 1
28#endif
29#ifdef htole16
30#define HAVE_DECL_HTOLE16 1
31#endif
32#ifdef be16toh
33#define HAVE_DECL_BE16TOH 1
34#endif
35#ifdef le16toh
36#define HAVE_DECL_LE16TOH 1
37#endif
38
39#ifdef htobe32
40#define HAVE_DECL_HTOBE32 1
41#endif
42#ifdef htole32
43#define HAVE_DECL_HTOLE32 1
44#endif
45#ifdef be32toh
46#define HAVE_DECL_BE32TOH 1
47#endif
48#ifdef le32toh
49#define HAVE_DECL_LE32TOH 1
50#endif
51
52#ifdef htobe64
53#define HAVE_DECL_HTOBE64 1
54#endif
55#ifdef htole64
56#define HAVE_DECL_HTOLE64 1
57#endif
58#ifdef be64toh
59#define HAVE_DECL_BE64TOH 1
60#endif
61#ifdef le64toh
62#define HAVE_DECL_LE64TOH 1
63#endif
64
65#endif // HAVE_CONFIG_H
66
67#if defined(WORDS_BIGENDIAN)
68
69#if HAVE_DECL_HTOBE16 == 0
71 return host_16bits;
72}
73#endif // HAVE_DECL_HTOBE16
74
75#if HAVE_DECL_HTOLE16 == 0
77 return bswap_16(host_16bits);
78}
79#endif // HAVE_DECL_HTOLE16
80
81#if HAVE_DECL_BE16TOH == 0
83 return big_endian_16bits;
84}
85#endif // HAVE_DECL_BE16TOH
86
87#if HAVE_DECL_LE16TOH == 0
90}
91#endif // HAVE_DECL_LE16TOH
92
93#if HAVE_DECL_HTOBE32 == 0
95 return host_32bits;
96}
97#endif // HAVE_DECL_HTOBE32
98
99#if HAVE_DECL_HTOLE32 == 0
101 return bswap_32(host_32bits);
102}
103#endif // HAVE_DECL_HTOLE32
104
105#if HAVE_DECL_BE32TOH == 0
107 return big_endian_32bits;
108}
109#endif // HAVE_DECL_BE32TOH
110
111#if HAVE_DECL_LE32TOH == 0
114}
115#endif // HAVE_DECL_LE32TOH
116
117#if HAVE_DECL_HTOBE64 == 0
119 return host_64bits;
120}
121#endif // HAVE_DECL_HTOBE64
122
123#if HAVE_DECL_HTOLE64 == 0
125 return bswap_64(host_64bits);
126}
127#endif // HAVE_DECL_HTOLE64
128
129#if HAVE_DECL_BE64TOH == 0
131 return big_endian_64bits;
132}
133#endif // HAVE_DECL_BE64TOH
134
135#if HAVE_DECL_LE64TOH == 0
138}
139#endif // HAVE_DECL_LE64TOH
140
141#else // WORDS_BIGENDIAN
142
143#if HAVE_DECL_HTOBE16 == 0
147#endif // HAVE_DECL_HTOBE16
148
149#if HAVE_DECL_HTOLE16 == 0
153#endif // HAVE_DECL_HTOLE16
154
155#if HAVE_DECL_BE16TOH == 0
159#endif // HAVE_DECL_BE16TOH
160
161#if HAVE_DECL_LE16TOH == 0
165#endif // HAVE_DECL_LE16TOH
166
167#if HAVE_DECL_HTOBE32 == 0
171#endif // HAVE_DECL_HTOBE32
172
173#if HAVE_DECL_HTOLE32 == 0
177#endif // HAVE_DECL_HTOLE32
178
179#if HAVE_DECL_BE32TOH == 0
183#endif // HAVE_DECL_BE32TOH
184
185#if HAVE_DECL_LE32TOH == 0
189#endif // HAVE_DECL_LE32TOH
190
191#if HAVE_DECL_HTOBE64 == 0
195#endif // HAVE_DECL_HTOBE64
196
197#if HAVE_DECL_HTOLE64 == 0
201#endif // HAVE_DECL_HTOLE64
202
203#if HAVE_DECL_BE64TOH == 0
207#endif // HAVE_DECL_BE64TOH
208
209#if HAVE_DECL_LE64TOH == 0
213#endif // HAVE_DECL_LE64TOH
214
215#endif // WORDS_BIGENDIAN
216
217#endif // BITCOIN_COMPAT_ENDIAN_H
uint16_t bswap_16(uint16_t x)
Definition byteswap.h:37
uint64_t bswap_64(uint64_t x)
Definition byteswap.h:50
uint32_t bswap_32(uint32_t x)
Definition byteswap.h:43
uint16_t be16toh(uint16_t big_endian_16bits)
Definition endian.h:156
uint16_t htobe16(uint16_t host_16bits)
Definition endian.h:144
uint32_t le32toh(uint32_t little_endian_32bits)
Definition endian.h:186
uint32_t htobe32(uint32_t host_32bits)
Definition endian.h:168
uint16_t le16toh(uint16_t little_endian_16bits)
Definition endian.h:162
uint64_t htobe64(uint64_t host_64bits)
Definition endian.h:192
uint64_t be64toh(uint64_t big_endian_64bits)
Definition endian.h:204
uint32_t be32toh(uint32_t big_endian_32bits)
Definition endian.h:180
uint64_t htole64(uint64_t host_64bits)
Definition endian.h:198
uint32_t htole32(uint32_t host_32bits)
Definition endian.h:174
uint16_t htole16(uint16_t host_16bits)
Definition endian.h:150
uint64_t le64toh(uint64_t little_endian_64bits)
Definition endian.h:210
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85