-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary.h
368 lines (273 loc) · 7.83 KB
/
binary.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
/*----------------------------------------------------------------------------*\
| PART OF THE CIRCUIT REWIND SOURCE CODE LIBRARY COLLECTION. |
| SOURCE: https://github.com/circuitrewind/binary |
| LICENSE: https://github.com/circuitrewind/binary/blob/main/LICENSE |
+------------------------------------------------------------------------------+
| SIMPLE STRUCTS/UNIONS FOR SWAPPING BETWEEN BIG AND LITTLE ENDIAN. |
| ALL SWAPPING IS DONE IN-PLACE, SO NO ADDITIONAL RAM IS NEEDED. |
| SWAPPING IS DONE VIA 3 INLINED XOR COMMANDS, KEEPING THE CODE FOOTPRINT VERY |
| SMALL AS WELL. |
+------------------------------------------------------------------------------+
| ADDITIONALLY, BIT FIELD, BYTE LISTS, AND MORE HAVE BEEN ADDED TO THESE. |
| IF YOU NEED A SIMPLIFIED WAY TO ACCESS ARBITRARY BITS FROM A BYTE OR |
| MULTI-BYTE DATA, THIS WILL BE ONE OF THE EASIEST WAYS TO DO IT. |
\*----------------------------------------------------------------------------*/
#ifndef __binary_h__
#define __binary_h__
#include "inline.h"
////////////////////////////////////////////////////////////////////////////////
// BITFIELD - 8-BIT
////////////////////////////////////////////////////////////////////////////////
union PACKED uint8_b {
INLINE uint8_b() { this->byte_0 = 0; }
INLINE uint8_b(uint8_t byte) { this->byte_0 = byte; }
INLINE uint8_b(uint8_t *byte) { this->byte_0 = byte[0]; }
uint8_t byte[1];
uint8_t byte_0;
struct PACKED {
union PACKED {
unsigned int nibble_0 : 4;
struct PACKED {
unsigned int bit_0 : 1;
unsigned int bit_1 : 1;
unsigned int bit_2 : 1;
unsigned int bit_3 : 1;
};
};
union PACKED {
unsigned int nibble_1 : 4;
struct PACKED {
unsigned int bit_4 : 1;
unsigned int bit_5 : 1;
unsigned int bit_6 : 1;
unsigned int bit_7 : 1;
};
};
};
INLINE operator uint8_t() const {
return this->byte_0;
}
INLINE uint8_t operator =(const uint8_t byte) {
return this->byte_0 = byte;
}
INLINE void in(uint8_t bit) {
this->byte_0 = (this->byte_0 << 1) | !!bit;
}
INLINE uint8_t out() {
uint8_t out = this->byte_0 & 0x01;
this->byte_0 = this->byte_0 >> 1;
return out;
}
INLINE uint8_t shift(uint8_t offset) {
return (this->byte_0 >> ((uint8_t)offset)) & 0x01;
}
INLINE void shift(uint8_t offset, bool value) {
uint8_t tmp = ((uint8_t)1)<<((uint8_t)offset);
value ? (this->byte_0 |= tmp)
: (this->byte_0 &= ~tmp);
}
static INLINE uint8_b fill() { return 0xff; }
};
////////////////////////////////////////////////////////////////////////////////
// BITFIELD - 16-BIT
////////////////////////////////////////////////////////////////////////////////
union PACKED uint16_b {
INLINE uint16_b() { this->word_0 = 0; }
INLINE uint16_b(uint16_t word) { this->word_0 = word; }
INLINE uint16_b(uint16_t *word) { this->word_0 = word[0]; }
INLINE uint16_b(uint8_t *byte) {
this->byte_0 = byte[0];
this->byte_1 = byte[1];
}
INLINE uint16_b(uint8_t byte_0, uint8_t byte_1) {
this->byte_0 = byte_0;
this->byte_1 = byte_1;
}
uint8_t byte[2];
uint16_t word[1];
uint16_t word_0;
struct PACKED {
union PACKED {
uint8_t byte_0;
struct PACKED {
unsigned int bit_0 : 1;
unsigned int bit_1 : 1;
unsigned int bit_2 : 1;
unsigned int bit_3 : 1;
unsigned int bit_4 : 1;
unsigned int bit_5 : 1;
unsigned int bit_6 : 1;
unsigned int bit_7 : 1;
};
};
union PACKED {
uint8_t byte_1;
struct PACKED {
unsigned int bit_8 : 1;
unsigned int bit_9 : 1;
unsigned int bit_10 : 1;
unsigned int bit_11 : 1;
unsigned int bit_12 : 1;
unsigned int bit_13 : 1;
unsigned int bit_14 : 1;
unsigned int bit_15 : 1;
};
};
};
INLINE operator uint16_t() const {
return this->word_0;
}
INLINE uint16_t operator =(uint16_t word) {
return this->word_0 = word;
}
INLINE void in(uint8_t bit) {
this->word_0 = (this->word_0 << 1) | !!bit;
}
INLINE uint8_t out() {
uint8_t out = this->word_0 & 0x01;
this->word_0 = this->word_0 >> 1;
return out;
}
INLINE uint8_t shift(uint8_t offset) {
return (this->word_0 >> ((uint16_t)offset)) & 0x01;
}
INLINE void shift(uint8_t offset, bool value) {
uint16_t tmp = ((uint16_t)1)<<((uint16_t)offset);
value ? (this->word_0 |= tmp)
: (this->word_0 &= ~tmp);
}
INLINE void swap() {
byte_0 ^= byte_1;
byte_1 ^= byte_0;
byte_0 ^= byte_1;
}
static INLINE uint16_b fill() { return 0xffff; }
};
////////////////////////////////////////////////////////////////////////////////
// BITFIELD - 32-BIT
////////////////////////////////////////////////////////////////////////////////
union PACKED uint32_b {
uint8_t byte[4];
uint16_t word[2];
uint32_t dword[1];
uint32_t dword_0;
INLINE uint32_b() { this->dword_0 = 0; }
INLINE uint32_b(uint32_t dword) { this->dword_0 = dword; }
INLINE uint32_b(uint32_t *dword) { this->dword_0 = dword[0]; }
INLINE uint32_b(uint8_t *byte) {
this->byte_0 = byte[0];
this->byte_1 = byte[1];
this->byte_2 = byte[2];
this->byte_3 = byte[3];
}
INLINE uint32_b(uint16_t *word) {
this->word_0 = word[0];
this->word_1 = word[1];
}
INLINE uint32_b(uint8_t word_0, uint8_t word_1) {
this->word_0 = word_0;
this->word_1 = word_1;
}
INLINE uint32_b(uint8_t byte_0, uint8_t byte_1,
uint8_t byte_2, uint8_t byte_3) {
this->byte_0 = byte_0;
this->byte_1 = byte_1;
this->byte_2 = byte_2;
this->byte_3 = byte_3;
}
struct PACKED {
union PACKED {
uint16_t word_0;
struct PACKED {
union PACKED {
uint8_t byte_0;
struct PACKED {
unsigned int bit_0 : 1;
unsigned int bit_1 : 1;
unsigned int bit_2 : 1;
unsigned int bit_3 : 1;
unsigned int bit_4 : 1;
unsigned int bit_5 : 1;
unsigned int bit_6 : 1;
unsigned int bit_7 : 1;
};
};
union PACKED {
uint8_t byte_1;
struct PACKED {
unsigned int bit_8 : 1;
unsigned int bit_9 : 1;
unsigned int bit_10 : 1;
unsigned int bit_11 : 1;
unsigned int bit_12 : 1;
unsigned int bit_13 : 1;
unsigned int bit_14 : 1;
unsigned int bit_15 : 1;
};
};
};
};
union PACKED {
uint16_t word_1;
struct PACKED {
union PACKED {
uint8_t byte_2;
struct PACKED {
unsigned int bit_16 : 1;
unsigned int bit_17 : 1;
unsigned int bit_18 : 1;
unsigned int bit_19 : 1;
unsigned int bit_20 : 1;
unsigned int bit_21 : 1;
unsigned int bit_22 : 1;
unsigned int bit_23 : 1;
};
};
union PACKED {
uint8_t byte_3;
struct PACKED {
unsigned int bit_24 : 1;
unsigned int bit_25 : 1;
unsigned int bit_26 : 1;
unsigned int bit_27 : 1;
unsigned int bit_28 : 1;
unsigned int bit_29 : 1;
unsigned int bit_30 : 1;
unsigned int bit_31 : 1;
};
};
};
};
};
INLINE operator uint32_t() const {
return this->dword_0;
}
INLINE uint32_t operator =(uint32_t dword) {
return this->dword_0 = dword;
}
INLINE void in(uint8_t bit) {
this->dword_0 = (this->dword_0 << 1L) | !!bit;
}
INLINE uint8_t out() {
uint8_t out = this->dword_0 & 0x01;
this->dword_0 = this->dword_0 >> 1L;
return out;
}
INLINE uint8_t shift(uint8_t offset) {
return (this->dword_0 >> ((uint32_t)offset)) & 0x01;
}
INLINE void shift(uint8_t offset, bool value) {
value ? (this->dword_0 |= ((uint32_t)1)<<((uint32_t)offset))
: (this->dword_0 &= ~((uint32_t)1)<<((uint32_t)offset));
}
INLINE void swap() {
byte_0 ^= byte_3;
byte_3 ^= byte_0;
byte_0 ^= byte_3;
byte_1 ^= byte_2;
byte_2 ^= byte_1;
byte_1 ^= byte_2;
}
static INLINE uint32_b fill() { return 0xffffffffL; }
};
#endif //__binary_h__