-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhgl_rle.h
380 lines (338 loc) · 11.5 KB
/
hgl_rle.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
369
370
371
372
373
374
375
376
377
378
379
/**
* LICENSE:
*
* MIT License
*
* Copyright (c) 2024 Henrik A. Glass
*
* 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.
* MIT License
*
*
* ABOUT:
*
* hgl_rle.h implements run-length encoding and decoding with multiple possible
* run-length and data block sizes.
*
*
* USAGE:
*
* Include hgl_rle.h file like this:
*
* #define HGL_RLE_IMPLEMENTATION
* #include "hgl_rle.h"
*
* HGL_RLE_IMPLEMENTATION must only be defined once, in a single compilation unit.
*
*
* EXAMPLE:
*
* ssize_t size;
* uint32_t *data = obtain_some_data(&size);
* uint8_t *encoded_data;
*
* // figure out the encoded size of `data`
* ssize_t encoded_size = hgl_rle_encode32_8(NULL, data, size);
*
* // allocate memory for encoded data
* encoded_data = malloc(encoded_size);
*
* // encode data and place into `encoded_data`.
* hgl_rle_encode32_8(encoded_data, data, size);
*
*
* AUTHOR: Henrik A. Glass
*
*/
#ifndef HGL_RLE_H
#define HGL_RLE_H
#include <sys/types.h>
#include <stdint.h>
/**
* Run-length encodes `size` bytes of data at `src` and places it into `dst`. The
* encoding is performed on byte-sized (8 bit) elements. Returns the size in bytes
* of the run-length encoded data. If `dst` is NULL, then only the encoded size is
* calculcated and returned.
*
* Each run-length encoded segment of data is stored as a pair of two 8 bit values,
* a run-length `RL` and the data `DA`. The run-length of the segment is stored on the
* lower address. The data is stored on the higher address. Note that a run-length
* value of N indicates an actual run length of N+1. I.e. if `RL`=5, then `DA` should
* be repeated 6 times.
*
* RL/DA pair layout:
*
* |_RL_|_DA_|
* address offset: 0 1 2
*
*/
ssize_t hgl_rle_encode8_8(uint8_t *restrict dst, const uint8_t *restrict src, size_t size);
/**
* Decodes `size` bytes of run-length encoded data at `src` and places it into `dst`.
* Returns the size in bytes of the decoded data. If `dst` is NULL, then only the decoded
* size is calculcated and returned.
*
* Note: this function assumes that `src` contains encoded data formatted in the way
* described in `hgl_rle_encode8_8`.
*/
ssize_t hgl_rle_decode8_8(uint8_t *restrict dst, const uint8_t *restrict src, size_t size);
/**
* Run-length encodes `size` bytes of data at `src` and places it into `dst`. The
* encoding is performed on 32 bit-sized elements. Returns the size in bytes of the
* run-length encoded data. If `dst` is NULL, then only the encoded size is
* calculcated and returned.
*
* Each run-length encoded segment of data is stored as a pair of two 32 bit values,
* a run-length `RL` and the data `DA`. The run-length of the segment is stored on the
* lower address. The data is stored on the higher address. Note that a run-length
* value of N indicates an actual run length of N+1. I.e. if `RL`=5, then `DA` should
* be repeated 6 times.
*
* Note: Both `RL` and `DA` are stored in the byte-order native to the machine running
* the program. Hence, if data is to be decoded on a different machine, care should be
* taken by the programmer to ensure correct byte ordering.
*
* RL/DA pair layout:
*
* |_____RL_____|______DA_____|
* address offset: 0 4 8
*
*/
ssize_t hgl_rle_encode32_32(uint32_t *restrict dst, const uint32_t *restrict src, size_t size);
/**
* Decodes `size` bytes of run-length encoded data at `src` and places it into `dst`.
* Returns the size in bytes of the decoded data. If `dst` is NULL, then only the decoded
* size is calculcated and returned.
*
* Note: this function assumes that `src` contains encoded data formatted in the way
* described in `hgl_rle_encode32_32`.
*/
ssize_t hgl_rle_decode32_32(uint32_t *restrict dst, const uint32_t *restrict src, size_t size);
/**
* Run-length encodes `size` bytes of data at `src` and places it into `dst`. The
* encoding is performed on 32 bit-sized elements. Returns the size in bytes of the
* run-length encoded data. If `dst` is NULL, then only the encoded size is
* calculcated and returned.
*
* Each run-length encoded segment of data is stored as a pair of two values, consisting
* of an 8-bit run-length value `RL` and a 32-bit data value `DA`. The run-length of
* the segment is stored on the lower address. The data is stored on the higher address.
* Note that a run-length value of N indicates an actual run length of N+1. I.e. if
* `RL`=5, then `DA` should be repeated 6 times.
*
* Note: `DA` is stored in big endian byte-order.
*
* RL/DA pair layout:
*
* |_RL_|______DA_____|
* address offset: 0 1 5
*
*/
ssize_t hgl_rle_encode32_8(uint8_t *restrict dst, const uint32_t *restrict src, size_t size);
/**
* Decodes `size` bytes of run-length encoded data at `src` and places it into `dst`.
* Returns the size in bytes of the decoded data. If `dst` is NULL, then only the decoded
* size is calculcated and returned.
*
* Note: this function assumes that `src` contains encoded data formatted in the way
* described in `hgl_rle_encode32_8`.
*/
ssize_t hgl_rle_decode32_8(uint32_t *restrict dst, const uint8_t *restrict src, size_t size);
#endif /* HGL_RLE_H */
#ifdef HGL_RLE_IMPLEMENTATION
#include <stdio.h> // DEBUG
#include <assert.h> // DEBUG
#include <string.h>
#define MEMSET32(dst, val, n) \
do { \
for (size_t i = 0; i < (n); i++) { \
(dst)[i] = (val); \
} \
} while (0)
static inline void memset32_(void *dst, uint32_t val, size_t n)
{
//uint32_t *dst32 = (uint32_t *) dst;
//uint32_t c = (uint32_t) n;
//(void) val;
//for (size_t i = 0; i < n; i++) {
// dst32[i] = val;
//}
__asm__ __volatile__ (
"mov %1, %%eax\n\t"
"movq %0, %%rcx\n\t"
"movq %2, %%rdi\n\t"
"cld\n\t"
"rep\n\t"
"stosl"
:
: "r" (n), "r" (val), "r" (dst)
: "ecx", "eax", "rdi"
);
}
ssize_t hgl_rle_encode8_8(uint8_t *restrict dst, const uint8_t *restrict src, size_t size)
{
size_t write_idx = 0;
uint8_t rl = 1;
uint8_t da = src[0];
for (size_t i = 1; i < size; i++) {
uint8_t next = src[i];
if ((rl == 0) || (next != da)) {
if (dst != NULL) {
dst[write_idx] = rl - 1;
dst[write_idx + 1] = da;
}
write_idx += 2;
da = next;
rl = 1;
} else {
rl++;
}
}
if (dst != NULL) {
dst[write_idx] = rl - 1;
dst[write_idx + 1] = da;
}
write_idx += 2;
assert(write_idx % 2 == 0);
return write_idx;
}
ssize_t hgl_rle_decode8_8(uint8_t *restrict dst, const uint8_t *restrict src, size_t size)
{
if((size & 1) != 0) {
fprintf(stderr, "[hgl_rle_decode8_8] Error: `size` must be divisible by 2");
return -1;
}
size_t write_idx = 0;
for (size_t i = 0; i < size; i += 2) {
size_t rl = ((size_t)src[i]) + 1;
uint8_t da = src[i + 1];
if (dst != NULL) {
memset(&dst[write_idx], da, rl);
}
write_idx += rl;
}
return write_idx;
}
ssize_t hgl_rle_encode32_32(uint32_t *restrict dst, const uint32_t *restrict src, size_t size)
{
size_t write_idx = 0;
uint32_t rl = 1;
uint32_t da = src[0];
if((size & 3) != 0) {
fprintf(stderr, "[hgl_rle_encode32_32] Error: `size` must be divisible by 4");
return -1;
}
for (size_t i = 1; i < size / sizeof(uint32_t); i++) {
uint32_t next = src[i];
if ((rl == 0) || (next != da)) {
if (dst != NULL) {
dst[write_idx] = rl - 1;
dst[write_idx + 1] = da;
}
write_idx += 2;
da = next;
rl = 1;
} else {
rl++;
}
}
if (dst != NULL) {
dst[write_idx] = rl - 1;
dst[write_idx + 1] = da;
}
write_idx += 2;
assert(write_idx % 2 == 0);
return write_idx * sizeof(uint32_t);
}
ssize_t hgl_rle_decode32_32(uint32_t *restrict dst, const uint32_t *restrict src, size_t size)
{
if((size & 7) != 0) {
fprintf(stderr, "[hgl_rle_decode32_32] Error: `size` must be divisible by 8");
return -1;
}
size_t write_idx = 0;
for (size_t i = 0; i < size / sizeof(uint32_t); i += 2) {
size_t rl = ((size_t)src[i]) + 1;
uint32_t da = src[i + 1];
if (dst != NULL) {
memset32_(&dst[write_idx], da, rl);
}
write_idx += rl;
}
return write_idx * sizeof(uint32_t);
}
ssize_t hgl_rle_encode32_8(uint8_t *restrict dst, const uint32_t *restrict src, size_t size)
{
size_t write_idx = 0;
uint8_t rl = 1;
uint32_t da = src[0];
if((size & 3) != 0) {
fprintf(stderr, "[hgl_rle_encode32_8] Error: `size` must be divisible by 4");
return -1;
}
for (size_t i = 1; i < size / sizeof(uint32_t); i++) {
uint32_t next = src[i];
if ((rl == 0) || (next != da)) {
if (dst != NULL) {
dst[write_idx] = rl - 1;
dst[write_idx + 1] = (da >> 24) & 0xFF;
dst[write_idx + 2] = (da >> 16) & 0xFF;
dst[write_idx + 3] = (da >> 8) & 0xFF;
dst[write_idx + 4] = (da >> 0) & 0xFF;
}
write_idx += 5;
da = next;
rl = 1;
} else {
rl++;
}
}
if (dst != NULL) {
dst[write_idx] = rl - 1;
dst[write_idx + 1] = (da >> 24) & 0xFF;
dst[write_idx + 2] = (da >> 16) & 0xFF;
dst[write_idx + 3] = (da >> 8) & 0xFF;
dst[write_idx + 4] = (da >> 0) & 0xFF;
}
write_idx += 5;
assert(write_idx % 5 == 0);
return write_idx;
}
ssize_t hgl_rle_decode32_8(uint32_t *restrict dst, const uint8_t *restrict src, size_t size)
{
if((size % 5) != 0) {
fprintf(stderr, "[hgl_rle_decode32_8] Error: `size` must be divisible by 5");
return -1;
}
size_t write_idx = 0;
for (size_t i = 0; i < size; i += 5) {
size_t rl = ((size_t)src[i]) + 1;
uint32_t da = 0;
da |= src[i + 1] << 24;
da |= src[i + 2] << 16;
da |= src[i + 3] << 8;
da |= src[i + 4] << 0;
if (dst != NULL) {
memset32_(&dst[write_idx], da, rl);
}
write_idx += rl;
}
return write_idx * sizeof(uint32_t);
}
#endif