-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEndian.h
89 lines (76 loc) · 2.68 KB
/
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
//
// Created by Bencz on 24/04/2020.
//
#ifndef DNA_ENDIAN_H
#define DNA_ENDIAN_H
#if _WIN32 || _WIN64
#ifndef BIGENDIAN
#define BIGENDIAN 0
#endif
#else
# include <endian.h>
# if __BYTE_ORDER == __BIG_ENDIAN
# define BIGENDIAN 1
# endif
#endif
#include <stdint.h>
#define UINT16_SWAP_LE_BE_CONSTANT(x) ((((uint16_t) x) >> 8) | ((((uint16_t) x) << 8)))
#define UINT16_SWAP_LE_BE(x) ((uint16_t) (((uint16_t) x) >> 8) | ((((uint16_t)(x)) & 0xff) << 8))
#define UINT32_SWAP_LE_BE(x) ((uint32_t) \
( (((uint32_t) (x)) << 24)| \
((((uint32_t) (x)) & 0xff0000) >> 8) | \
((((uint32_t) (x)) & 0xff00) << 8) | \
(((uint32_t) (x)) >> 24)) )
#define UINT64_SWAP_LE_BE(x) ((uint64_t) (((uint64_t)(UINT32_SWAP_LE_BE(((uint64_t)x) & 0xffffffff))) << 32) | \
UINT32_SWAP_LE_BE(((uint64_t)x) >> 32))
#if BIGENDIAN == 1
# define UINT64_FROM_BE(x) (x)
# define UINT32_FROM_BE(x) (x)
# define UINT16_FROM_BE(x) (x)
# define UINT_FROM_BE(x) (x)
# define UINT64_FROM_LE(x) UINT64_SWAP_LE_BE(x)
# define UINT32_FROM_LE(x) UINT32_SWAP_LE_BE(x)
# define UINT16_FROM_LE(x) UINT16_SWAP_LE_BE(x)
# define UINT_FROM_LE(x) UINT32_SWAP_LE_BE(x)
# define UINT64_TO_BE(x) (x)
# define UINT32_TO_BE(x) (x)
# define UINT16_TO_BE(x) (x)
# define UINT_TO_BE(x) (x)
# define UINT64_TO_LE(x) UINT64_SWAP_LE_BE(x)
# define UINT32_TO_LE(x) UINT32_SWAP_LE_BE(x)
# define UINT16_TO_LE(x) UINT16_SWAP_LE_BE(x)
# define UINT_TO_LE(x) UINT32_SWAP_LE_BE(x)
#else
# define UINT64_FROM_BE(x) UINT64_SWAP_LE_BE(x)
# define UINT32_FROM_BE(x) UINT32_SWAP_LE_BE(x)
# define UINT16_FROM_BE(x) UINT16_SWAP_LE_BE(x)
# define UINT_FROM_BE(x) UINT32_SWAP_LE_BE(x)
# define UINT64_FROM_LE(x) (x)
# define UINT32_FROM_LE(x) (x)
# define UINT16_FROM_LE(x) (x)
# define UINT_FROM_LE(x) (x)
# define UINT64_TO_BE(x) UINT64_SWAP_LE_BE(x)
# define UINT32_TO_BE(x) UINT32_SWAP_LE_BE(x)
# define UINT16_TO_BE(x) UINT16_SWAP_LE_BE(x)
# define UINT_TO_BE(x) UINT32_SWAP_LE_BE(x)
# define UINT64_TO_LE(x) (x)
# define UINT32_TO_LE(x) (x)
# define UINT16_TO_LE(x) (x)
# define UINT_TO_LE(x) (x)
#endif
#if BIGENDIAN == 1
# define SWAP16(x) (x) = UINT16_FROM_LE ((x))
# define SWAP32(x) (x) = UINT32_FROM_LE ((x))
# define SWAP64(x) (x) = UINT64_FROM_LE ((x))
#else
# define SWAP64(x)
# define SWAP32(x)
# define SWAP16(x)
#endif
#if BIGENDIAN == 0 || BIGENDIAN == 1
# define READ16(x) UINT16_FROM_LE (*((const uint16_t *) (x)))
# define READ32(x) UINT32_FROM_LE (*((const uint32_t *) (x)))
# define READ64(x) UINT64_FROM_LE (*((const uint64_t *) (x)))
#else
#endif
#endif //DNA_ENDIAN_H