-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_endian.h
371 lines (316 loc) · 8.25 KB
/
fs_endian.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright (c) 2014-2018 The Bitcoin Core developers
// Copyright (c) 2020 The SorachanCoin Developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef SORACHANCOIN_FS_ENDIAN
#define SORACHANCOIN_FS_ENDIAN
#include <stddef.h>
#include "fs_types.h"
static inline uindex_t getnumbits(uindex_t val) {
val=(val&0x55555555)+((val>>1)&0x55555555);
val=(val&0x33333333)+((val>>2)&0x33333333);
val=(val&0x0f0f0f0f)+((val>>4)&0x0f0f0f0f);
val=(val&0x00ff00ff)+((val>>8)&0x00ff00ff);
return (val&0x0000ffff)+((val>>16)&0x0000ffff);
}
static inline uindex_t getLsb_32(uindex_t val) {
uindex_t tmp=val;
val|=(val<<1);
val|=(val<<2);
val|=(val<<4);
val|=(val<<8);
val|=(val<<16);
return (tmp==0) ? 0: 32-getnumbits(val);
}
static inline uindex_t getMsb_32(uindex_t val) {
uindex_t tmp=val;
val|=(val>>1);
val|=(val>>2);
val|=(val>>4);
val|=(val>>8);
val|=(val>>16);
return (tmp==0) ? 0: getnumbits(val)-1;
}
static inline uint16_t bswap_16(uint16_t x)
{
return (x >> 8) | (x << 8);
}
static inline uint32_t bswap_32(uint32_t x)
{
return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
}
static inline uint64_t bswap_64(uint64_t x)
{
return (((x & 0xff00000000000000ull) >> 56)
| ((x & 0x00ff000000000000ull) >> 40)
| ((x & 0x0000ff0000000000ull) >> 24)
| ((x & 0x000000ff00000000ull) >> 8)
| ((x & 0x00000000ff000000ull) << 8)
| ((x & 0x0000000000ff0000ull) << 24)
| ((x & 0x000000000000ff00ull) << 40)
| ((x & 0x00000000000000ffull) << 56));
}
#if defined(HAVE_ENDIAN_H)
#include <endian.h>
#elif defined(HAVE_SYS_ENDIAN_H)
#include <sys/endian.h>
#endif
#ifndef HAVE_CONFIG_H
// While not technically a supported configuration, defaulting to defining these
// DECLs when we were compiled without autotools makes it easier for other build
// systems to build things like libbitcoinconsensus for strange targets.
#ifdef htobe16
#define HAVE_DECL_HTOBE16 1
#endif
#ifdef htole16
#define HAVE_DECL_HTOLE16 1
#endif
#ifdef be16toh
#define HAVE_DECL_BE16TOH 1
#endif
#ifdef le16toh
#define HAVE_DECL_LE16TOH 1
#endif
#ifdef htobe32
#define HAVE_DECL_HTOBE32 1
#endif
#ifdef htole32
#define HAVE_DECL_HTOLE32 1
#endif
#ifdef be32toh
#define HAVE_DECL_BE32TOH 1
#endif
#ifdef le32toh
#define HAVE_DECL_LE32TOH 1
#endif
#ifdef htobe64
#define HAVE_DECL_HTOBE64 1
#endif
#ifdef htole64
#define HAVE_DECL_HTOLE64 1
#endif
#ifdef be64toh
#define HAVE_DECL_BE64TOH 1
#endif
#ifdef le64toh
#define HAVE_DECL_LE64TOH 1
#endif
#endif // HAVE_CONFIG_H
#if defined(WORDS_BIGENDIAN)
static inline uindex_t fs_getLsb32(uindex_t val) {
val=bswap_32(val);
return getLsb_32(val);
}
static inline uindex_t fs_getMsb32(uindex_t val) {
val=bswap_32(val);
return getMsb_32(val);
}
#if HAVE_DECL_HTOBE16 == 0
static inline uint16_t htobe16(uint16_t host_16bits)
{
return host_16bits;
}
#endif // HAVE_DECL_HTOBE16
#if HAVE_DECL_HTOLE16 == 0
static inline uint16_t htole16(uint16_t host_16bits)
{
return bswap_16(host_16bits);
}
#endif // HAVE_DECL_HTOLE16
#if HAVE_DECL_BE16TOH == 0
static inline uint16_t be16toh(uint16_t big_endian_16bits)
{
return big_endian_16bits;
}
#endif // HAVE_DECL_BE16TOH
#if HAVE_DECL_LE16TOH == 0
static inline uint16_t le16toh(uint16_t little_endian_16bits)
{
return bswap_16(little_endian_16bits);
}
#endif // HAVE_DECL_LE16TOH
#if HAVE_DECL_HTOBE32 == 0
static inline uint32_t htobe32(uint32_t host_32bits)
{
return host_32bits;
}
#endif // HAVE_DECL_HTOBE32
#if HAVE_DECL_HTOLE32 == 0
static inline uint32_t htole32(uint32_t host_32bits)
{
return bswap_32(host_32bits);
}
#endif // HAVE_DECL_HTOLE32
#if HAVE_DECL_BE32TOH == 0
static inline uint32_t be32toh(uint32_t big_endian_32bits)
{
return big_endian_32bits;
}
#endif // HAVE_DECL_BE32TOH
#if HAVE_DECL_LE32TOH == 0
static inline uint32_t le32toh(uint32_t little_endian_32bits)
{
return bswap_32(little_endian_32bits);
}
#endif // HAVE_DECL_LE32TOH
#if HAVE_DECL_HTOBE64 == 0
static inline uint64_t htobe64(uint64_t host_64bits)
{
return host_64bits;
}
#endif // HAVE_DECL_HTOBE64
#if HAVE_DECL_HTOLE64 == 0
static inline uint64_t htole64(uint64_t host_64bits)
{
return bswap_64(host_64bits);
}
#endif // HAVE_DECL_HTOLE64
#if HAVE_DECL_BE64TOH == 0
static inline uint64_t be64toh(uint64_t big_endian_64bits)
{
return big_endian_64bits;
}
#endif // HAVE_DECL_BE64TOH
#if HAVE_DECL_LE64TOH == 0
static inline uint64_t le64toh(uint64_t little_endian_64bits)
{
return bswap_64(little_endian_64bits);
}
#endif // HAVE_DECL_LE64TOH
#else // WORDS_BIGENDIAN
static inline uindex_t fs_getLsb32(uindex_t val) {
return getLsb_32(val);
}
static inline uindex_t fs_getMsb32(uindex_t val) {
return getMsb_32(val);
}
#if HAVE_DECL_HTOBE16 == 0
static inline uint16_t htobe16(uint16_t host_16bits)
{
return bswap_16(host_16bits);
}
#endif // HAVE_DECL_HTOBE16
#if HAVE_DECL_HTOLE16 == 0
static inline uint16_t htole16(uint16_t host_16bits)
{
return host_16bits;
}
#endif // HAVE_DECL_HTOLE16
#if HAVE_DECL_BE16TOH == 0
static inline uint16_t be16toh(uint16_t big_endian_16bits)
{
return bswap_16(big_endian_16bits);
}
#endif // HAVE_DECL_BE16TOH
#if HAVE_DECL_LE16TOH == 0
static inline uint16_t le16toh(uint16_t little_endian_16bits)
{
return little_endian_16bits;
}
#endif // HAVE_DECL_LE16TOH
#if HAVE_DECL_HTOBE32 == 0
static inline uint32_t htobe32(uint32_t host_32bits)
{
return bswap_32(host_32bits);
}
#endif // HAVE_DECL_HTOBE32
#if HAVE_DECL_HTOLE32 == 0
static inline uint32_t htole32(uint32_t host_32bits)
{
return host_32bits;
}
#endif // HAVE_DECL_HTOLE32
#if HAVE_DECL_BE32TOH == 0
static inline uint32_t be32toh(uint32_t big_endian_32bits)
{
return bswap_32(big_endian_32bits);
}
#endif // HAVE_DECL_BE32TOH
#if HAVE_DECL_LE32TOH == 0
static inline uint32_t le32toh(uint32_t little_endian_32bits)
{
return little_endian_32bits;
}
#endif // HAVE_DECL_LE32TOH
#if HAVE_DECL_HTOBE64 == 0
static inline uint64_t htobe64(uint64_t host_64bits)
{
return bswap_64(host_64bits);
}
#endif // HAVE_DECL_HTOBE64
#if HAVE_DECL_HTOLE64 == 0
static inline uint64_t htole64(uint64_t host_64bits)
{
return host_64bits;
}
#endif // HAVE_DECL_HTOLE64
#if HAVE_DECL_BE64TOH == 0
static inline uint64_t be64toh(uint64_t big_endian_64bits)
{
return bswap_64(big_endian_64bits);
}
#endif // HAVE_DECL_BE64TOH
#if HAVE_DECL_LE64TOH == 0
static inline uint64_t le64toh(uint64_t little_endian_64bits)
{
return little_endian_64bits;
}
#endif // HAVE_DECL_LE64TOH
#endif // WORDS_BIGENDIAN
static inline uint16_t ReadLE16(const byte_t *ptr)
{
uint16_t x;
memcpy_s((char *)&x, sizeof(uint16_t), ptr, sizeof(uint16_t));
return le16toh(x);
}
static inline uint32_t ReadLE32(const byte_t *ptr)
{
uint32_t x;
memcpy_s((char *)&x, sizeof(uint32_t), ptr, sizeof(uint32_t));
return le32toh(x);
}
static inline uint64_t ReadLE64(const byte_t *ptr)
{
uint64_t x;
memcpy_s((char *)&x, sizeof(uint64_t), ptr, sizeof(uint64_t));
return le64toh(x);
}
static inline void WriteLE16(byte_t *ptr, uint16_t x)
{
uint16_t v = htole16(x);
memcpy_s(ptr, sizeof(uint16_t), (const char *)&v, sizeof(uint16_t));
}
static inline void WriteLE32(byte_t *ptr, uint32_t x)
{
uint32_t v = htole32(x);
memcpy_s(ptr, sizeof(uint32_t), (const char *)&v, sizeof(uint32_t));
}
static inline void WriteLE64(byte_t *ptr, uint64_t x)
{
uint64_t v = htole64(x);
memcpy_s(ptr, sizeof(uint64_t), (const char *)&v, sizeof(uint64_t));
}
static inline uint32_t ReadBE32(const byte_t *ptr)
{
uint32_t x;
memcpy_s((char *)&x, sizeof(uint32_t), ptr, sizeof(uint32_t));
return be32toh(x);
}
static inline uint64_t ReadBE64(const byte_t *ptr)
{
uint64_t x;
memcpy_s((char *)&x, sizeof(uint64_t), ptr, sizeof(uint64_t));
return be64toh(x);
}
static inline void WriteBE32(byte_t *ptr, uint32_t x)
{
uint32_t v = htobe32(x);
memcpy_s(ptr, sizeof(uint32_t), (const char *)&v, sizeof(uint32_t));
}
static inline void WriteBE64(byte_t *ptr, uint64_t x)
{
uint64_t v = htobe64(x);
memcpy_s(ptr, sizeof(uint64_t), (const char *)&v, sizeof(uint64_t));
}
#endif // SORACHANCOIN_FS_ENDIAN