veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
endian.hpp
1
#pragma once
2
3
#include <cstdint>
4
5
#if defined(__MINGW32__) || defined(_WIN32)
6
#define LITTLE_ENDIAN 1234
7
#define BYTE_ORDER LITTLE_ENDIAN
8
#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
9
#include <sys/endian.h>
10
#elif defined(__OpenBSD__) || defined(__SVR4)
11
#include <sys/types.h>
12
#elif defined(__APPLE__)
13
#include <machine/endian.h>
14
#elif defined(BSD) && (BSD >= 199103)
15
#include <machine/endian.h>
16
#elif defined(__QNXNTO__) && defined(__LITTLEENDIAN__)
17
#define LITTLE_ENDIAN 1234
18
#define BYTE_ORDER LITTLE_ENDIAN
19
#elif defined(__QNXNTO__) && defined(__BIGENDIAN__)
20
#define BIG_ENDIAN 1234
21
#define BYTE_ORDER BIG_ENDIAN
22
#else
23
#include <endian.h>
24
#endif
25
26
#if defined(VBK_HAS_BUILTIN_BYTESWAP32) and defined(VBK_HAS_BUILTIN_BYTESWAP64)
27
#define vbk_swap_u32(input_) __builtin_bswap32(input_)
28
#define vbk_swap_u64(input_) __builtin_bswap64(input_)
29
#else
30
31
#if defined(_WIN32)
32
#include <stdlib.h>
33
#define vbk_swap_u32(input_) _byteswap_ulong(input_)
34
#define vbk_swap_u64(input_) _byteswap_uint64(input_)
35
#elif defined(__APPLE__)
36
#include <libkern/OSByteOrder.h>
37
#define vbk_swap_u32(input_) OSSwapInt32(input_)
38
#define vbk_swap_u64(input_) OSSwapInt64(input_)
39
#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
40
#define vbk_swap_u32(input_) bswap32(input_)
41
#define vbk_swap_u64(input_) bswap64(input_)
42
#else
// posix
43
#include <byteswap.h>
44
#define vbk_swap_u32(input_) __bswap_32(input_)
45
#define vbk_swap_u64(input_) __bswap_64(input_)
46
#endif
// posix
47
48
#endif
// defined(VBK_HAS_BUILTIN_BYTESWAP32) and
49
// defined(VBK_HAS_BUILTIN_BYTESWAP64)
50
51
#if LITTLE_ENDIAN == BYTE_ORDER
52
53
#define fix_endian32(dst_, src_) dst_ = src_
54
#define fix_endian32_same(val_)
55
#define fix_endian64(dst_, src_) dst_ = src_
56
#define fix_endian64_same(val_)
57
#define fix_endian_arr32(arr_, size_)
58
#define fix_endian_arr64(arr_, size_)
59
60
#elif BIG_ENDIAN == BYTE_ORDER
61
62
#define fix_endian32(dst_, src_) dst_ = vbk_swap_u32(src_)
63
#define fix_endian32_same(val_) val_ = vbk_swap_u32(val_)
64
#define fix_endian64(dst_, src_) dst_ = vbk_swap_u64(src_
65
#define fix_endian64_same(val_) val_ = vbk_swap_u64(val_)
66
#define fix_endian_arr32(arr_, size_) \
67
do { \
68
for (unsigned i_ = 0; i_ < (size_), ++i_) { \
69
arr_[i_] = vbk_swap_u32(arr_[i_]); \
70
} \
71
while (0)
72
#define fix_endian_arr64(arr_, size_) \
73
do { \
74
for (unsigned i_ = 0; i_ < (size_), ++i_) { \
75
arr_[i_] = vbk_swap_u64(arr_[i_]); \
76
} \
77
while (0)
78
79
#else
80
#error "endian not supported"
81
#endif
// BYTE_ORDER