-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrologue.c
539 lines (405 loc) · 9.18 KB
/
Prologue.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* Cemetery prologue */
typedef unsigned long word_t;
#define W ((int)sizeof(word_t))
#define WB (W * 8)
#define bit(i) ((word_t)1 << (i))
#define mask_l(i) (bit(i) - 1)
#define mask_m(i) (~(mask_l(WB - i)))
struct cmt_bits {
int length;
int size;
word_t data[];
};
struct cmt_init {
int length;
unsigned char data[];
};
typedef struct cmt_bits *cmt_bits_t;
static inline void __cmt_error(char *s)
{
fprintf(stderr, "Cemetery error: %s\n", s);
abort();
}
void cmt_free(cmt_bits_t b)
{
free(b);
}
#define __cmt_assert(c) \
do { \
if (!(c)) \
__cmt_error("ASSERT FAILED: " #c); \
} while(0);
static inline int max(int a, int b)
{
return a > b ? a : b;
}
int cmt_length(cmt_bits_t b) {
return b->length;
}
static inline int __cmt_mod(int a, int b)
{
__cmt_assert(b > 0);
/* Special case for powers of 2 */
if ((b & (b - 1)) == 0)
return a & (b - 1);
return (a % b) + (a < 0 ? b : 0);
}
static cmt_bits_t __cmt_alloc(int length)
{
cmt_bits_t ret;
int size;
size = (length + WB - 1) / WB;
ret = calloc(1, offsetof(struct cmt_bits, data[size]));
if (!ret)
__cmt_error("allocation failed\n");
ret->length = length;
ret->size = size;
return ret;
}
static void __cmt_fixup(cmt_bits_t b)
{
word_t m;
if (b->length % WB) {
m = -1;
m <<= b->length % WB;
b->data[b->size - 1] &= ~m;
}
}
static inline bool get_bit(cmt_bits_t b, int o)
{
if (o > b->length)
return false;
return b->data[o / WB] & bit(o % WB);
}
static inline void set_bit(cmt_bits_t b, int o)
{
if (o > b->length)
__cmt_error("wat");
b->data[o / WB] |= bit(o % WB);
}
static inline word_t get_word(cmt_bits_t b, int wi)
{
if (wi < 0)
__cmt_error("fuck 1");
/*
* Allowed for now since it simplies the binops
* but, up for discussion wether we should fail
* hard here.
*/
if (wi >= b->size)
return 0UL;
return b->data[wi];
}
static inline void set_word(cmt_bits_t b, int wi, word_t w)
{
__cmt_assert(wi >= 0);
__cmt_assert(wi < (b->length + WB - 1)/ WB);
b->data[wi] = w;
}
static cmt_bits_t __cmt_band(cmt_bits_t l, cmt_bits_t r)
{
cmt_bits_t ret = __cmt_alloc(max(l->length, r->length));
int i;
for (i = 0; i < ret->size; i++)
set_word(ret, i, get_word(l, i) & get_word(r, i));
return ret;
}
static cmt_bits_t __cmt_bor(cmt_bits_t l, cmt_bits_t r)
{
cmt_bits_t ret = __cmt_alloc(max(l->length, r->length));
int i;
for (i = 0; i < ret->size; i++)
set_word(ret, i, get_word(l, i) | get_word(r, i));
return ret;
}
static cmt_bits_t __cmt_xor(cmt_bits_t l, cmt_bits_t r)
{
cmt_bits_t ret = __cmt_alloc(max(l->length, r->length));
int i;
for (i = 0; i < ret->size; i++)
set_word(ret, i, get_word(l, i) ^ get_word(r, i));
return ret;
}
static inline word_t mask_set(word_t v, word_t on, word_t off)
{
return (v & ~off) | on;
}
static void __cmt_bitcopy_iter(cmt_bits_t to, int offset,
cmt_bits_t from, int start_bit, int len)
{
int i;
if (!len)
return;
if (start_bit / WB == (start_bit + len - 1) / WB &&
offset / WB == (offset + len - 1) / WB) {
word_t t, w, m;
w = from->data[start_bit / WB];
t = to->data[offset / WB];
w >>= start_bit % WB;
m = mask_l(len);
w &= m;
w <<= offset % WB;
m <<= offset % WB;
t = mask_set(t, w, m);
to->data[offset / WB] = t;
return;
}
for (i = 0; i < len; i++) {
if (get_bit(from, start_bit + i))
set_bit(to, offset + i);
}
}
static void __cmt_bitcopy(cmt_bits_t to, int offset,
cmt_bits_t from, int start_bit, int len)
{
int fw, lw;
int w;
int lskip, rskip;
word_t *T, *F;
int L, R;
int n;
if (!len)
return;
fw = (offset + WB - 1) / WB;
lw = (offset + len) / WB;
lskip = fw * WB - offset;
rskip = offset +len - lw * WB;
T = &to->data[fw];
F = &from->data[(start_bit + lskip) / WB];
L = (start_bit + lskip) % WB;
R = WB - L;
n = lw - fw;
if (n <= 0) {
__cmt_bitcopy_iter(to, offset, from, start_bit, len);
return;
}
if (L == 0) {
for (w = 0; w < n; w++)
T[w] = F[w];
} else {
for (w = 0; w < n; w++) {
word_t t = T[w];
t = mask_set(t, F[w] >> L, mask_l(R));
t = mask_set(t, F[w +1] << R, mask_m(L));
T[w] = t;
}
}
__cmt_bitcopy_iter(to, offset, from, start_bit, lskip);
__cmt_bitcopy_iter(to, offset + len - rskip, from,
start_bit + len - rskip, rskip);
}
static cmt_bits_t __cmt_bconcat(cmt_bits_t l, cmt_bits_t r)
{
cmt_bits_t ret = __cmt_alloc(l->length + r->length);
__cmt_bitcopy(ret, 0, r, 0, r->length);
__cmt_bitcopy(ret, r->length, l, 0, l->length);
return ret;
}
static cmt_bits_t __cmt_slice(cmt_bits_t l, int from, int to)
{
int tob;
cmt_bits_t ret = __cmt_alloc(to - from + 1);
__cmt_assert(from >= 0);
__cmt_assert(from < l->length);
__cmt_assert(to >= 0);
__cmt_assert(to < l->length);
/*
* Slice takes bits counted from the left (MSB),
* so turn them into an index from the right (LSB)
*/
tob = l->length - 1 - to;
__cmt_bitcopy(ret, 0, l, tob, ret->length);
return ret;
}
static cmt_bits_t __cmt_bnot(cmt_bits_t e)
{
cmt_bits_t ret = __cmt_alloc(e->length);
int i;
for (i = 0; i < ret->size; i++)
set_word(ret, i, ~get_word(e, i));
/* Fixup the trailing bits, they should always be 0 */
__cmt_fixup(ret);
return ret;
}
static cmt_bits_t __cmt_permute(cmt_bits_t e, int perm[], int len)
{
cmt_bits_t ret = __cmt_alloc(len);
int i;
for (i = 0; i < len; i++) {
if (get_bit(e, e->length - perm[i]))
set_bit(ret, len - 1 - i);
}
return ret;
}
static cmt_bits_t __cmt_tobits(int x, int len)
{
cmt_bits_t ret = __cmt_alloc(len);
ret->data[0] = x;
return ret;
}
static inline int __cmt_toint(cmt_bits_t b)
{
return b->data[0];
}
static cmt_bits_t __cmt_shiftl(cmt_bits_t b, int s)
{
__cmt_assert(s >= 0);
cmt_bits_t ret = __cmt_alloc(b->length + s);
__cmt_bitcopy(ret, s, b, 0, b->length);
return ret;
}
static cmt_bits_t __cmt_shiftr(cmt_bits_t b, int s)
{
__cmt_assert(s >= 0);
if (s > b->length)
return __cmt_alloc(0);
cmt_bits_t ret = __cmt_alloc(b->length - s);
__cmt_bitcopy(ret, 0, b, s, b->length - s);
return ret;
}
static cmt_bits_t __cmt_rotl(cmt_bits_t b, int s)
{
cmt_bits_t ret = __cmt_alloc(b->length);
s = __cmt_mod(s, b->length);
if (b->length <= WB) {
word_t p, q, w;
w = b->data[0];
p = (w & mask_l(b->length - s)) << s;
q = w >> (b->length - s);
ret->data[0] = p | q;
return ret;
} else {
__cmt_bitcopy(ret, 0, b, b->length - s, s);
__cmt_bitcopy(ret, s, b, 0, b->length - s);
}
return ret;
}
static cmt_bits_t __cmt_inplace_rotl(cmt_bits_t b, int s)
{
s = __cmt_mod(s, b->length);
if (b->length <= WB) {
word_t p, q, w;
w = b->data[0];
p = (w & mask_l(b->length - s)) << s;
q = w >> (b->length - s);
b->data[0] = p | q;
return b;
} else {
/*
* Just call the usual function and free
* the original result. We'll implement this
* properly later
*/
cmt_bits_t ret = __cmt_rotl(b, s);
cmt_free(b);
return ret;
}
}
static cmt_bits_t __cmt_inplace_rotr(cmt_bits_t b, int s)
{
return __cmt_inplace_rotl(b, b->length - s);
}
static cmt_bits_t __cmt_rotr(cmt_bits_t b, int s)
{
return __cmt_rotl(b, b->length - s);
}
static cmt_bits_t __cmt_init(struct cmt_init *init, int length)
{
cmt_bits_t ret = __cmt_alloc(length);
memcpy(ret->data, init->data, init->length);
return ret;
}
#define add_carry(c, l, r) ({ \
const word_t sc = c, sl = l, sr = r; \
const word_t t = sl + sr; \
if (t < l || t + sc < t) \
c = 1; \
t + sc; \
})
static cmt_bits_t __cmt_modplus(cmt_bits_t l, cmt_bits_t r)
{
cmt_bits_t ret = __cmt_alloc(max(l->length, r->length));
word_t c = 0, cc;
int i;
for (i = 0; i < ret->size; i++) {
cc = add_carry(c, get_word(l,i), get_word(r, i));
set_word(ret, i, cc);
}
__cmt_fixup(ret);
return ret;
}
static bool __cmt_eq(cmt_bits_t l, cmt_bits_t r)
{
if (l->length != r->length)
return false;
__cmt_assert(l->size == r->size);
return !memcmp(l->data, r->data, l->size);
}
static inline cmt_bits_t __cmt_zero(int l)
{
return __cmt_alloc(l);
}
static cmt_bits_t __cmt_copy(cmt_bits_t b)
{
cmt_bits_t ret = __cmt_alloc(b->length);
__cmt_bitcopy(ret, 0, b, 0, b->length);
return ret;
}
cmt_bits_t cmt_mkbuf(const char *d, int bitlen)
{
cmt_bits_t ret = __cmt_alloc(bitlen);
char *t = (char*)ret->data;
int i;
__cmt_assert(bitlen % 8 == 0);
for (i = 0; i < bitlen / 8; i++)
t[bitlen/8 - 1 - i] = d[i];
return ret;
}
void cmt_frombuf(char *dest, cmt_bits_t b)
{
char *f = (char*)b->data;
int i;
int l = b->length / 8;
__cmt_assert(b->length % 8 == 0);
for (i = 0; i < l; i++)
dest[i] = f[l - 1 - i];
}
static cmt_bits_t __cmt_resize(cmt_bits_t b, int len)
{
int s = (len + WB - 1) / WB;
cmt_bits_t t;
t = realloc(b, offsetof(struct cmt_bits, data[s]));
if (!t)
__cmt_error("OOM");
t->length = len;
t->size = s;
return t;
}
static cmt_bits_t __cmt_resize_zero(cmt_bits_t b, int len)
{
int ol = b->size;
b = __cmt_resize(b, len);
if (b->size > ol)
memset(&b->data[ol], 0, W * (b->size - ol));
__cmt_fixup(b);
return b;
}
static void __cmt_init_bitarr(cmt_bits_t *a, int len)
{
int i;
for (i = 0; i < len; i++)
a[i] = __cmt_zero(0);
}
static void __cmt_free_bitarr(cmt_bits_t *a, int len)
{
int i;
for (i = 0; i < len; i++)
cmt_free(a[i]);
}
#undef W
#undef WB
#undef bit
#undef mask_l
#undef mask_m
#undef __cmt_assert
/* / Cemetery prologue */