forked from urbit/vere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
1459 lines (1222 loc) · 30.5 KB
/
serial.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 "serial.h"
#include <errno.h>
#include <fcntl.h>
#include "allocate.h"
#include "hashtable.h"
#include "jets/k.h"
#include "jets/q.h"
#include "retrieve.h"
#include "serial.h"
#include "ur.h"
#include "vortex.h"
#include "xtract.h"
const c3_y u3s_dit_y[64] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'-', '~'
};
/* _cs_jam_buf: struct for tracking the fibonacci-allocated jam of a noun
*/
struct _cs_jam_fib {
u3i_slab* sab_u;
u3p(u3h_root) har_p;
c3_w a_w;
c3_w b_w;
c3_w bit_w;
};
/* _cs_jam_fib_grow(): reallocate buffer with fibonacci growth
*/
static inline void
_cs_jam_fib_grow(struct _cs_jam_fib* fib_u, c3_w mor_w)
{
c3_w wan_w = fib_u->bit_w + mor_w;
// check for c3_w overflow
//
if ( wan_w < mor_w ) {
u3m_bail(c3__fail);
return;
}
if ( wan_w > fib_u->a_w ) {
c3_w c_w = 0;
// fibonacci growth
//
while ( c_w < wan_w ) {
c_w = fib_u->a_w + fib_u->b_w;
fib_u->b_w = fib_u->a_w;
fib_u->a_w = c_w;
}
u3i_slab_grow(fib_u->sab_u, 0, c_w);
}
}
/* _cs_jam_fib_chop(): chop [met_w] bits of [a] into [fib_u]
*/
static inline void
_cs_jam_fib_chop(struct _cs_jam_fib* fib_u, c3_w met_w, u3_noun a)
{
c3_w bit_w = fib_u->bit_w;
_cs_jam_fib_grow(fib_u, met_w);
fib_u->bit_w += met_w;
{
c3_w* buf_w = fib_u->sab_u->buf_w;
u3r_chop(0, 0, met_w, bit_w, buf_w, a);
}
}
/* _cs_jam_fib_mat(): length-prefixed encode (mat) [a] into [fib_u]
*/
static void
_cs_jam_fib_mat(struct _cs_jam_fib* fib_u, u3_noun a)
{
if ( 0 == a ) {
_cs_jam_fib_chop(fib_u, 1, 1);
}
else {
c3_w a_w = u3r_met(0, a);
c3_w b_w = c3_bits_word(a_w);
c3_w bit_w = fib_u->bit_w;
// amortize overflow checks and reallocation
//
{
c3_w met_w = a_w + (2 * b_w);
if ( a_w > (UINT32_MAX - 64) ) {
u3m_bail(c3__fail);
return;
}
_cs_jam_fib_grow(fib_u, met_w);
fib_u->bit_w += met_w;
}
{
c3_w src_w[2];
c3_w* buf_w = fib_u->sab_u->buf_w;
// _cs_jam_fib_chop(fib_u, b_w+1, 1 << b_w);
//
{
c3_d dat_d = (c3_d)1 << b_w;
src_w[0] = (c3_w)dat_d;
src_w[1] = dat_d >> 32;
u3r_chop_words(0, 0, b_w + 1, bit_w, buf_w, 2, src_w);
bit_w += b_w + 1;
}
// _cs_jam_fib_chop(fib_u, b_w-1, a_w);
//
{
src_w[0] = a_w;
u3r_chop_words(0, 0, b_w - 1, bit_w, buf_w, 1, src_w);
bit_w += b_w - 1;
}
// _cs_jam_fib_chop(fib_u, a_w, a);
//
u3r_chop(0, 0, a_w, bit_w, buf_w, a);
}
}
}
/* _cs_jam_fib_atom_cb(): encode atom or backref
*/
static void
_cs_jam_fib_atom_cb(u3_atom a, void* ptr_v)
{
struct _cs_jam_fib* fib_u = ptr_v;
u3_weak b = u3h_git(fib_u->har_p, a);
// if [a] has no backref, encode atom and put cursor into [har_p]
//
if ( u3_none == b ) {
u3h_put(fib_u->har_p, a, u3i_words(1, &(fib_u->bit_w)));
_cs_jam_fib_chop(fib_u, 1, 0);
_cs_jam_fib_mat(fib_u, a);
}
else {
c3_w a_w = u3r_met(0, a);
c3_w b_w = u3r_met(0, b);
// if [a] is smaller than the backref, encode atom
//
if ( a_w <= b_w ) {
_cs_jam_fib_chop(fib_u, 1, 0);
_cs_jam_fib_mat(fib_u, a);
}
// otherwise, encode backref
//
else {
_cs_jam_fib_chop(fib_u, 2, 3);
_cs_jam_fib_mat(fib_u, b);
}
}
}
/* _cs_jam_fib_cell_cb(): encode cell or backref
*/
static c3_o
_cs_jam_fib_cell_cb(u3_noun a, void* ptr_v)
{
struct _cs_jam_fib* fib_u = ptr_v;
u3_weak b = u3h_git(fib_u->har_p, a);
// if [a] has no backref, encode cell and put cursor into [har_p]
//
if ( u3_none == b ) {
u3h_put(fib_u->har_p, a, u3i_words(1, &(fib_u->bit_w)));
_cs_jam_fib_chop(fib_u, 2, 1);
return c3y;
}
// otherwise, encode backref and shortcircuit traversal
//
else {
_cs_jam_fib_chop(fib_u, 2, 3);
_cs_jam_fib_mat(fib_u, b);
return c3n;
}
}
/* u3s_jam_fib(): jam without atom allocation.
**
** returns atom-suitable words, and *bit_w will have
** the length (in bits). return should be freed with u3a_wfree().
*/
c3_w
u3s_jam_fib(u3i_slab* sab_u, u3_noun a)
{
struct _cs_jam_fib fib_u;
fib_u.har_p = u3h_new();
fib_u.sab_u = sab_u;
// fib(12) is small enough to be reasonably fast to allocate.
// fib(11) is needed to get fib(13).
//
//
fib_u.a_w = ur_fib12;
fib_u.b_w = ur_fib11;
fib_u.bit_w = 0;
u3i_slab_init(sab_u, 0, fib_u.a_w);
u3a_walk_fore(a, &fib_u, _cs_jam_fib_atom_cb, _cs_jam_fib_cell_cb);
u3h_free(fib_u.har_p);
return fib_u.bit_w;
}
typedef struct _jam_xeno_s {
u3p(u3h_root) har_p;
ur_bsw_t rit_u;
} _jam_xeno_t;
/* _cs_coin_chub(): shortcircuit u3i_chubs().
*/
static inline u3_atom
_cs_coin_chub(c3_d a_d)
{
return ( 0x7fffffffULL >= a_d ) ? a_d : u3i_chubs(1, &a_d);
}
/* _cs_jam_xeno_atom(): encode in/direct atom in bitstream.
*/
static inline void
_cs_jam_bsw_atom(ur_bsw_t* rit_u, c3_w met_w, u3_atom a)
{
if ( c3y == u3a_is_cat(a) ) {
// XX need a ur_bsw_atom32()
//
ur_bsw_atom64(rit_u, (c3_y)met_w, (c3_d)a);
}
else {
u3a_atom* vat_u = u3a_to_ptr(a);
// XX assumes little-endian
// XX need a ur_bsw_atom_words()
//
c3_y* byt_y = (c3_y*)vat_u->buf_w;
ur_bsw_atom_bytes(rit_u, (c3_d)met_w, byt_y);
}
}
/* _cs_jam_bsw_back(): encode in/direct backref in bitstream.
*/
static inline void
_cs_jam_bsw_back(ur_bsw_t* rit_u, c3_w met_w, u3_atom a)
{
c3_d bak_d = ( c3y == u3a_is_cat(a) )
? (c3_d)a
: u3r_chub(0, a);
// XX need a ur_bsw_back32()
//
ur_bsw_back64(rit_u, (c3_y)met_w, bak_d);
}
/* _cs_jam_xeno_atom(): encode atom or backref in bitstream.
*/
static void
_cs_jam_xeno_atom(u3_atom a, void* ptr_v)
{
_jam_xeno_t* jam_u = ptr_v;
ur_bsw_t* rit_u = &(jam_u->rit_u);
u3_weak bak = u3h_git(jam_u->har_p, a);
c3_w met_w = u3r_met(0, a);
if ( u3_none == bak ) {
u3h_put(jam_u->har_p, a, _cs_coin_chub(rit_u->bits));
_cs_jam_bsw_atom(rit_u, met_w, a);
}
else {
c3_w bak_w = u3r_met(0, bak);
if ( met_w <= bak_w ) {
_cs_jam_bsw_atom(rit_u, met_w, a);
}
else {
_cs_jam_bsw_back(rit_u, bak_w, bak);
}
}
}
/* _cs_jam_xeno_cell(): encode cell or backref in bitstream.
*/
static c3_o
_cs_jam_xeno_cell(u3_noun a, void* ptr_v)
{
_jam_xeno_t* jam_u = ptr_v;
ur_bsw_t* rit_u = &(jam_u->rit_u);
u3_weak bak = u3h_git(jam_u->har_p, a);
if ( u3_none == bak ) {
u3h_put(jam_u->har_p, a, _cs_coin_chub(rit_u->bits));
ur_bsw_cell(rit_u);
return c3y;
}
else {
_cs_jam_bsw_back(rit_u, u3r_met(0, bak), bak);
return c3n;
}
}
/* u3s_jam_xeno(): jam with off-loom buffer (re-)allocation.
*/
c3_d
u3s_jam_xeno(u3_noun a, c3_d* len_d, c3_y** byt_y)
{
_jam_xeno_t jam_u = {0};
ur_bsw_init(&jam_u.rit_u, ur_fib11, ur_fib12);
jam_u.har_p = u3h_new();
u3a_walk_fore(a, &jam_u, _cs_jam_xeno_atom, _cs_jam_xeno_cell);
u3h_free(jam_u.har_p);
return ur_bsw_done(&jam_u.rit_u, len_d, byt_y);
}
/* _cs_cue: stack frame for tracking intermediate cell results
*/
typedef struct _cs_cue {
u3_weak hed; // head of a cell or u3_none
u3_atom wid; // bitwidth of [hed] or 0
u3_atom cur; // bit-cursor position
} _cs_cue;
/* _cs_rub: rub, TRANSFER [cur], RETAIN [a]
*/
static inline u3_noun
_cs_rub(u3_atom cur, u3_atom a)
{
u3_noun pro = u3qe_rub(cur, a);
u3z(cur);
return pro;
}
/* _cs_cue_next(): advance into [a], reading next value
** TRANSFER [cur], RETAIN [a]
*/
static inline u3_noun
_cs_cue_next(u3a_pile* pil_u,
u3p(u3h_root) har_p,
u3_atom cur,
u3_atom a,
u3_atom* wid)
{
while ( 1 ) {
// read tag bit at cur
//
c3_y tag_y = u3qc_cut(0, cur, 1, a);
// low bit unset, (1 + cur) points to an atom
//
// produce atom and the width we read
//
if ( 0 == tag_y ) {
u3_noun bur = _cs_rub(u3i_vint(cur), a);
u3_noun pro = u3k(u3t(bur));
u3h_put(har_p, cur, u3k(pro));
*wid = u3qa_inc(u3h(bur));
u3z(bur);
return pro;
}
else {
// read tag bit at (1 + cur)
//
{
u3_noun x = u3qa_inc(cur);
tag_y = u3qc_cut(0, x, 1, a);
u3z(x);
}
// next bit set, (2 + cur) points to a backref
//
// produce referenced value and the width we read
//
if ( 1 == tag_y ) {
u3_noun bur = _cs_rub(u3ka_add(2, cur), a);
u3_noun pro = u3x_good(u3h_get(har_p, u3t(bur)));
*wid = u3qa_add(2, u3h(bur));
u3z(bur);
return pro;
}
// next bit unset, (2 + cur) points to the head of a cell
//
// push a head-frame onto the road stack and read the head
//
else {
_cs_cue* fam_u = u3a_push(pil_u);
// NB: fam_u->wid unused in head-frame
//
fam_u->hed = u3_none;
fam_u->cur = cur;
cur = u3qa_add(2, cur);
continue;
}
}
}
}
u3_noun
u3s_cue(u3_atom a)
{
// pro: cue'd noun product
// wid: bitwidth read to produce [pro]
// fam_u: stack frame
// har_p: backreference table
// pil_u: stack control structure
//
u3_noun pro;
u3_atom wid;
_cs_cue* fam_u;
u3p(u3h_root) har_p = u3h_new();
u3a_pile pil_u;
// initialize stack control
//
u3a_pile_prep(&pil_u, sizeof(*fam_u));
// commence cueing at bit-position 0
//
pro = _cs_cue_next(&pil_u, har_p, 0, a, &wid);
// process cell results
//
if ( c3n == u3a_pile_done(&pil_u) ) {
fam_u = u3a_peek(&pil_u);
do {
// head-frame: stash [pro] and [wid]; continue into the tail
//
if ( u3_none == fam_u->hed ) {
// NB: fam_u->wid unused in head-frame
//
fam_u->hed = pro;
fam_u->wid = wid;
// continue reading at the bit-position after [pro]
{
u3_noun cur = u3ka_add(2, u3qa_add(wid, fam_u->cur));
pro = _cs_cue_next(&pil_u, har_p, cur, a, &wid);
}
fam_u = u3a_peek(&pil_u);
}
// tail-frame: cons cell, recalculate [wid], and pop the stack
//
else {
pro = u3nc(fam_u->hed, pro);
u3h_put(har_p, fam_u->cur, u3k(pro));
u3z(fam_u->cur);
wid = u3ka_add(2, u3ka_add(wid, fam_u->wid));
fam_u = u3a_pop(&pil_u);
}
} while ( c3n == u3a_pile_done(&pil_u) );
}
u3z(wid);
u3h_free(har_p);
return pro;
}
/*
** stack frame for recording head vs tail iteration
**
** $? [u3_none bits=@]
** [hed=* bits=@]
*/
typedef struct _cue_frame_s {
u3_weak ref;
c3_d bit_d;
} _cue_frame_t;
/* _cs_cue_xeno_next(): read next value from bitstream, dictionary off-loom.
*/
static inline ur_cue_res_e
_cs_cue_xeno_next(u3a_pile* pil_u,
ur_bsr_t* red_u,
ur_dict32_t* dic_u,
u3_noun* out)
{
ur_root_t* rot_u = 0;
while ( 1 ) {
c3_d len_d, bit_d = red_u->bits;
ur_cue_tag_e tag_e;
ur_cue_res_e res_e;
if ( ur_cue_good != (res_e = ur_bsr_tag(red_u, &tag_e)) ) {
return res_e;
}
switch ( tag_e ) {
default: u3_assert(0);
case ur_jam_cell: {
_cue_frame_t* fam_u = u3a_push(pil_u);
fam_u->ref = u3_none;
fam_u->bit_d = bit_d;
continue;
}
case ur_jam_back: {
if ( ur_cue_good != (res_e = ur_bsr_rub_len(red_u, &len_d)) ) {
return res_e;
}
else if ( 62 < len_d ) {
return ur_cue_meme;
}
else {
c3_d bak_d = ur_bsr64_any(red_u, len_d);
c3_w bak_w;
if ( !ur_dict32_get(rot_u, dic_u, bak_d, &bak_w) ) {
return ur_cue_back;
}
*out = u3k((u3_noun)bak_w);
return ur_cue_good;
}
}
case ur_jam_atom: {
if ( ur_cue_good != (res_e = ur_bsr_rub_len(red_u, &len_d)) ) {
return res_e;
}
if ( 31 >= len_d ) {
*out = (u3_noun)ur_bsr32_any(red_u, len_d);
}
else {
c3_d byt_d = (len_d + 0x7) >> 3;
u3i_slab sab_u;
if ( 0xffffffffULL < byt_d) {
return ur_cue_meme;
}
else {
u3i_slab_init(&sab_u, 3, byt_d);
ur_bsr_bytes_any(red_u, len_d, sab_u.buf_y);
*out = u3i_slab_mint_bytes(&sab_u);
}
}
ur_dict32_put(rot_u, dic_u, bit_d, *out);
return ur_cue_good;
}
}
}
}
struct _u3_cue_xeno {
ur_dict32_t dic_u;
};
/* _cs_cue_xeno(): cue on-loom, with off-loom dictionary in handle.
*/
static u3_weak
_cs_cue_xeno(u3_cue_xeno* sil_u,
c3_d len_d,
const c3_y* byt_y)
{
ur_bsr_t red_u = {0};
ur_dict32_t* dic_u = &sil_u->dic_u;
u3a_pile pil_u;
_cue_frame_t* fam_u;
ur_cue_res_e res_e;
u3_noun ref;
// initialize stack control
//
u3a_pile_prep(&pil_u, sizeof(*fam_u));
// init bitstream-reader
//
if ( ur_cue_good != (res_e = ur_bsr_init(&red_u, len_d, byt_y)) ) {
return c3n;
}
// bit-cursor (and backreferences) must fit in 62-bit direct atoms
//
else if ( 0x7ffffffffffffffULL < len_d ) {
return c3n;
}
// advance into stream
//
res_e = _cs_cue_xeno_next(&pil_u, &red_u, dic_u, &ref);
// process cell results
//
if ( (c3n == u3a_pile_done(&pil_u))
&& (ur_cue_good == res_e) )
{
fam_u = u3a_peek(&pil_u);
do {
// f is a head-frame; stash result and read the tail from the stream
//
if ( u3_none == fam_u->ref ) {
fam_u->ref = ref;
res_e = _cs_cue_xeno_next(&pil_u, &red_u, dic_u, &ref);
fam_u = u3a_peek(&pil_u);
}
// f is a tail-frame; pop the stack and continue
//
else {
ur_root_t* rot_u = 0;
ref = u3nc(fam_u->ref, ref);
ur_dict32_put(rot_u, dic_u, fam_u->bit_d, ref);
fam_u = u3a_pop(&pil_u);
}
}
while ( (c3n == u3a_pile_done(&pil_u))
&& (ur_cue_good == res_e) );
}
if ( ur_cue_good == res_e ) {
return ref;
}
// on failure, unwind the stack and dispose of intermediate nouns
//
else if ( c3n == u3a_pile_done(&pil_u) ) {
do {
if ( u3_none != fam_u->ref ) {
u3z(fam_u->ref);
}
fam_u = u3a_pop(&pil_u);
}
while ( c3n == u3a_pile_done(&pil_u) );
}
return u3_none;
}
/* u3s_cue_xeno_init_with(): initialize a cue_xeno handle as specified.
*/
u3_cue_xeno*
u3s_cue_xeno_init_with(c3_d pre_d, c3_d siz_d)
{
u3_cue_xeno* sil_u;
u3_assert( &(u3H->rod_u) == u3R );
sil_u = c3_calloc(sizeof(*sil_u));
ur_dict32_grow((ur_root_t*)0, &sil_u->dic_u, pre_d, siz_d);
return sil_u;
}
/* u3s_cue_xeno_init(): initialize a cue_xeno handle.
*/
u3_cue_xeno*
u3s_cue_xeno_init(void)
{
return u3s_cue_xeno_init_with(ur_fib10, ur_fib11);
}
/* u3s_cue_xeno_init(): cue on-loom, with off-loom dictionary in handle.
*/
u3_weak
u3s_cue_xeno_with(u3_cue_xeno* sil_u,
c3_d len_d,
const c3_y* byt_y)
{
u3_weak som;
u3_assert( &(u3H->rod_u) == u3R );
som = _cs_cue_xeno(sil_u, len_d, byt_y);
ur_dict32_wipe(&sil_u->dic_u);
return som;
}
/* u3s_cue_xeno_init(): dispose cue_xeno handle.
*/
void
u3s_cue_xeno_done(u3_cue_xeno* sil_u)
{
ur_dict_free((ur_dict_t*)&sil_u->dic_u);
c3_free(sil_u);
}
/* u3s_cue_xeno(): cue on-loom, with off-loom dictionary.
*/
u3_weak
u3s_cue_xeno(c3_d len_d,
const c3_y* byt_y)
{
u3_cue_xeno* sil_u;
u3_weak som;
u3_assert( &(u3H->rod_u) == u3R );
sil_u = u3s_cue_xeno_init();
som = _cs_cue_xeno(sil_u, len_d, byt_y);
u3s_cue_xeno_done(sil_u);
return som;
}
/* _cs_cue_need(): bail on ur_cue_* read failures.
*/
static inline void
_cs_cue_need(ur_cue_res_e res_e)
{
if ( ur_cue_good == res_e ) {
return;
}
else {
c3_m mot_m = (ur_cue_meme == res_e) ? c3__meme : c3__exit;
u3m_bail(mot_m);
}
}
/* _cs_cue_get(): u3h_get wrapper handling allocation and refcounts.
*/
static inline u3_weak
_cs_cue_get(u3p(u3h_root) har_p, c3_d key_d)
{
u3_atom key = _cs_coin_chub(key_d);
u3_weak pro = u3h_get(har_p, key);
u3z(key);
return pro;
}
/* _cs_cue_put(): u3h_put wrapper handling allocation and refcounts.
*/
static inline u3_noun
_cs_cue_put(u3p(u3h_root) har_p, c3_d key_d, u3_noun val)
{
u3_atom key = _cs_coin_chub(key_d);
u3h_put(har_p, key, u3k(val));
u3z(key);
return val;
}
/* _cs_cue_bytes_next(): read next value from bitstream.
*/
static inline u3_noun
_cs_cue_bytes_next(u3a_pile* pil_u,
u3p(u3h_root) har_p,
ur_bsr_t* red_u)
{
while ( 1 ) {
c3_d len_d, bit_d = red_u->bits;
ur_cue_tag_e tag_e;
_cs_cue_need(ur_bsr_tag(red_u, &tag_e));
switch ( tag_e ) {
default: u3_assert(0);
case ur_jam_cell: {
_cue_frame_t* fam_u = u3a_push(pil_u);
fam_u->ref = u3_none;
fam_u->bit_d = bit_d;
continue;
}
case ur_jam_back: {
_cs_cue_need(ur_bsr_rub_len(red_u, &len_d));
if ( 62 < len_d ) {
return u3m_bail(c3__meme);
}
else {
c3_d bak_d = ur_bsr64_any(red_u, len_d);
u3_weak bak = _cs_cue_get(har_p, bak_d);
return u3x_good(bak);
}
}
case ur_jam_atom: {
u3_atom vat;
_cs_cue_need(ur_bsr_rub_len(red_u, &len_d));
if ( 31 >= len_d ) {
vat = (u3_noun)ur_bsr32_any(red_u, len_d);
}
else {
u3i_slab sab_u;
u3i_slab_init(&sab_u, 0, len_d);
ur_bsr_bytes_any(red_u, len_d, sab_u.buf_y);
vat = u3i_slab_mint_bytes(&sab_u);
}
return _cs_cue_put(har_p, bit_d, vat);
}
}
}
}
/* u3s_cue_bytes(): cue bytes onto the loom.
*/
u3_noun
u3s_cue_bytes(c3_d len_d, const c3_y* byt_y)
{
ur_bsr_t red_u = {0};
u3a_pile pil_u;
_cue_frame_t* fam_u;
u3p(u3h_root) har_p;
u3_noun ref;
// initialize stack control
//
u3a_pile_prep(&pil_u, sizeof(*fam_u));
// initialize a hash table for dereferencing backrefs
//
har_p = u3h_new();
// init bitstream-reader
//
_cs_cue_need(ur_bsr_init(&red_u, len_d, byt_y));
// bit-cursor (and backreferences) must fit in 62-bit direct atoms
//
if ( 0x7ffffffffffffffULL < len_d ) {
return u3m_bail(c3__meme);
}
// advance into stream
//
ref = _cs_cue_bytes_next(&pil_u, har_p, &red_u);
// process cell results
//
if ( c3n == u3a_pile_done(&pil_u) ) {
fam_u = u3a_peek(&pil_u);
do {
// f is a head-frame; stash result and read the tail from the stream
//
if ( u3_none == fam_u->ref ) {
fam_u->ref = ref;
ref = _cs_cue_bytes_next(&pil_u, har_p, &red_u);
fam_u = u3a_peek(&pil_u);
}
// f is a tail-frame; pop the stack and continue
//
else {
ref = u3nc(fam_u->ref, ref);
_cs_cue_put(har_p, fam_u->bit_d, ref);
fam_u = u3a_pop(&pil_u);
}
}
while ( c3n == u3a_pile_done(&pil_u) );
}
u3h_free(har_p);
return ref;
}
/* u3s_cue_atom(): cue atom.
*/
u3_noun
u3s_cue_atom(u3_atom a)
{
c3_w len_w = u3r_met(3, a);
c3_y* byt_y;
// XX assumes little-endian
//
if ( c3y == u3a_is_cat(a) ) {
byt_y = (c3_y*)&a;
}
else {
u3a_atom* vat_u = u3a_to_ptr(a);
byt_y = (c3_y*)vat_u->buf_w;
}
return u3s_cue_bytes((c3_d)len_w, byt_y);
}
/* _cs_etch_ud_size(): output length in @ud for given mpz_t.
*/
static inline size_t
_cs_etch_ud_size(mpz_t a_mp)
{
size_t len_i = mpz_sizeinbase(a_mp, 10);
return len_i + (len_i / 3); // separators
}
/* _cs_etch_ud_bytes(): atom to @ud impl.
*/
static size_t
_cs_etch_ud_bytes(mpz_t a_mp, size_t len_i, c3_y* hun_y)
{
c3_y* buf_y = hun_y + (len_i - 1);
mpz_t b_mp;
c3_w b_w;
size_t dif_i;
mpz_init2(b_mp, 10);
if ( !mpz_size(a_mp) ) {
*buf_y-- = '0';
}
else {
while ( 1 ) {
b_w = mpz_tdiv_qr_ui(a_mp, b_mp, a_mp, 1000);
u3_assert( mpz_get_ui(b_mp) == b_w ); // XX
if ( !mpz_size(a_mp) ) {
while ( b_w ) {
*buf_y-- = '0' + (b_w % 10);
b_w /= 10;
}
break;
}
*buf_y-- = '0' + (b_w % 10);
b_w /= 10;
*buf_y-- = '0' + (b_w % 10);
b_w /= 10;
*buf_y-- = '0' + (b_w % 10);
*buf_y-- = '.';
}
}
buf_y++;
u3_assert( buf_y >= hun_y ); // XX
// mpz_sizeinbase may overestimate by 1
//
{
size_t dif_i = buf_y - hun_y;
if ( dif_i ) {
len_i -= dif_i;
memmove(hun_y, buf_y, len_i);
memset(hun_y + len_i, 0, dif_i);
}
}
mpz_clear(b_mp);
return len_i;
}
/* u3s_etch_ud_smol(): c3_d to @ud
**
** =(26 (met 3 (scot %ud (dec (bex 64)))))
*/
c3_y*
u3s_etch_ud_smol(c3_d a_d, c3_y hun_y[26])
{
c3_y* buf_y = hun_y + 25;
c3_w b_w;
if ( !a_d ) {
*buf_y-- = '0';
}
else {
while ( 1 ) {
b_w = a_d % 1000;
a_d /= 1000;
if ( !a_d ) {
while ( b_w ) {
*buf_y-- = '0' + (b_w % 10);
b_w /= 10;
}
break;
}
*buf_y-- = '0' + (b_w % 10);
b_w /= 10;
*buf_y-- = '0' + (b_w % 10);
b_w /= 10;
*buf_y-- = '0' + (b_w % 10);
*buf_y-- = '.';
}
}