03b01d2 |
Jamozed |
2022-03-06 17:10:01 |
0
|
// util/endian.h, version 1.0.2 |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
1
|
// Endian header file from libutil |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
2
|
// Copyright (C) 2021, Jakob Wakeling |
03b01d2 |
Jamozed |
2022-03-06 17:10:01 |
3
|
// MIT Licence |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
4
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
5
|
#ifndef UTIL_ENDIAN_H_G7AID2RQ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
6
|
#define UTIL_ENDIAN_H_G7AID2RQ |
03b01d2 |
Jamozed |
2022-03-06 17:10:01 |
7
|
|
03b01d2 |
Jamozed |
2022-03-06 17:10:01 |
8
|
#ifdef __cplusplus |
03b01d2 |
Jamozed |
2022-03-06 17:10:01 |
9
|
extern "C" { |
03b01d2 |
Jamozed |
2022-03-06 17:10:01 |
10
|
#endif |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
11
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
12
|
#include <stdint.h> |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
13
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
14
|
/* Byte swap a 16-bit integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
15
|
#define BSWAP16(x) ( \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
16
|
(((uint16_t)(x) & 0xFF00u) >> 8) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
17
|
(((uint16_t)(x) & 0x00FFu) << 8)) |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
18
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
19
|
/* Byte swap a 32-bit integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
20
|
#define BSWAP32(x) ( \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
21
|
(((uint32_t)(x) & 0xFF000000uL) >> 24) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
22
|
(((uint32_t)(x) & 0x00FF0000uL) >> 8) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
23
|
(((uint32_t)(x) & 0x0000FF00uL) << 8) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
24
|
(((uint32_t)(x) & 0x000000FFuL) << 24)) |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
25
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
26
|
/* Byte swap a 64-bit integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
27
|
#define BSWAP64(x) ( \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
28
|
(((uint64_t)(x) & 0xFF00000000000000uLL) >> 56) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
29
|
(((uint64_t)(x) & 0x00FF000000000000uLL) >> 40) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
30
|
(((uint64_t)(x) & 0x0000FF0000000000uLL) >> 24) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
31
|
(((uint64_t)(x) & 0x000000FF00000000uLL) >> 8) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
32
|
(((uint64_t)(x) & 0x00000000FF000000uLL) << 8) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
33
|
(((uint64_t)(x) & 0x0000000000FF0000uLL) << 24) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
34
|
(((uint64_t)(x) & 0x000000000000FF00uLL) << 40) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
35
|
(((uint64_t)(x) & 0x00000000000000FFuLL) << 56)) |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
36
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
37
|
/* Load a 16-bit little endian integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
38
|
#define LD16LE(p) ( \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
39
|
((uint16_t)(((uint8_t *)(p))[0]) << 0) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
40
|
((uint16_t)(((uint8_t *)(p))[1]) << 8)) |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
41
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
42
|
/* Load a 16-bit big endian integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
43
|
#define LD16BE(p) ( \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
44
|
((uint16_t)(((uint8_t *)(p))[0]) << 8) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
45
|
((uint16_t)(((uint8_t *)(p))[1]) << 0)) |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
46
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
47
|
/* Load a 32-bit little endian integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
48
|
#define LD32LE(p) ( \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
49
|
((uint32_t)(((uint8_t *)(p))[0]) << 0) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
50
|
((uint32_t)(((uint8_t *)(p))[1]) << 8) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
51
|
((uint32_t)(((uint8_t *)(p))[2]) << 16) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
52
|
((uint32_t)(((uint8_t *)(p))[3]) << 24)) |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
53
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
54
|
/* Load a 32-bit big endian integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
55
|
#define LD32BE(p) ( \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
56
|
((uint32_t)(((uint8_t *)(p))[0]) << 24) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
57
|
((uint32_t)(((uint8_t *)(p))[1]) << 16) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
58
|
((uint32_t)(((uint8_t *)(p))[2]) << 8) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
59
|
((uint32_t)(((uint8_t *)(p))[3]) << 0)) |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
60
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
61
|
/* Load a 64-bit little endian integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
62
|
#define LD64LE(p) ( \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
63
|
((uint64_t)(((uint8_t *)(p))[0]) << 0) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
64
|
((uint64_t)(((uint8_t *)(p))[1]) << 8) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
65
|
((uint64_t)(((uint8_t *)(p))[2]) << 16) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
66
|
((uint64_t)(((uint8_t *)(p))[3]) << 24) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
67
|
((uint64_t)(((uint8_t *)(p))[4]) << 32) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
68
|
((uint64_t)(((uint8_t *)(p))[5]) << 40) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
69
|
((uint64_t)(((uint8_t *)(p))[6]) << 48) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
70
|
((uint64_t)(((uint8_t *)(p))[7]) << 56)) |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
71
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
72
|
/* Load a 64-bit big endian integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
73
|
#define LD64BE(p) ( \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
74
|
((uint64_t)(((uint8_t *)(p))[0]) << 56) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
75
|
((uint64_t)(((uint8_t *)(p))[1]) << 48) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
76
|
((uint64_t)(((uint8_t *)(p))[2]) << 40) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
77
|
((uint64_t)(((uint8_t *)(p))[3]) << 32) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
78
|
((uint64_t)(((uint8_t *)(p))[4]) << 24) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
79
|
((uint64_t)(((uint8_t *)(p))[5]) << 16) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
80
|
((uint64_t)(((uint8_t *)(p))[6]) << 8) | \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
81
|
((uint64_t)(((uint8_t *)(p))[7]) << 0)) |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
82
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
83
|
/* Store a 16-bit little endian integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
84
|
#define ST16LE(p, x) \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
85
|
((uint8_t *)(p))[0] = ((uint16_t)(x) >> 0) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
86
|
((uint8_t *)(p))[1] = ((uint16_t)(x) >> 8) & 0xFFu |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
87
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
88
|
/* Store a 16-bit big endian integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
89
|
#define ST16BE(p, x) \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
90
|
((uint8_t *)(p))[0] = ((uint16_t)(x) >> 8) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
91
|
((uint8_t *)(p))[1] = ((uint16_t)(x) >> 0) & 0xFFu |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
92
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
93
|
/* Store a 32-bit little endian integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
94
|
#define ST32LE(p, x) \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
95
|
((uint8_t *)(p))[0] = ((uint32_t)(x) >> 0) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
96
|
((uint8_t *)(p))[1] = ((uint32_t)(x) >> 8) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
97
|
((uint8_t *)(p))[2] = ((uint32_t)(x) >> 16) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
98
|
((uint8_t *)(p))[3] = ((uint32_t)(x) >> 24) & 0xFFu |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
99
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
100
|
/* Store a 32-bit big endian integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
101
|
#define ST32BE(p, x) \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
102
|
((uint8_t *)(p))[0] = ((uint32_t)(x) >> 24) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
103
|
((uint8_t *)(p))[1] = ((uint32_t)(x) >> 16) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
104
|
((uint8_t *)(p))[2] = ((uint32_t)(x) >> 8) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
105
|
((uint8_t *)(p))[3] = ((uint32_t)(x) >> 0) & 0xFFu |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
106
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
107
|
/* Store a 64-bit little endian integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
108
|
#define ST64LE(p, x) \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
109
|
((uint8_t *)(p))[0] = ((uint64_t)(x) >> 0) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
110
|
((uint8_t *)(p))[1] = ((uint64_t)(x) >> 8) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
111
|
((uint8_t *)(p))[2] = ((uint64_t)(x) >> 16) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
112
|
((uint8_t *)(p))[3] = ((uint64_t)(x) >> 24) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
113
|
((uint8_t *)(p))[4] = ((uint64_t)(x) >> 32) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
114
|
((uint8_t *)(p))[5] = ((uint64_t)(x) >> 40) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
115
|
((uint8_t *)(p))[6] = ((uint64_t)(x) >> 48) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
116
|
((uint8_t *)(p))[7] = ((uint64_t)(x) >> 56) & 0xFFu |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
117
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
118
|
/* Store a 64-bit big endian integer */ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
119
|
#define ST64BE(p, x) \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
120
|
((uint8_t *)(p))[0] = ((uint64_t)(x) >> 56) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
121
|
((uint8_t *)(p))[1] = ((uint64_t)(x) >> 48) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
122
|
((uint8_t *)(p))[2] = ((uint64_t)(x) >> 40) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
123
|
((uint8_t *)(p))[3] = ((uint64_t)(x) >> 32) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
124
|
((uint8_t *)(p))[4] = ((uint64_t)(x) >> 24) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
125
|
((uint8_t *)(p))[5] = ((uint64_t)(x) >> 16) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
126
|
((uint8_t *)(p))[6] = ((uint64_t)(x) >> 8) & 0xFFu; \ |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
127
|
((uint8_t *)(p))[7] = ((uint64_t)(x) >> 0) & 0xFFu |
03b01d2 |
Jamozed |
2022-03-06 17:10:01 |
128
|
|
03b01d2 |
Jamozed |
2022-03-06 17:10:01 |
129
|
#ifdef __cplusplus |
03b01d2 |
Jamozed |
2022-03-06 17:10:01 |
130
|
} // extern "C" |
03b01d2 |
Jamozed |
2022-03-06 17:10:01 |
131
|
#endif |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
132
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
133
|
#endif // UTIL_ENDIAN_H_G7AID2RQ |
|
|
|
134
|
|