forked from urbit/vere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitstream.c
1283 lines (1043 loc) · 24.8 KB
/
bitstream.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// @file
#include "bitstream.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
ur_cue_res_e
ur_bsr_init(ur_bsr_t *bsr, uint64_t len, const uint8_t *bytes)
{
// check for overflow
//
if ( (len << 3) < len ) {
return ur_cue_meme;
}
bsr->left = len;
bsr->bytes = bytes;
return ur_cue_good;
}
ur_bool_t
ur_bsr_sane(ur_bsr_t *bsr)
{
ur_bool_t ret = 8 > bsr->off;
if ( !bsr->left ) {
ret = ret && (!bsr->off && !bsr->bytes);
}
return ret;
}
ur_cue_res_e
ur_bsr_bit(ur_bsr_t *bsr, uint8_t *out)
{
uint64_t left = bsr->left;
if ( !left ) {
return ur_cue_gone;
}
else {
const uint8_t *b = bsr->bytes;
uint8_t off = bsr->off;
uint8_t bit = (b[0] >> off) & 1;
if ( 7 == off ) {
bsr->bytes = ( --left ) ? (b + 1) : 0;
bsr->left = left;
bsr->off = 0;
}
else {
bsr->off = 1 + off;
}
bsr->bits++;
*out = bit;
return ur_cue_good;
}
}
uint8_t
ur_bsr_bit_any(ur_bsr_t *bsr)
{
uint64_t left = bsr->left;
bsr->bits++;
if ( !left ) {
return 0;
}
else {
const uint8_t *b = bsr->bytes;
uint8_t off = bsr->off;
uint8_t bit = (b[0] >> off) & 1;
if ( 7 == off ) {
bsr->bytes = ( --left ) ? (b + 1) : 0;
bsr->left = left;
bsr->off = 0;
}
else {
bsr->off = 1 + off;
}
return bit;
}
}
uint8_t
ur_bsr8_any(ur_bsr_t *bsr, uint8_t len)
{
uint64_t left = bsr->left;
len = ur_min(8, len);
bsr->bits += len;
if ( !left ) {
return 0;
}
else {
uint8_t off = bsr->off;
uint8_t rest = 8 - off;
const uint8_t *b = bsr->bytes;
uint8_t m = b[0] >> off;
if ( len < rest ) {
bsr->off = off + len;
return m & ((1 << len) - 1);
}
else if ( 1 == left ) {
bsr->off = 0;
bsr->left = 0;
bsr->bytes = 0;
return m;
}
else {
off = len - rest;
bsr->off = off;
bsr->left--;
bsr->bytes++;
{
uint8_t l = b[1] & ((1 << off) - 1);
return m ^ (l << rest);
}
}
}
}
uint32_t
ur_bsr32_any(ur_bsr_t *bsr, uint8_t len)
{
uint64_t left = bsr->left;
len = ur_min(32, len);
bsr->bits += len;
if ( !left ) {
return 0;
}
else {
uint8_t off = bsr->off;
uint8_t rest = 8 - off;
uint32_t m = bsr->bytes[0] >> off;
if ( len < rest ) {
bsr->off = off + len;
return m & ((1 << len) - 1);
}
else {
const uint8_t *b;
uint8_t mask, len_byt;
uint32_t l;
len -= rest;
left--;
b = ++bsr->bytes;
len_byt = len >> 3;
if ( len_byt >= left ) {
len_byt = left;
bsr->off = off = 0;
bsr->left = 0;
bsr->bytes = 0;
}
else {
bsr->off = off = ur_mask_3(len);
bsr->left = left - len_byt;
bsr->bytes += len_byt;
}
mask = (1 << off) - 1;
switch ( len_byt ) {
default: assert(0);
case 4: {
l = (uint32_t)b[0]
^ (uint32_t)b[1] << 8
^ (uint32_t)b[2] << 16
^ (uint32_t)b[3] << 24;
} break;
case 3: {
l = (uint32_t)b[0]
^ (uint32_t)b[1] << 8
^ (uint32_t)b[2] << 16;
if ( mask ) {
l ^= (uint32_t)(b[3] & mask) << 24;
}
} break;
case 2: {
l = (uint32_t)b[0]
^ (uint32_t)b[1] << 8;
if ( mask ) {
l ^= (uint32_t)(b[2] & mask) << 16;
}
} break;
case 1: {
l = (uint32_t)b[0];
if ( mask ) {
l ^= (uint32_t)(b[1] & mask) << 8;
}
} break;
case 0: {
l = ( mask ) ? (uint32_t)(b[0] & mask) : 0;
} break;
}
return m ^ (l << rest);
}
}
}
uint64_t
ur_bsr64_any(ur_bsr_t *bsr, uint8_t len)
{
uint64_t left = bsr->left;
len = ur_min(64, len);
bsr->bits += len;
if ( !left ) {
return 0;
}
else {
uint8_t off = bsr->off;
uint8_t rest = 8 - off;
uint64_t m = bsr->bytes[0] >> off;
if ( len < rest ) {
bsr->off = off + len;
return m & ((1 << len) - 1);
}
else {
const uint8_t *b;
uint8_t mask, len_byt;
uint64_t l;
len -= rest;
left--;
b = ++bsr->bytes;
len_byt = len >> 3;
if ( len_byt >= left ) {
len_byt = left;
bsr->off = off = 0;
bsr->left = 0;
bsr->bytes = 0;
}
else {
bsr->off = off = ur_mask_3(len);
bsr->left = left - len_byt;
bsr->bytes += len_byt;
}
mask = (1 << off) - 1;
switch ( len_byt ) {
case 8: {
l = (uint64_t)b[0]
^ (uint64_t)b[1] << 8
^ (uint64_t)b[2] << 16
^ (uint64_t)b[3] << 24
^ (uint64_t)b[4] << 32
^ (uint64_t)b[5] << 40
^ (uint64_t)b[6] << 48
^ (uint64_t)b[7] << 56;
} break;
case 7: {
l = (uint64_t)b[0]
^ (uint64_t)b[1] << 8
^ (uint64_t)b[2] << 16
^ (uint64_t)b[3] << 24
^ (uint64_t)b[4] << 32
^ (uint64_t)b[5] << 40
^ (uint64_t)b[6] << 48;
if ( mask ) {
l ^= (uint64_t)(b[7] & mask) << 56;
}
} break;
case 6: {
l = (uint64_t)b[0]
^ (uint64_t)b[1] << 8
^ (uint64_t)b[2] << 16
^ (uint64_t)b[3] << 24
^ (uint64_t)b[4] << 32
^ (uint64_t)b[5] << 40;
if ( mask ) {
l ^= (uint64_t)(b[6] & mask) << 48;
}
} break;
case 5: {
l = (uint64_t)b[0]
^ (uint64_t)b[1] << 8
^ (uint64_t)b[2] << 16
^ (uint64_t)b[3] << 24
^ (uint64_t)b[4] << 32;
if ( mask ) {
l ^= (uint64_t)(b[5] & mask) << 40;
}
} break;
case 4: {
l = (uint64_t)b[0]
^ (uint64_t)b[1] << 8
^ (uint64_t)b[2] << 16
^ (uint64_t)b[3] << 24;
if ( mask ) {
l ^= (uint64_t)(b[4] & mask) << 32;
}
} break;
case 3: {
l = (uint64_t)b[0]
^ (uint64_t)b[1] << 8
^ (uint64_t)b[2] << 16;
if ( mask ) {
l ^= (uint64_t)(b[3] & mask) << 24;
}
} break;
case 2: {
l = (uint64_t)b[0]
^ (uint64_t)b[1] << 8;
if ( mask ) {
l ^= (uint64_t)(b[2] & mask) << 16;
}
} break;
case 1: {
l = (uint64_t)b[0];
if ( mask ) {
l ^= (uint64_t)(b[1] & mask) << 8;
}
} break;
case 0: {
l = ( mask ) ? (uint64_t)(b[0] & mask) : 0;
} break;
}
return m ^ (l << rest);
}
}
}
void
ur_bsr_bytes_any(ur_bsr_t *bsr, uint64_t len, uint8_t *out)
{
uint64_t left = bsr->left;
bsr->bits += len;
if ( !left ) {
return;
}
else {
const uint8_t *b = bsr->bytes;
uint8_t off = bsr->off;
uint64_t len_byt = len >> 3;
uint8_t len_bit = ur_mask_3(len);
uint64_t need = len_byt + !!len_bit;
if ( !off ) {
if ( need > left ) {
memcpy(out, b, left);
left = 0;
bsr->bytes = 0;
}
else {
memcpy(out, b, len_byt);
off = len_bit;
if ( off ) {
out[len_byt] = b[len_byt] & ((1 << off) - 1);
}
left -= len_byt;
bsr->bytes = ( left ) ? b + len_byt : 0;
}
}
// the most-significant bits from a byte in the stream
// become the least-significant bits of an output byte, and vice-versa
//
else {
uint8_t rest = 8 - off;
uint64_t last = left - 1;
uint64_t max = ur_min(last, len_byt);
uint8_t m, l;
// loop over all the bytes we need (or all that remain)
//
{
uint64_t i;
for ( i = 0; i < max; i++ ) {
out[i] = (b[i] >> off) ^ (b[i + 1] << rest);
}
b += max;
m = *b >> off;
}
// we're reading into or beyond the last byte [bsr]
//
// [m] holds all the remaining bits in [bsr],
// but we might not need all of it
//
if ( need >= left ) {
uint8_t bits = len - (last << 3);
if ( bits < rest ) {
out[max] = m & ((1 << len_bit) - 1);
bsr->bytes = b;
left = 1;
off += len_bit;
}
else {
out[max] = m;
bsr->bytes = 0;
left = 0;
off = 0;
}
}
// we need less than a byte, but it might span multiple bytes
//
else {
uint8_t bits = off + len_bit;
uint8_t step = !!(bits >> 3);
bsr->bytes = b + step;
left -= len_byt + step;
off = ur_mask_3(bits);
if ( len_bit ) {
if ( len_bit <= rest ) {
out[max] = m & ((1 << len_bit) - 1);
}
else {
l = *++b & ((1 << off) - 1);
out[max] = (m ^ (l << rest)) & ((1 << len_bit) - 1);
}
}
}
}
bsr->off = off;
bsr->left = left;
}
}
void
ur_bsr_skip_any(ur_bsr_t *bsr, uint64_t len)
{
uint64_t left = bsr->left;
bsr->bits += len;
if ( !left ) {
return;
}
else {
const uint8_t *b = bsr->bytes;
uint8_t off = bsr->off;
uint64_t len_byt = len >> 3;
uint8_t len_bit = ur_mask_3(len);
uint64_t need = len_byt + !!len_bit;
uint8_t rest = 8 - off;
uint64_t last = left - 1;
b += ur_min(last, len_byt) + 1;
if ( need >= left ) {
uint8_t bits = len - (last << 3);
if ( bits < rest ) {
bsr->bytes = b - 1;
left = 1;
off += len_bit;
}
else {
bsr->bytes = 0;
left = 0;
off = 0;
}
}
else {
uint8_t bits = off + len_bit;
uint8_t step = !!(bits >> 3);
bsr->bytes = b - (1 - step);
left -= len_byt + step;
off = ur_mask_3(bits);
}
bsr->off = off;
bsr->left = left;
}
}
static inline ur_cue_res_e
_bsr_set_gone(ur_bsr_t *bsr, uint8_t bits)
{
bsr->bits += bits;
bsr->bytes = 0;
bsr->left = 0;
bsr->off = 0;
return ur_cue_gone;
}
ur_cue_res_e
ur_bsr_tag(ur_bsr_t *bsr, ur_cue_tag_e *out)
{
uint64_t left = bsr->left;
if ( !left ) {
return ur_cue_gone;
}
else {
const uint8_t *b = bsr->bytes;
uint8_t off = bsr->off;
uint8_t bit = (b[0] >> off) & 1;
uint8_t len = 1;
if ( 0 == bit ) {
*out = ur_jam_atom;
}
else {
if ( 7 == off ) {
if ( 1 == left ) {
return _bsr_set_gone(bsr, 1);
}
bit = b[1] & 1;
}
else {
bit = (b[0] >> (off + 1)) & 1;
}
len++;
*out = ( 0 == bit ) ? ur_jam_cell : ur_jam_back;
}
{
uint8_t bits = off + len;
uint8_t bytes = bits >> 3;
left -= bytes;
if ( !left ) {
bsr->bytes = 0;
bsr->left = 0;
bsr->off = 0;
}
else {
bsr->bytes += bytes;
bsr->left = left;
bsr->off = ur_mask_3(bits);
}
bsr->bits += len;
return ur_cue_good;
}
}
}
static inline ur_cue_res_e
_bsr_log_meme(ur_bsr_t *bsr)
{
bsr->bits += 256;
bsr->bytes += 32;
bsr->left -= 32;
return ur_cue_meme;
}
ur_cue_res_e
ur_bsr_log(ur_bsr_t *bsr, uint8_t *out)
{
uint64_t left = bsr->left;
if ( !left ) {
return ur_cue_gone;
}
else {
uint8_t off = bsr->off;
const uint8_t *b = bsr->bytes;
uint8_t byt = b[0] >> off;
uint8_t skip = 0;
while ( !byt ) {
if ( 32 == skip ) {
return _bsr_log_meme(bsr);
}
byt = b[++skip];
if ( skip == left ) {
return _bsr_set_gone(bsr, (skip << 3) - off);
}
}
{
uint32_t zeros = ur_tz8(byt) + (skip ? ((skip << 3) - off) : 0);
if ( 255 < zeros ) {
return _bsr_log_meme(bsr);
}
else {
uint32_t bits = off + 1 + zeros;
uint8_t bytes = bits >> 3;
left -= bytes;
bsr->bytes = left ? (b + bytes) : 0;
bsr->bits += 1 + zeros;
bsr->left = left;
bsr->off = ur_mask_3(bits);
*out = zeros;
return ur_cue_good;
}
}
}
}
ur_cue_res_e
ur_bsr_rub_len(ur_bsr_t *bsr, uint64_t *out)
{
ur_cue_res_e res;
uint8_t len;
if ( ur_cue_good != (res = ur_bsr_log(bsr, &len)) ) {
return res;
}
else if ( 64 <= len ) {
return ur_cue_meme;
}
switch ( len ) {
case 0: {
*out = 0;
} break;
case 1: {
*out = 1;
} break;
default: {
len--;
*out = ur_bsr64_any(bsr, len) ^ (1ULL << len);
} break;
}
return ur_cue_good;
}
/*
** bitstream-writer operations follow a pattern of an unsafe (inline)
** implementation, unsafe wrt to buffer size and reallocation,
** wrapped in a public function with buffer size checks.
**
** higher-level operations made up of multiple discrete writes check
** the buffer size once for all involved writes.
**
** this pattern should be easily adaptable to an alternate bitstream-writer
** implementation that flushes accumulated output periodically instead
** of reallocating the output buffer.
*/
void
ur_bsw_init(ur_bsw_t *bsw, uint64_t prev, uint64_t size)
{
bsw->prev = prev;
bsw->size = size;
bsw->bytes = calloc(size, 1);
if ( !bsw->bytes ) {
fprintf(stderr,
"ur: bitstream-init allocation failed, out of memory\r\n");
abort();
}
}
void
ur_bsw_grow(ur_bsw_t *bsw, uint64_t step)
{
uint64_t size = bsw->size;
uint64_t next = size + step;
bsw->bytes = realloc(bsw->bytes, next);
if ( !bsw->bytes ) {
fprintf(stderr,
"ur: bitstream-write allocation failed, out of memory\r\n");
abort();
}
memset(bsw->bytes + size, 0, step);
bsw->prev = size;
bsw->size = next;
}
ur_bool_t
ur_bsw_sane(ur_bsw_t *bsw)
{
return ( (8 > bsw->off)
&& ((bsw->fill << 3) + bsw->off == bsw->bits) );
}
uint64_t
ur_bsw_done(ur_bsw_t *bsw, uint64_t *len, uint8_t **byt)
{
uint64_t bits = bsw->bits;
*len = bsw->fill + !!bsw->off;
*byt = bsw->bytes;
memset(bsw, 0, sizeof(*bsw));
return bits;
}
static inline void
_bsw_bit_unsafe(ur_bsw_t *bsw, uint8_t bit)
{
uint64_t fill = bsw->fill;
uint8_t off = bsw->off;
bsw->bytes[fill] ^= (bit & 1) << off;
if ( 7 == off ) {
bsw->fill = 1 + fill;
bsw->off = 0;
}
else {
bsw->off = 1 + off;
}
bsw->bits++;
}
void
ur_bsw_bit(ur_bsw_t *bsw, uint8_t bit)
{
if ( (7 == bsw->off)
&& ((1 + bsw->fill) == bsw->size) )
{
ur_bsw_grow(bsw, bsw->prev);
}
_bsw_bit_unsafe(bsw, bit);
}
static inline void
_bsw8_unsafe(ur_bsw_t *bsw, uint8_t len, uint8_t byt)
{
uint64_t fill = bsw->fill;
uint8_t off = bsw->off;
uint8_t rest = 8 - off;
uint8_t l, m;
// the least-significant bits of the input become the
// most-significant bits of a byte in the output stream
//
if ( len < rest ) {
l = byt & ((1 << len) - 1);
bsw->bytes[fill] ^= l << off;
bsw->off = off + len;
}
// and vice-versa
//
else {
l = byt & ((1 << rest) - 1);
m = byt >> rest;
bsw->bytes[fill++] ^= l << off;
off = len - rest;
bsw->bytes[fill] = m & ((1 << off) - 1);
bsw->fill = fill;
bsw->off = off;
}
bsw->bits += len;
}
void
ur_bsw8(ur_bsw_t *bsw, uint8_t len, uint8_t byt)
{
len = ur_min(8, len);
if ( bsw->fill + !!((bsw->off + len) >> 3) >= bsw->size ) {
ur_bsw_grow(bsw, bsw->prev);
}
_bsw8_unsafe(bsw, len, byt);
}
static inline void
_bsw32_unsafe(ur_bsw_t *bsw, uint8_t len, uint32_t val)
{
uint64_t fill = bsw->fill;
uint8_t off = bsw->off;
uint8_t *bytes = bsw->bytes;
bsw->bits += len;
if ( off ) {
uint8_t rest = 8 - off;
if ( len < rest ) {
bytes[fill] ^= (val & ((1 << len) - 1)) << off;
bsw->off = off + len;
return;
}
bytes[fill++] ^= (val & ((1 << rest) - 1)) << off;
val >>= rest;
len -= rest;
}
switch ( len >> 3 ) {
case 4: {
bytes[fill++] = ur_mask_8(val);
bytes[fill++] = ur_mask_8(val >> 8);
bytes[fill++] = ur_mask_8(val >> 16);
bytes[fill++] = ur_mask_8(val >> 24);
// no offset is possible here
//
bsw->fill = fill;
bsw->off = 0;
return;
}
case 3: {
bytes[fill++] = ur_mask_8(val);
bytes[fill++] = ur_mask_8(val >> 8);
bytes[fill++] = ur_mask_8(val >> 16);
val >>= 24;
} break;
case 2: {
bytes[fill++] = ur_mask_8(val);
bytes[fill++] = ur_mask_8(val >> 8);
val >>= 16;
} break;
case 1: {
bytes[fill++] = ur_mask_8(val);
val >>= 8;
} break;
}
off = ur_mask_3(len);
if ( off ) {
bytes[fill] = (uint8_t)(val & ((1 << off) - 1));
}
bsw->fill = fill;
bsw->off = off;
}
void
ur_bsw32(ur_bsw_t *bsw, uint8_t len, uint32_t val)
{
uint8_t need;
len = ur_min(32, len);
need = ur_bloq_up3( bsw->off + len );
if ( bsw->fill + need >= bsw->size ) {
ur_bsw_grow(bsw, ur_max(need, bsw->prev));
}
_bsw32_unsafe(bsw, len, val);
}
static inline void
_bsw64_unsafe(ur_bsw_t *bsw, uint8_t len, uint64_t val)
{
uint64_t fill = bsw->fill;
uint8_t off = bsw->off;
uint8_t *bytes = bsw->bytes;
bsw->bits += len;
if ( off ) {
uint8_t rest = 8 - off;
if ( len < rest ) {
bytes[fill] ^= (val & ((1 << len) - 1)) << off;
bsw->off = off + len;
return;
}
bytes[fill++] ^= (val & ((1 << rest) - 1)) << off;
val >>= rest;
len -= rest;
}
switch ( len >> 3 ) {
case 8: {
bytes[fill++] = ur_mask_8(val);
bytes[fill++] = ur_mask_8(val >> 8);
bytes[fill++] = ur_mask_8(val >> 16);
bytes[fill++] = ur_mask_8(val >> 24);
bytes[fill++] = ur_mask_8(val >> 32);
bytes[fill++] = ur_mask_8(val >> 40);
bytes[fill++] = ur_mask_8(val >> 48);
bytes[fill++] = ur_mask_8(val >> 56);
// no offset is possible here
//
bsw->fill = fill;
bsw->off = 0;
return;
}
case 7: {
bytes[fill++] = ur_mask_8(val);
bytes[fill++] = ur_mask_8(val >> 8);
bytes[fill++] = ur_mask_8(val >> 16);
bytes[fill++] = ur_mask_8(val >> 24);
bytes[fill++] = ur_mask_8(val >> 32);
bytes[fill++] = ur_mask_8(val >> 40);
bytes[fill++] = ur_mask_8(val >> 48);
val >>= 56;
} break;
case 6: {
bytes[fill++] = ur_mask_8(val);
bytes[fill++] = ur_mask_8(val >> 8);
bytes[fill++] = ur_mask_8(val >> 16);
bytes[fill++] = ur_mask_8(val >> 24);
bytes[fill++] = ur_mask_8(val >> 32);
bytes[fill++] = ur_mask_8(val >> 40);
val >>= 48;
} break;
case 5: {
bytes[fill++] = ur_mask_8(val);
bytes[fill++] = ur_mask_8(val >> 8);
bytes[fill++] = ur_mask_8(val >> 16);
bytes[fill++] = ur_mask_8(val >> 24);
bytes[fill++] = ur_mask_8(val >> 32);
val >>= 40;
} break;
case 4: {
bytes[fill++] = ur_mask_8(val);
bytes[fill++] = ur_mask_8(val >> 8);
bytes[fill++] = ur_mask_8(val >> 16);
bytes[fill++] = ur_mask_8(val >> 24);
val >>= 32;
} break;
case 3: {
bytes[fill++] = ur_mask_8(val);
bytes[fill++] = ur_mask_8(val >> 8);
bytes[fill++] = ur_mask_8(val >> 16);
val >>= 24;
} break;
case 2: {
bytes[fill++] = ur_mask_8(val);