-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlibx.c
366 lines (270 loc) · 7.09 KB
/
libx.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
357
358
359
360
361
362
363
364
365
366
#include <assert.h>
#include <stdint.h>
#include "libx.h"
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
enum { READ_MODE, WRITE_MODE };
/* I/O abstraction layer */
typedef struct {
uint32_t *ptr; /* pointer to memory */
void *end;
uint32_t b; /* bit buffer */
size_t c; /* bit counter */
} generic_io_t;
static struct context {
size_t freq[256]; /* char -> frequency */
uint8_t sorted[256]; /* index -> char */
uint8_t order[256]; /* char -> index */
} table[256];
static size_t opt_k;
static size_t symbol_sum, symbol_count; /* mean = symbol_sum / symbol_count */
/* Recompute Golomb-Rice codes after... */
#define RESET_INTERVAL 256
static void initiate(generic_io_t *io, void *ptr, void *end, int mode)
{
assert(io != NULL);
io->ptr = ptr;
io->end = end ? (char *) end - 3 : NULL;
if (mode == READ_MODE) {
io->c = 32;
} else {
io->b = 0;
io->c = 0;
}
}
static void flush_buffer(generic_io_t *io)
{
assert(io != NULL);
assert(io->ptr != NULL);
*(io->ptr++) = io->b;
io->b = 0;
io->c = 0;
}
static void reload_buffer(generic_io_t *io)
{
assert(io != NULL);
assert(io->ptr != NULL);
if ((void *) io->ptr < io->end)
io->b = *(io->ptr++);
else
io->b = 0x80000000;
io->c = 0;
}
static void put_nonzero_bit(generic_io_t *io)
{
assert(io != NULL);
assert(io->c < 32);
io->b |= (uint32_t) 1 << io->c;
io->c++;
if (io->c == 32)
flush_buffer(io);
}
/* Count trailing zeros */
static inline size_t ctzu32(uint32_t n)
{
if (n == 0)
return 32;
#ifdef __GNUC__
return __builtin_ctz((unsigned) n);
#endif
/* If we can not access hardware ctz, use branch-less algorithm
* http://graphics.stanford.edu/~seander/bithacks.html
*/
static const int lut[32] = {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20,
15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19,
16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
return lut[((uint32_t)((n & -n) * 0x077CB531U)) >> 27];
}
static void write_bits(generic_io_t *io, uint32_t b, size_t n)
{
assert(n <= 32);
for (int i = 0; i < 2; ++i) {
assert(io->c < 32);
size_t m = MIN(32 - io->c, n);
io->b |= (b & (((uint32_t) 1 << m) - 1)) << io->c;
io->c += m;
if (io->c == 32)
flush_buffer(io);
b >>= m;
n -= m;
if (n == 0)
return;
}
}
static void write_zero_bits(generic_io_t *io, size_t n)
{
assert(n <= 32);
for (size_t m; n > 0; n -= m) {
assert(io->c < 32);
m = MIN(32 - io->c, n);
io->c += m;
if (io->c == 32)
flush_buffer(io);
}
}
static uint32_t read_bits(generic_io_t *io, size_t n)
{
if (io->c == 32)
reload_buffer(io);
/* Get the available least-significant bits */
size_t s = MIN(32 - io->c, n);
uint32_t w = io->b & (((uint32_t) 1 << s) - 1);
io->b >>= s;
io->c += s;
n -= s;
/* Need more bits? If so, reload and get the most-significant bits */
if (n > 0) {
assert(io->c == 32);
reload_buffer(io);
w |= (io->b & (((uint32_t) 1 << n) - 1)) << s;
io->b >>= n;
io->c += n;
}
return w;
}
static void finalize(generic_io_t *io, int mode)
{
assert(io != NULL);
if (mode == WRITE_MODE && io->c > 0)
flush_buffer(io);
}
static void write_unary(generic_io_t *io, uint32_t N)
{
for (; N > 32; N -= 32)
write_zero_bits(io, 32);
write_zero_bits(io, N);
put_nonzero_bit(io);
}
static uint32_t read_unary(generic_io_t *io)
{
uint32_t total_zeros = 0;
assert(io != NULL);
do {
if (io->c == 32)
reload_buffer(io);
/* Get trailing zeros */
size_t s = MIN(32 - io->c, ctzu32(io->b));
io->b >>= s;
io->c += s;
total_zeros += s;
} while (io->c == 32);
/* ...and drop non-zero bit */
assert(io->c < 32);
io->b >>= 1;
io->c++;
return total_zeros;
}
/* Golomb-Rice, encode non-negative integer N, parameter M = 2^k */
static void write_golomb(generic_io_t *io, size_t k, uint32_t N)
{
assert(k < 32);
write_unary(io, N >> k);
write_bits(io, N, k);
}
static uint32_t read_golom(generic_io_t *io, size_t k)
{
uint32_t N = read_unary(io) << k;
N |= read_bits(io, k);
return N;
}
void x_init()
{
opt_k = 3;
symbol_sum = 0;
symbol_count = 0;
for (int p = 0; p < 256; ++p) {
for (int i = 0; i < 256; ++i) {
table[p].sorted[i] = i;
table[p].freq[i] = 0;
table[p].order[i] = i;
}
}
}
static void swap_symbols(struct context *ctx, uint8_t c, uint8_t d)
{
assert(ctx != NULL);
uint8_t ic = ctx->order[c], id = ctx->order[d];
assert(ctx->sorted[ic] == c);
assert(ctx->sorted[id] == d);
ctx->sorted[ic] = d;
ctx->sorted[id] = c;
ctx->order[c] = id;
ctx->order[d] = ic;
}
static void increment_frequency(struct context *ctx, uint8_t c)
{
assert(ctx != NULL);
uint8_t ic = ctx->order[c];
size_t freq_c = ++(ctx->freq[c]);
uint8_t *pd;
for (pd = ctx->sorted + ic - 1; pd >= ctx->sorted; --pd) {
if (freq_c <= ctx->freq[*pd])
break;
}
uint8_t d = *(pd + 1);
if (c != d)
swap_symbols(ctx, c, d);
}
/* Geometric probability mode.
* See https://ipnpr.jpl.nasa.gov/progress_report/42-159/159E.pdf
*/
static void update_model(uint8_t delta)
{
if (symbol_count == RESET_INTERVAL) {
int k;
/* 2^k <= E{r[k]} + 0 */
for (k = 1; (symbol_count << k) <= symbol_sum; ++k)
;
opt_k = k - 1;
symbol_count = 0;
symbol_sum = 0;
}
symbol_sum += delta;
symbol_count++;
}
void *x_compress(void *iptr, size_t isize, void *optr)
{
generic_io_t io;
uint8_t *end = (uint8_t *) iptr + isize;
struct context *ctx = table + 0;
initiate(&io, optr, NULL, WRITE_MODE);
for (uint8_t *iptrc = iptr; iptrc < end; ++iptrc) {
uint8_t c = *iptrc;
/* get index */
uint8_t d = ctx->order[c];
write_golomb(&io, opt_k, (uint32_t) d);
assert(c == ctx->sorted[d]);
/* Update context model */
increment_frequency(ctx, c);
/* Update Golomb-Rice model */
update_model(d);
ctx = table + c;
}
/* EOF symbol */
write_golomb(&io, opt_k, 256);
finalize(&io, WRITE_MODE);
return io.ptr;
}
void *x_decompress(void *iptr, size_t isize, void *optr)
{
generic_io_t io;
uint8_t *end = (uint8_t *) iptr + isize;
struct context *ctx = table + 0;
initiate(&io, iptr, end, READ_MODE);
uint8_t *optrc = optr;
for (;; ++optrc) {
uint32_t d = read_golom(&io, opt_k);
if (d >= 256)
break;
uint8_t c = ctx->sorted[d];
*optrc = c;
/* Update context model */
increment_frequency(ctx, c);
/* Update Golomb-Rice model */
update_model(d);
ctx = table + c;
}
finalize(&io, READ_MODE);
return optrc;
}