-
Notifications
You must be signed in to change notification settings - Fork 1
/
ECL_common.c
356 lines (325 loc) · 13.7 KB
/
ECL_common.c
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
/*
* Copyright 2017 - 2018 Evgeniy Evstratov
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "ECL_JH_States.h"
#include "ECL_utils.h"
static uint8_t ECL_JH_dummy_buffer; /* dummy data storage to simplify some checks */
static const uint8_t c_bmasks8[] = {0, 1, 3, 7, 15, 31, 63, 127, 255};
/* utils part */
uint32_t ECL_GetSizeBitness(void) {
return ECL_SIZE_TYPE_BITS_COUNT;
}
uint32_t ECL_GetVersionNumber(void) {
return ECL_VERSION_NUMBER;
}
#define ECL_STRING_OF_HELPER(x) #x
#define ECL_STRING_OF(x) ECL_STRING_OF_HELPER(x)
const char* ECL_GetVersionString(void) {
return ECL_STRING_OF(ECL_VERSION_MAJOR.ECL_VERSION_MINOR.ECL_VERSION_PATCH);
}
#undef ECL_STRING_OF_HELPER
#undef ECL_STRING_OF
const char* ECL_GetVersionBranch(void) {
return ECL_VERSION_BRANCH;
}
uint8_t ECL_LogRoundUp(ECL_usize value) {
ECL_usize tmp;
uint8_t result;
if(value < 2) {
return 1;
}
result = sizeof(value) * 8;
tmp = 1;
tmp <<= result - 1;
while(value <= tmp) {
tmp >>= 1;
--result;
}
return result;
}
uint16_t* ECL_GetAlignedPointer2(uint8_t* ptr) {
ECL_ASSERT(ptr);
return (uint16_t*) ( (((uintptr_t)ptr) & 1) ? (ptr + 1) : ptr);
}
ECL_usize* ECL_GetAlignedPointerS(uint8_t* ptr) {
ECL_SCOPED_CONST int offset = ((uintptr_t)ptr) & (sizeof(ECL_usize) - 1);
ECL_ASSERT(ptr);
return (ECL_usize*)(offset ? (ptr + sizeof(ECL_usize) - offset) : ptr);
}
uint8_t* ECL_Helper_WriteE7(uint8_t* data_start, ECL_usize max_bytes, ECL_usize value) {
if(data_start) {
while(max_bytes) {
ECL_SCOPED_CONST uint8_t e = value > 0x7F ? 0x80 : 0;
*data_start = (uint8_t)value | e;
value >>= 7;
++data_start;
if(! e) {
return data_start;
}
--max_bytes;
}
}
return 0;
}
const uint8_t* ECL_Helper_ReadE7(const uint8_t* data_start, ECL_usize max_bytes, ECL_usize* output_value) {
if(data_start && output_value) {
uint8_t shift = 0;
*output_value = 0;
while(max_bytes) {
ECL_SCOPED_CONST ECL_usize adding = *data_start & 0x7F;
ECL_SCOPED_CONST uint8_t e = *data_start & 0x80;
*output_value |= adding << shift;
++data_start;
if(! e) {
if(shift) {
ECL_SCOPED_CONST uint8_t last_allowed_bits = ECL_SIZE_TYPE_BITS_COUNT - shift;
if(( ((ECL_usize)1) << last_allowed_bits ) <= adding) {
break; /* failed - not enough capacity */
}
}
return data_start;
}
--max_bytes;
shift += 7;
if(shift >= ECL_SIZE_TYPE_BITS_COUNT) {
break; /* failed - not enough capacity */
}
}
}
return 0;
}
/* JH part */
void ECL_JH_WInit(ECL_JH_WState* state, uint8_t* ptr, ECL_usize size, ECL_usize start) {
uint8_t input_is_valid = 1;
if((! ptr) || (! size)) {
input_is_valid = 0;
ptr = &ECL_JH_dummy_buffer;
size = 1;
}
state->is_valid = input_is_valid * (start < size ? 1 : 0);
state->end = ptr + size;
if(state->is_valid) {
state->byte = ptr + start;
state->next = state->byte + 1;
} else {
state->byte = ptr;
state->next = state->end; /* if invalid - next==end */
}
state->n_bits = 8;
*(state->byte) = 0;
}
void ECL_JH_RInit(ECL_JH_RState* state, const uint8_t* ptr, ECL_usize size, ECL_usize start) {
uint8_t input_is_valid = 1;
if((! ptr) || (! size)) {
input_is_valid = 0;
ptr = &ECL_JH_dummy_buffer;
size = 1;
}
state->is_valid = input_is_valid * (start < size ? 1 : 0);
state->end = ptr + size;
if(state->is_valid) {
state->byte = ptr + start;
state->next = state->byte + 1;
} else {
state->byte = ptr;
state->next = state->end;
}
state->n_bits = 8;
}
void ECL_JH_Write(ECL_JH_WState* state, uint8_t value, uint8_t bits) {
ECL_ASSERT(bits && (bits < 9));
value &= c_bmasks8[bits];
if(bits <= state->n_bits) { /* fits easily */
*(state->byte) |= value << (8 - state->n_bits);
state->n_bits -= bits;
} else if(! state->n_bits) { /* to next */
if(state->next == state->end) {
state->is_valid = 0;
return;
}
state->byte = state->next;
++(state->next);
*(state->byte) = value;
state->n_bits = 8 - bits;
} else { /* 2 parts */
*(state->byte) |= value << (8 - state->n_bits);
if(state->next == state->end) {
state->is_valid = 0;
return;
}
state->byte = state->next;
++(state->next);
*(state->byte) = value >> state->n_bits;
state->n_bits = 8 - bits + state->n_bits;
}
}
uint8_t ECL_JH_Read(ECL_JH_RState* state, uint8_t bits) {
uint8_t res;
ECL_ASSERT(bits && (bits < 9));
if(bits <= state->n_bits) { /* fits easily */
res = *(state->byte) >> (8 - state->n_bits);
state->n_bits -= bits;
} else if(! state->n_bits) { /* to next */
if(state->next == state->end) {
state->is_valid = 0;
return 0;
}
state->byte = state->next;
++(state->next);
res = *(state->byte);
state->n_bits = 8 - bits;
} else { /* 2 parts */
res = *(state->byte) >> (8 - state->n_bits);
if(state->next == state->end) {
state->is_valid = 0;
return 0;
}
state->byte = state->next;
++(state->next);
res |= *(state->byte) << state->n_bits;
state->n_bits = 8 - bits + state->n_bits;
}
return res & c_bmasks8[bits];
}
void ECL_JH_WJump(ECL_JH_WState* state, ECL_usize distance) {
if((state->next + distance) <= state->end) {
state->next += distance;
} else {
state->next = state->end;
state->is_valid = 0;
}
}
void ECL_JH_RJump(ECL_JH_RState* state, ECL_usize distance) {
if((state->next + distance) <= state->end) {
state->next += distance;
} else {
state->next = state->end;
state->is_valid = 0;
}
}
#ifdef ECL_USE_BRANCHLESS
#define ECL_CALC_E(value, n_bits) \
(((ECL_usize)((1 << (n_bits)) - 1) - (value)) >> (ECL_SIZE_TYPE_BITS_COUNT - 1 - (n_bits))) & (1 << (n_bits));
#else
#define ECL_CALC_E(value, n_bits) \
((value) > ((ECL_usize)(1 << (n_bits)) - 1) ? (1 << (n_bits)) : 0)
#endif
/*
this should have been done in C++ templates, but unfortunately embedded rather use C.
0 < num < 8.
*/
#define ECL_E_NUMBER_DEFINE_SIMPLE_IMPL(num) \
void ECL_JH_Write_E##num(ECL_JH_WState* state, ECL_usize value) { \
do { \
ECL_SCOPED_CONST uint8_t e = ECL_CALC_E(value, num); \
ECL_JH_Write(state, (uint8_t)value | e, num + 1); \
value >>= num; \
} while(value); \
} \
ECL_usize ECL_JH_Read_E##num(ECL_JH_RState* state) { \
ECL_usize result; \
uint8_t shift; \
shift = 0; \
result = 0; \
do { \
ECL_SCOPED_CONST uint8_t code = ECL_JH_Read(state, (num + 1)); \
result |= ((ECL_usize)(code & ((1 << num) - 1))) << shift; \
if(! (code & (1 << num))) { \
break; \
} \
shift += num; \
} while(shift < ECL_SIZE_TYPE_BITS_COUNT); \
return result; \
} \
uint8_t ECL_Evaluate_E##num(ECL_usize number) { \
uint8_t result = 0; \
do { \
number >>= num; \
result += num + 1; \
} while(number); \
return result; \
}
ECL_E_NUMBER_DEFINE_SIMPLE_IMPL(2)
ECL_E_NUMBER_DEFINE_SIMPLE_IMPL(3)
ECL_E_NUMBER_DEFINE_SIMPLE_IMPL(4)
ECL_E_NUMBER_DEFINE_SIMPLE_IMPL(5)
ECL_E_NUMBER_DEFINE_SIMPLE_IMPL(6)
ECL_E_NUMBER_DEFINE_SIMPLE_IMPL(7)
#undef ECL_E_NUMBER_DEFINE_SIMPLE_IMPL
/*
0 < num1 < 8.
0 < num2 < 8.
*/
#define ECL_E_NUMBER_DEFINE_X2_IMPL(num1, num2) \
void ECL_JH_Write_E##num1##E##num2(ECL_JH_WState* state, ECL_usize value) { \
uint8_t e = ECL_CALC_E(value, num1); \
ECL_JH_Write(state, (uint8_t)value | e, num1 + 1); \
value >>= num1; \
while(value) { \
e = ECL_CALC_E(value, num2); \
ECL_JH_Write(state, (uint8_t)value | e, num2 + 1); \
value >>= num2; \
} \
} \
ECL_usize ECL_JH_Read_E##num1##E##num2(ECL_JH_RState* state) { \
ECL_usize result; \
uint8_t code, shift; \
code = ECL_JH_Read(state, num1 + 1); \
result = code & ((1 << num1) - 1); \
if(code & (1 << num1)) { \
shift = num1; \
do { \
code = ECL_JH_Read(state, num2 + 1); \
result |= ((ECL_usize)(code & ((1 << num2) - 1))) << shift; \
if(! (code & (1 << num2))) { \
break; \
} \
shift += num2; \
} while(shift < ECL_SIZE_TYPE_BITS_COUNT); \
} \
return result; \
} \
uint8_t ECL_Evaluate_E##num1##E##num2(ECL_usize number) { \
uint8_t result = num1 + 1; \
number >>= num1; \
while(number) { \
number >>= num2; \
result += num2 + 1; \
} \
return result; \
}
ECL_E_NUMBER_DEFINE_X2_IMPL(4, 5)
ECL_E_NUMBER_DEFINE_X2_IMPL(5, 2)
ECL_E_NUMBER_DEFINE_X2_IMPL(5, 3)
ECL_E_NUMBER_DEFINE_X2_IMPL(5, 4)
ECL_E_NUMBER_DEFINE_X2_IMPL(6, 2)
ECL_E_NUMBER_DEFINE_X2_IMPL(6, 3)
ECL_E_NUMBER_DEFINE_X2_IMPL(6, 4)
ECL_E_NUMBER_DEFINE_X2_IMPL(7, 2)
ECL_E_NUMBER_DEFINE_X2_IMPL(7, 3)
ECL_E_NUMBER_DEFINE_X2_IMPL(7, 4)
#undef ECL_E_NUMBER_DEFINE_X2_IMPL
/* cleanup in case of compiling as single file */
#undef ECL_CALC_E
#undef ECL_CHECK_E_AND_SHIFT