interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / native / turing / origsel.om

1862 lines · 29923 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note omega-mv#700 ORIGSEL evaluator: the Turing measurement of the ORIG matvec arm chooser
3note under calibration/profiles/Turing-profile-ORIGSEL-v1.1.toml, exactly as
4note native/turing/REPORT-CONTRACT.md. Concatenated after the pinned units (add32, mul32,
5note metadata, affine, fixture, world, omexp, omexpfix, select), origsel_kappa.om, then
6note origselmain.om. It reads one stream on fd 0 and writes one result on fd 1:
7note mode byte, config (8 LE words: 0 arm code 1..10 for P and M, 1 V1 bytes, 2..6 canonical
8note P2 bytes of select.om, oxrule, oxstatic, oxnearok+oxl1+oxnear, origsel_kappa.om,
9note 7 flags: bit 0 coder block present), allow block (4096), digests (96: profile, model,
10note dataset), three SI19SLCK images (M1, CSH, CUN; 11560 bytes each), V1 bytes, coder block
11note (480 bytes when flagged: 10 arms x rans, range x roundtrip, ideal_ub, coded_bits),
12note then the collected records to end of input.
13note Modes: R report, H report plus h1 lines, P TPS1 body of one arm (trailer by the glue),
14note Y TSY1 body, M model code bytes of one arm, A audit derivative C', K image comparison.
15note Arms (index, code - 1): 0 U, 1 O0, 2 FR, 3 S, 4 R, 5 M1, 6 M2, 7 A, 8 CSH, 9 CUN.
16note TQZ: 0 n, 1 burned, 2 F, 3 fresh, 4 v1fam, 5 p1r, 6 ntrain, 7 bit count, 8 9 S orders,
17note 11 records, 12 V1 records, 13 record bytes, 14 allow length, 15 cf blocks, 30 rng,
18note 32..41 dl sums, 42..51 fresh sums, 52..61 v1fam sums.
19note TQT: 3 longs (total, lb, ub) per statistic index; 90..92 the last tqst result.
20global TQK uchar 64
21global TQD uchar 96
22global TQI uchar 34680
23global TQV uchar 524288
24global TQC uchar 480
25global TQM uchar 1
26global TQO ulong 20480
27global TQN ulong 200
28global TQQ ulong 200
29global TQL ulong 16
30global TQ2 ulong 16
31global TQP ulong 16
32global TQZ ulong 64
33global TQDL ulong 2048
34global TQPL ulong 2048
35global TQVK ulong 2048
36global TQU ulong 20480
37global TQB ulong 512
38global TQBS long 5120
39global TQR long 100000
40global TQS long 10001
41global TQW uchar 16384
42global TQF uchar 512
43global TQOB uchar 131072
44global TQT long 96
45global TQDG uchar 24
46text TQH0 "ORIGSEL v1.1\n"
47text TQA0 "admit records="
48text TQA1 " admitted="
49text TQA2 " train="
50text TQA3 " validation="
51text TQA4 " heldout="
52text TQD0 "d n="
53text TQD1 " burned="
54text TQD2 " F="
55text TQD3 " fresh="
56text TQD4 " v1fam="
57text TQD5 " p1r="
58text TQR0 "arm "
59text TQR1 " p1="
60text TQR2 " p2="
61text TQR3 " dl="
62text TQR4 " rows="
63text TQT0 "t "
64text TQT1 " total="
65text TQT2 " lb="
66text TQT3 " ub="
67text TQT4 "dl "
68text TQT5 "delta M2 M1"
69text TQT6 "tcf "
70text TQT7 "nmin "
71text TQT8 "verdict "
72text TQT9 "experiment "
73text TQNM "UO0FRSRM1M2ACSHCUN"
74text TQV0 "QUALIFIED_IMPROVEMENT"
75text TQV1 "NO_IMPROVEMENT(COST_BOUND)"
76text TQV2 "NO_IMPROVEMENT"
77text TQV3 "INCONCLUSIVE(UNDERPOWERED)"
78text TQV4 "INCONCLUSIVE"
79text TQV5 "QUALIFIED_IMPROVEMENT(UNISOLATED)"
80text TQE0 "refused allow\n"
81text TQE1 "refused admission "
82text TQE2 "refused image "
83text TQK0 "K same\n"
84text TQK1 "K diff "
85text TQX0 "h1 "
86text TQX1 " rans"
87text TQX2 " range"
88text TQX3 " rt="
89text TQX4 " ideal="
90text TQX5 " delta="
91text TQX6 " band="
92text TQX7 "match"
93text TQX8 "mismatch"
94text TQX9 "ok"
95text TQY0 "fail"
96text TQY1 "h1t "
97text TQY2 " treal="
98text TQY3 " lbreal="
99text TQY4 " h1="
100text TQY5 " compression="
101text TQY6 "pass"
102text TQY7 "yes"
103text TQY8 "no"
104text TQY9 " FR"
105text TQSP " "
106text TQNL "\n"
107text TQMI "-"
108text TQCM ","
109text TQSC ";"
110text TQRS "ORS1"
111
112note ---------------------------------------------------------------- output
113
114fn tqnl to void
115w = write 1 TQNL 0 1
116end
117
118fn tqsp to void
119w = write 1 TQSP 0 1
120end
121
122fn tqsn x long to void
123note Signed decimal.
124if lt x 0
125w = write 1 TQMI 0 1
126wnum ulong neg x
127ret
128end
129wnum ulong x
130end
131
132fn tqnoff x ulong to ulong
133note Offset * 16 + length of arm x's name in TQNM.
134if eq x 0
135ret 1
136end
137if eq x 1
138ret 18
139end
140if eq x 2
141ret 50
142end
143if eq x 3
144ret 81
145end
146if eq x 4
147ret 97
148end
149if eq x 5
150ret 114
151end
152if eq x 6
153ret 146
154end
155if eq x 7
156ret 177
157end
158if eq x 8
159ret 195
160end
161ret 243
162end
163
164fn tqname x ulong to void
165v = tqnoff x
166w = write 1 TQNM shr v 4 and v 15
167end
168
169fn tqvw v ulong to void
170note Verdict word v (0..5).
171if eq v 0
172w = write 1 TQV0 0 len TQV0
173end
174if eq v 1
175w = write 1 TQV1 0 len TQV1
176end
177if eq v 2
178w = write 1 TQV2 0 len TQV2
179end
180if eq v 3
181w = write 1 TQV3 0 len TQV3
182end
183if eq v 4
184w = write 1 TQV4 0 len TQV4
185end
186if eq v 5
187w = write 1 TQV5 0 len TQV5
188end
189end
190
191fn tqout n ulong to void
192note Write TQOB[0..n) to fd 1.
193o = ulong 0
194while lt o n bound 999999
195w = write 1 TQOB o sub n o
196if le w 0
197exit 74
198end
199o = add o ulong w
200end
201end
202
203fn tqpb p ulong v ulong k ulong to void
204note k little-endian bytes of v at TQOB[p].
205for i ulong k
206put TQOB add p i uchar and shr v mul i 8 255
207end
208end
209
210note ---------------------------------------------------------------- input
211
212fn tqrdk to bool
213o = ulong 0
214while lt o 64 bound 64
215r = read 0 TQK o sub 64 o
216if le r 0
217ret false
218end
219o = add o ulong r
220end
221ret true
222end
223
224fn tqkw i ulong to ulong
225v = ulong 0
226for k ulong 8
227v = or v shl ulong get TQK add mul i 8 k mul k 8
228end
229ret v
230end
231
232fn tqrdlb to bool
233note The allow block into LB (4096 bytes); TQZ[14] = used length.
234o = ulong 0
235while lt o 4096 bound 4096
236r = read 0 LB o sub 4096 o
237if le r 0
238ret false
239end
240o = add o ulong r
241end
242n = ulong 0
243for i ulong 4096
244if ne get LB i 0
245n = add i 1
246end
247end
248put TQZ 14 n
249ret true
250end
251
252fn tqrdd to bool
253o = ulong 0
254while lt o 96 bound 96
255r = read 0 TQD o sub 96 o
256if le r 0
257ret false
258end
259o = add o ulong r
260end
261ret true
262end
263
264fn tqrdi to bool
265o = ulong 0
266while lt o 34680 bound 34680
267r = read 0 TQI o sub 34680 o
268if le r 0
269ret false
270end
271o = add o ulong r
272end
273ret true
274end
275
276fn tqrdv n ulong to bool
277o = ulong 0
278while lt o n bound 524288
279r = read 0 TQV o sub n o
280if le r 0
281ret false
282end
283o = add o ulong r
284end
285ret true
286end
287
288fn tqrdc to bool
289o = ulong 0
290while lt o 480 bound 480
291r = read 0 TQC o sub 480 o
292if le r 0
293ret false
294end
295o = add o ulong r
296end
297ret true
298end
299
300fn tqrdrb to ulong
301note Records to end of input into RB; 524289 when more than the capacity arrived.
302o = ulong 0
303while lt o 524288 bound 999999
304r = read 0 RB o sub 524288 o
305if lt r 0
306exit 74
307end
308if eq r 0
309ret o
310end
311o = add o ulong r
312end
313r = read 0 TQM 0 1
314if ne r 0
315ret 524289
316end
317ret o
318end
319
320fn tqcw a ulong i ulong to ulong
321note Coder block word i of arm a (6 words per arm: rans rt, ideal, bits, range rt, ideal, bits).
322v = ulong 0
323b = add mul a 48 mul i 8
324for k ulong 8
325v = or v shl ulong get TQC add b k mul k 8
326end
327ret v
328end
329
330fn tqiw k ulong j ulong to ulong
331note Low 32 bits of the u64 at image k offset j.
332b = add mul k 11560 j
333v = ulong 0
334for i ulong 4
335v = or v shl ulong get TQI add b i mul i 8
336end
337ret v
338end
339
340fn tqv1w q ulong i ulong to ulong
341v = ulong 0
342b = add mul q 256 mul i 8
343for k ulong 8
344v = or v shl ulong get TQV add b k mul k 8
345end
346ret v
347end
348
349fn tqv1 to void
350note V1 shape keys (M * 16384 + N) and cells.
351k = div tqkw 1 256
352put TQZ 12 k
353for q ulong k
354m = tqv1w q 5
355n = tqv1w q 6
356put TQVK q add mul m 16384 n
357if and ge m 1 ge n 1
358c = add mul oxfl m 16 oxfl n
359if lt c 512
360put TQF c 1
361end
362end
363end
364end
365
366note ---------------------------------------------------------------- record classes
367
368fn tqfit r ulong to bool
369note R1 fit set: admitted VALIDATION, pressure 0, core 0 or 1.
370if ne oxpart r 1
371ret false
372end
373if ne oxpress r 0
374ret false
375end
376ret le oxcore r 1
377end
378
379fn tqheld r ulong p ulong to bool
380if ne oxpart r 2
381ret false
382end
383if ne oxpress r p
384ret false
385end
386ret le oxcore r 1
387end
388
389fn tqburn r ulong to bool
390key = add mul oxm r 16384 oxn r
391k = get TQZ 12
392for q ulong k
393if eq get TQVK q key
394ret true
395end
396end
397ret false
398end
399
400note ---------------------------------------------------------------- orders
401
402fn tqidp to ulong
403ret 274960
404end
405
406fn tqarm pk ulong j ulong to ulong
407ret and shr pk mul 4 j 15
408end
409
410fn tqrank pk ulong a ulong to ulong
411for j ulong 5
412if eq tqarm pk j a
413ret j
414end
415end
416exit 92
417end
418
419fn tqord5 k view long 5 to ulong
420note Arms ascending by key, ties to the lower index, packed 4 bits per rank.
421used = ulong 0
422pk = ulong 0
423for j ulong 5
424b = ulong 5
425for a ulong 5
426if eq and shr used a 1 0
427if eq b 5
428b = a
429else
430if lt get k a get k b
431b = a
432end
433end
434end
435end
436used = or used shl 1 b
437pk = or pk shl b mul 4 j
438end
439ret pk
440end
441
442fn tqfr r ulong to ulong
443ra = ulong 3
444if le oxn r 8
445ra = 2
446end
447pk = ra
448p = ulong 1
449for a ulong 5
450if ne a ra
451pk = or pk shl a mul 4 p
452p = add p 1
453end
454end
455ret pk
456end
457
458fn tqqord q ulong to ulong
459note Arms of record q ascending by its measured ps.
460k = new long 5 0
461for a ulong 5
462put k a long oxcost q a
463end
464ret tqord5 k
465end
466
467fn tqstat to void
468note S: per core class 0 and 1, arms ascending by total TRAIN ps (both pressures).
469k = new long 5 0
470for c ulong 2
471for a ulong 5
472put k a 0
473end
474for q ulong get TQZ 11
475if and eq oxpart q 0 eq oxcore q c
476for a ulong 5
477put k a add get k a long oxcost q a
478end
479end
480end
481put TQZ add 8 c tqord5 k
482end
483end
484
485fn tqnear r ulong to ulong
486q = oxnear r
487if ge q 1048576
488ret tqidp
489end
490ret tqqord q
491end
492
493fn tqmlp r ulong to ulong
494k = new long 5 0
495for a ulong 5
496put k a slkey slfwd r a
497end
498ret tqord5 k
499end
500
501fn tqctx x ulong r ulong to ulong
502if or eq x 6 eq x 7
503ret tkappa r
504end
505ret 0
506end
507
508fn tqnctx x ulong to ulong
509if or eq x 6 eq x 7
510ret 4
511end
512ret 1
513end
514
515fn tqneed r ulong to bool
516if tqfit r
517ret true
518end
519if eq oxpart r 2
520ret le oxcore r 1
521end
522ret false
523end
524
525fn tqorders to void
526note Non-MLP orders for every needed record.
527for r ulong get TQZ 11
528if tqneed r
529put TQO r tqidp
530put TQO add 2048 r tqidp
531put TQO add 4096 r tqfr r
532put TQO add 6144 r get TQZ add 8 oxcore r
533put TQO add 8192 r tqnear r
534put TQO add 14336 r tqidp
535end
536end
537end
538
539fn tqimg k ulong to int
540note Install image k (0 M1, 1 CSH, 2 CUN) through sladmit, best-on-VALIDATION params.
541b = mul k 11560
542for i ulong 11560
543put SI i get TQI add b i
544end
545st = sladmit 0 11560
546if ne st 0
547ret st
548end
549if or ne get SS 0 2 ne get SS 1 32
550ret -20
551end
552if ne get SS 20 1
553ret -21
554end
555slbest
556ret 0
557end
558
559fn tqmord k ulong to void
560note MLP orders of image k into its arms (k 0: M1 and M2, 1: CSH, 2: CUN).
561for r ulong get TQZ 11
562if tqneed r
563pk = tqmlp r
564if eq k 0
565put TQO add 10240 r pk
566put TQO add 12288 r pk
567end
568if eq k 1
569put TQO add 16384 r pk
570end
571if eq k 2
572put TQO add 18432 r pk
573end
574end
575end
576end
577
578note ---------------------------------------------------------------- rows (F1)
579
580fn tqcount to void
581for i ulong 200
582put TQN i 0
583end
584for r ulong get TQZ 11
585if tqfit r
586x = oxbest r
587for a ulong 9
588v = add a 1
589c = tqctx v r
590k = tqrank get TQO add mul v 2048 r x
591i = add add mul v 20 mul c 5 k
592put TQN i add get TQN i 1
593end
594end
595end
596end
597
598fn tqrow x ulong c ulong to void
599note F1 for arm x, context c.
600b = add mul x 20 mul c 5
601n = ulong 0
602for r ulong 5
603n = add n get TQN add b r
604end
605s = ulong 0
606for r ulong 5
607nr = add mul 2 get TQN add b r 1
608dn = add mul 2 n 5
609q = add 1 div mul 65531 nr dn
610put TQQ add b r q
611s = add s q
612end
613m = ulong 0
614for r ulong 5
615if gt get TQN add b r get TQN add b m
616m = r
617end
618end
619qm = add get TQQ add b m sub 65536 s
620put TQQ add b m qm
621end
622
623fn tqrows to void
624put TQQ 0 13108
625for r ulong 4
626put TQQ add r 1 13107
627end
628for i ulong 9
629x = add i 1
630for c ulong tqnctx x
631tqrow x c
632end
633end
634end
635
636fn tqrown x ulong c ulong to ulong
637b = add mul x 20 mul c 5
638n = ulong 0
639for r ulong 5
640n = add n get TQN add b r
641end
642ret n
643end
644
645note ---------------------------------------------------------------- model codes
646
647fn tqwclr to void
648for i ulong 16384
649put TQW i 0
650end
651put TQZ 7 0
652end
653
654fn tqbit b ulong to void
655c = get TQZ 7
656if ge c 131072
657exit 93
658end
659if ne b 0
660i = div c 8
661v = ulong get TQW i
662v = or v shl 1 sub 7 rem c 8
663put TQW i uchar v
664end
665put TQZ 7 add c 1
666end
667
668fn tqbits v ulong w ulong to void
669for j ulong w
670tqbit and shr v sub sub w 1 j 1
671end
672end
673
674fn tqgam v ulong to void
675note Elias gamma of v >= 1.
676l = oxfl v
677for i ulong l
678tqbit 0
679end
680tqbits v add l 1
681end
682
683fn tqbin n ulong k ulong to ulong
684if gt k n
685ret 0
686end
687r = ulong 1
688for i ulong k
689r = div mul r sub n i add i 1
690end
691ret r
692end
693
694fn tqclg v ulong to ulong
695if le v 1
696ret 0
697end
698ret add oxfl sub v 1 1
699end
700
701fn tqenum x ulong c ulong to ulong
702note Lexicographic index of the count vector among 5-part compositions of N.
703b = add mul x 20 mul c 5
704rr = tqrown x c
705idx = ulong 0
706for i ulong 4
707ni = get TQN add b i
708for v ulong ni
709idx = add idx tqbin sub add rr sub 3 i v sub 3 i
710end
711rr = sub rr ni
712end
713ret idx
714end
715
716fn tqwrow x ulong c ulong to void
717n = tqrown x c
718tqgam add n 1
719tqbits tqenum x c tqclg tqbin add n 4 4
720end
721
722fn tqtgfr to void
723note TG1 of the incumbent: one test (word 6 <= 8), leaves arm 2 and arm 3.
724tqgam 2
725tqbits 3 3
726tqbit 0
727tqgam 9
728tqbits 2 3
729tqbits 3 3
730end
731
732fn tqtgk to void
733note TG1 of kappa: tests word 3 = 1 and word 6 <= 8, no leaves.
734tqgam 3
735tqbits 0 3
736tqbit 1
737tqgam 2
738tqbits 3 3
739tqbit 0
740tqgam 9
741end
742
743fn tqfac m ulong to ulong
744if eq m 4
745ret 24
746end
747if eq m 3
748ret 6
749end
750if eq m 2
751ret 2
752end
753ret 1
754end
755
756fn tqpidx pk ulong to ulong
757idx = ulong 0
758for i ulong 5
759ai = tqarm pk i
760cnt = ulong 0
761for j ulong 5
762if and gt j i lt tqarm pk j ai
763cnt = add cnt 1
764end
765end
766idx = add idx mul cnt tqfac sub 4 i
767end
768ret idx
769end
770
771fn tqwmlp k ulong to void
772note MLP metadata (kind 2, H 32, 705 params) and the 705 SB patterns of image k.
773tqbits 2 8
774tqbits 32 8
775tqbits 705 16
776for i ulong 705
777tqbits tqiw k add 5912 mul i 8 32
778end
779end
780
781fn tqwret to void
782note R: the stored TRAIN table.
783tqbits get TQZ 6 12
784for q ulong get TQZ 11
785if eq oxpart q 0
786tqbits oxcore q 2
787tqbits oxpress q 1
788tqbits oxlg16 oxm q 8
789tqbits oxlg16 oxn q 8
790tqbits tqpidx tqqord q 7
791end
792end
793end
794
795fn tqcode x ulong to ulong
796note Write arm x's canonical model code into TQW; returns its length in bits (P1).
797tqwclr
798if eq x 1
799tqwrow 1 0
800end
801if eq x 2
802tqtgfr
803tqwrow 2 0
804end
805if eq x 3
806tqbits tqpidx get TQZ 8 7
807tqbits tqpidx get TQZ 9 7
808tqwrow 3 0
809end
810if eq x 4
811tqwret
812tqwrow 4 0
813end
814if eq x 5
815tqwmlp 0
816tqwrow 5 0
817end
818if eq x 6
819tqwmlp 0
820tqtgk
821for c ulong 4
822tqwrow 6 c
823end
824end
825if eq x 7
826tqtgk
827for c ulong 4
828tqwrow 7 c
829end
830end
831if eq x 8
832tqwmlp 1
833tqwrow 8 0
834end
835if eq x 9
836tqwmlp 2
837tqwrow 9 0
838end
839ret get TQZ 7
840end
841
842fn tqp2b x ulong to ulong
843note Canonical P2 source bytes of arm x (config words 2..6).
844if or eq x 5 or eq x 8 eq x 9
845ret tqkw 2
846end
847if eq x 2
848ret tqkw 3
849end
850if eq x 3
851ret tqkw 4
852end
853if eq x 4
854ret tqkw 5
855end
856if eq x 6
857ret add tqkw 2 tqkw 6
858end
859if eq x 7
860ret tqkw 6
861end
862ret 0
863end
864
865fn tqlens to void
866for x ulong 10
867l = tqcode x
868put TQL x l
869put TQ2 x add l mul 8 tqp2b x
870end
871end
872
873note ---------------------------------------------------------------- ub (CODER_SPEC s.8)
874
875fn tqsq m ulong to ulong
876note floor(m * m / 2^62) for m < 2^63, through 32-bit limbs.
877a = shr m 32
878b = and m 4294967295
879p0 = mul b b
880p1 = mul a b
881p2 = mul a a
882c = shl p1 1
883mid = add shr p0 32 and c 4294967295
884lo = or and p0 4294967295 shl and mid 4294967295 32
885hi = add add p2 shr c 32 shr mid 32
886ret or shl hi 2 shr lo 62
887end
888
889fn tqub q ulong to ulong
890ip = oxfl q
891m = shl q sub 62 ip
892f = ulong 0
893for i ulong 32
894m = tqsq m
895if ge m 9223372036854775808
896f = or f shl 1 sub 31 i
897m = shr m 1
898end
899end
900l = or shl ip 32 f
901x = sub 68719476736 l
902ret shr add mul x 1000000 2147483648 32
903end
904
905fn tqq x ulong r ulong a ulong to ulong
906note q of arm a at record r under arm x.
907c = tqctx x r
908k = tqrank get TQO add mul x 2048 r a
909ret get TQQ add add mul x 20 mul c 5 k
910end
911
912note ---------------------------------------------------------------- D, blocks, sums
913
914fn tqsel to void
915note D, P1R, burned, F, fresh / v1fam counts.
916n = ulong 0
917p = ulong 0
918bu = ulong 0
919for i ulong 512
920put TQB i 0
921end
922for r ulong get TQZ 11
923if tqheld r 0
924if tqburn r
925bu = add bu 1
926else
927put TQDL n r
928n = add n 1
929put TQB oxcell r 1
930end
931end
932if tqheld r 1
933if not tqburn r
934put TQPL p r
935p = add p 1
936end
937end
938end
939f = ulong 0
940for c ulong 512
941if ne get TQB c 0
942f = add f 1
943put TQB c f
944end
945end
946put TQZ 0 n
947put TQZ 1 bu
948put TQZ 2 f
949put TQZ 5 p
950fr = ulong 0
951for t ulong n
952if eq get TQF oxcell get TQDL t 0
953fr = add fr 1
954end
955end
956put TQZ 3 fr
957put TQZ 4 sub n fr
958end
959
960fn tqntr to void
961k = ulong 0
962for q ulong get TQZ 11
963if eq oxpart q 0
964k = add k 1
965end
966end
967put TQZ 6 k
968end
969
970fn tqzadd i ulong u ulong to void
971put TQZ i add get TQZ i u
972end
973
974fn tqubs to void
975note Per-decision ub of every arm over D, dl / fresh / v1fam sums.
976for i ulong 30
977put TQZ add 32 i 0
978end
979for t ulong get TQZ 0
980r = get TQDL t
981x = oxbest r
982v1 = ne get TQF oxcell r 0
983for a ulong 10
984u = tqub tqq a r x
985put TQU add mul a 2048 t u
986tqzadd add 32 a u
987if v1
988tqzadd add 52 a u
989else
990tqzadd add 42 a u
991end
992end
993end
994end
995
996fn tqp1rs x ulong to ulong
997s = ulong 0
998for t ulong get TQZ 5
999r = get TQPL t
1000s = add s tqub tqq x r oxbest r
1001end
1002ret s
1003end
1004
1005fn tqbadd i ulong v long to void
1006put TQBS i add get TQBS i v
1007end
1008
1009fn tqbrec t ulong j ulong to void
1010for a ulong 10
1011k = add mul a 512 j
1012v = long get TQU add mul a 2048 t
1013tqbadd k v
1014end
1015end
1016
1017fn tqbsum cf bool to void
1018note Block sums of every arm: family blocks, or (cell, core) blocks when cf.
1019for i ulong 5120
1020put TQBS i 0
1021end
1022for t ulong get TQZ 0
1023r = get TQDL t
1024j = sub get TQB oxcell r 1
1025if cf
1026j = sub get TQB add mul oxcell r 2 oxcore r 1
1027end
1028tqbrec t j
1029end
1030end
1031
1032fn tqcfb to ulong
1033note (cell, core) block ids into TQB (key 2 cell + core); returns their count.
1034for i ulong 512
1035put TQB i 0
1036end
1037for t ulong get TQZ 0
1038r = get TQDL t
1039put TQB add mul oxcell r 2 oxcore r 1
1040end
1041f = ulong 0
1042for c ulong 512
1043if ne get TQB c 0
1044f = add f 1
1045put TQB c f
1046end
1047end
1048ret f
1049end
1050
1051note ---------------------------------------------------------------- bootstrap
1052
1053fn tqsm to ulong
1054note splitmix64 (wrapping), state TQZ[30].
1055s = add get TQZ 30 11400714819323198485
1056put TQZ 30 s
1057z = s
1058z = mul xor z shr z 30 13787848793156543929
1059z = mul xor z shr z 27 10723151780598845931
1060ret xor z shr z 31
1061end
1062
1063fn tqboot nb ulong to void
1064acc = new long 10 0
1065put TQZ 30 20261025
1066for i ulong 10000
1067for x ulong 10
1068put acc x 0
1069end
1070for k ulong nb
1071p = rem tqsm nb
1072for x ulong 10
1073put acc x add get acc x get TQBS add mul x 512 p
1074end
1075end
1076for x ulong 10
1077put TQR add mul i 10 x get acc x
1078end
1079end
1080end
1081
1082fn tqsift s ulong e ulong to void
1083r = s
1084c = add mul r 2 1
1085while lt c e bound 64
1086d = add c 1
1087if lt d e
1088if lt get TQS c get TQS d
1089c = d
1090end
1091end
1092if lt get TQS r get TQS c
1093t = get TQS r
1094put TQS r get TQS c
1095put TQS c t
1096r = c
1097c = add mul r 2 1
1098else
1099c = e
1100end
1101end
1102end
1103
1104fn tqhsort to void
1105s = ulong 5000
1106while gt s 0 bound 5001
1107s = sub s 1
1108tqsift s 10000
1109end
1110e = ulong 10000
1111while gt e 1 bound 10001
1112e = sub e 1
1113t = get TQS 0
1114put TQS 0 get TQS e
1115put TQS e t
1116tqsift 0 e
1117end
1118end
1119
1120fn tqst m ulong y ulong cost bool to void
1121note Statistic S_Y - S_M (- 10^6 (P1 M - P1 Y) when cost): total, lb, ub into TQT[90..92].
1122d = long 0
1123if cost
1124d = mul 1000000 sub long get TQL m long get TQL y
1125end
1126for i ulong 10000
1127va = get TQR add mul i 10 y
1128vb = get TQR add mul i 10 m
1129put TQS i sub sub va vb d
1130end
1131tqhsort
1132va = long get TQZ add 32 y
1133vb = long get TQZ add 32 m
1134put TQT 90 sub sub va vb d
1135put TQT 91 get TQS 166
1136put TQT 92 get TQS 9833
1137end
1138
1139fn tqkeep i ulong to void
1140put TQT mul i 3 get TQT 90
1141put TQT add mul i 3 1 get TQT 91
1142put TQT add mul i 3 2 get TQT 92
1143end
1144
1145fn tqtlu to void
1146w = write 1 TQT1 0 len TQT1
1147tqsn get TQT 90
1148w = write 1 TQT2 0 len TQT2
1149tqsn get TQT 91
1150w = write 1 TQT3 0 len TQT3
1151tqsn get TQT 92
1152tqnl
1153end
1154
1155fn tqmy i ulong to ulong
1156note Arm of t-line row i: M1 M2 A R CSH CUN.
1157if eq i 0
1158ret 5
1159end
1160if eq i 1
1161ret 6
1162end
1163if eq i 2
1164ret 7
1165end
1166if eq i 3
1167ret 4
1168end
1169if eq i 4
1170ret 8
1171end
1172ret 9
1173end
1174
1175fn tqyy j ulong to ulong
1176note Baseline of column j: FR U O0 S.
1177if eq j 0
1178ret 2
1179end
1180if eq j 1
1181ret 0
1182end
1183if eq j 2
1184ret 1
1185end
1186ret 3
1187end
1188
1189note ---------------------------------------------------------------- report
1190
1191fn tqarml x ulong to void
1192w = write 1 TQR0 0 len TQR0
1193tqname x
1194w = write 1 TQR1 0 len TQR1
1195wnum get TQL x
1196w = write 1 TQR2 0 len TQR2
1197wnum get TQ2 x
1198w = write 1 TQR3 0 len TQR3
1199wnum get TQZ add 32 x
1200w = write 1 TQD3 0 len TQD3
1201wnum get TQZ add 42 x
1202w = write 1 TQD4 0 len TQD4
1203wnum get TQZ add 52 x
1204w = write 1 TQD5 0 len TQD5
1205wnum tqp1rs x
1206w = write 1 TQR4 0 len TQR4
1207if eq x 0
1208w = write 1 TQMI 0 1
1209else
1210for c ulong tqnctx x
1211if gt c 0
1212w = write 1 TQSC 0 1
1213end
1214for r ulong 5
1215if gt r 0
1216w = write 1 TQCM 0 1
1217end
1218wnum get TQN add add mul x 20 mul c 5 r
1219end
1220end
1221end
1222tqnl
1223end
1224
1225fn tqhead to void
1226w = write 1 TQH0 0 len TQH0
1227w = write 1 TQA0 0 len TQA0
1228wnum get TQZ 11
1229w = write 1 TQA1 0 len TQA1
1230wnum get RC 10
1231w = write 1 TQA2 0 len TQA2
1232wnum get RC 4
1233w = write 1 TQA3 0 len TQA3
1234wnum get RC 5
1235w = write 1 TQA4 0 len TQA4
1236wnum get RC 6
1237tqnl
1238w = write 1 TQD0 0 len TQD0
1239wnum get TQZ 0
1240w = write 1 TQD1 0 len TQD1
1241wnum get TQZ 1
1242w = write 1 TQD2 0 len TQD2
1243wnum get TQZ 2
1244w = write 1 TQD3 0 len TQD3
1245wnum get TQZ 3
1246w = write 1 TQD4 0 len TQD4
1247wnum get TQZ 4
1248w = write 1 TQD5 0 len TQD5
1249wnum get TQZ 5
1250tqnl
1251end
1252
1253fn tqtlines to void
1254note t, dl and delta lines; kept in TQT by index (t: 4 i + j, dl: 24, 25, delta: 26).
1255for i ulong 6
1256m = tqmy i
1257for j ulong 4
1258y = tqyy j
1259tqst m y true
1260tqkeep add mul i 4 j
1261w = write 1 TQT0 0 len TQT0
1262tqname m
1263tqsp
1264tqname y
1265tqtlu
1266end
1267end
1268tqst 5 8 false
1269tqkeep 24
1270w = write 1 TQT4 0 len TQT4
1271tqname 5
1272tqsp
1273tqname 8
1274tqtlu
1275tqst 5 9 false
1276tqkeep 25
1277w = write 1 TQT4 0 len TQT4
1278tqname 5
1279tqsp
1280tqname 9
1281tqtlu
1282tqst 6 5 true
1283tqkeep 26
1284w = write 1 TQT5 0 len TQT5
1285tqtlu
1286end
1287
1288fn tqtcf to void
1289for i ulong 3
1290m = tqmy i
1291tqst m 2 true
1292w = write 1 TQT6 0 len TQT6
1293tqname m
1294w = write 1 TQY9 0 len TQY9
1295tqtlu
1296end
1297end
1298
1299fn tqnmin m ulong to ulong
1300v = mul get TQL m 1000000
1301q = div v 2321950
1302if ne rem v 2321950 0
1303q = add q 1
1304end
1305ret q
1306end
1307
1308fn tqlb i ulong to long
1309ret get TQT add mul i 3 1
1310end
1311
1312fn tqverd i ulong to ulong
1313note Verdict word of candidate row i (0 M1, 1 M2, 2 A).
1314m = tqmy i
1315if or lt get TQZ 2 20 lt get TQZ 0 100
1316ret 3
1317end
1318b = mul i 4
1319q = gt get TQT mul b 3 0
1320for j ulong 4
1321if le tqlb add b j 0
1322q = false
1323end
1324end
1325if lt i 2
1326if or le tqlb 24 0 le tqlb 25 0
1327q = false
1328end
1329end
1330if q
1331ret 0
1332end
1333if le get TQT add mul b 3 2 0
1334if lt get TQZ 0 tqnmin m
1335ret 1
1336end
1337ret 2
1338end
1339ret 4
1340end
1341
1342fn tqverds to void
1343for i ulong 3
1344w = write 1 TQT7 0 len TQT7
1345tqname tqmy i
1346tqsp
1347wnum tqnmin tqmy i
1348tqnl
1349end
1350anyq = false
1351alln = true
1352for i ulong 3
1353v = tqverd i
1354if eq v 0
1355anyq = true
1356end
1357if gt v 2
1358alln = false
1359end
1360if eq v 0
1361alln = false
1362end
1363w = write 1 TQT8 0 len TQT8
1364tqname tqmy i
1365tqsp
1366tqvw v
1367tqnl
1368end
1369w = write 1 TQT9 0 len TQT9
1370if anyq
1371tqvw 5
1372else
1373if alln
1374tqvw 2
1375else
1376tqvw 4
1377end
1378end
1379tqnl
1380end
1381
1382fn tqbw n ulong to ulong
1383note H1 band in micro-bits: (64 + ceil(n / 64)) 10^6.
1384c = div n 64
1385if ne rem n 64 0
1386c = add c 1
1387end
1388ret mul add 64 c 1000000
1389end
1390
1391fn tqdel a ulong k ulong to long
1392note delta of arm a under coder k (0 rans, 1 range) in micro-bits.
1393ret sub mul long tqcw a add mul k 3 2 1000000 long get TQZ add 32 a
1394end
1395
1396fn tqaok a ulong k ulong to bool
1397note Round trip, ideal agreement and band of arm a under coder k.
1398if ne tqcw a mul k 3 1
1399ret false
1400end
1401if ne tqcw a add mul k 3 1 get TQZ add 32 a
1402ret false
1403end
1404e = sub tqdel a k 448000000
1405ret le wsabs e long tqbw get TQZ 0
1406end
1407
1408fn tqh1a a ulong k ulong to void
1409w = write 1 TQX0 0 len TQX0
1410tqname a
1411if eq k 0
1412w = write 1 TQX1 0 len TQX1
1413else
1414w = write 1 TQX2 0 len TQX2
1415end
1416w = write 1 TQX3 0 len TQX3
1417wnum tqcw a mul k 3
1418w = write 1 TQX4 0 len TQX4
1419if eq tqcw a add mul k 3 1 get TQZ add 32 a
1420w = write 1 TQX7 0 len TQX7
1421else
1422w = write 1 TQX8 0 len TQX8
1423end
1424w = write 1 TQX5 0 len TQX5
1425tqsn tqdel a k
1426w = write 1 TQX6 0 len TQX6
1427if tqaok a k
1428w = write 1 TQX9 0 len TQX9
1429else
1430w = write 1 TQY0 0 len TQY0
1431end
1432tqnl
1433end
1434
1435fn tqh1t i ulong k ulong to void
1436m = tqmy i
1437dd = sub tqdel 2 k tqdel m k
1438ok = and tqaok 2 k tqaok m k
1439if gt wsabs dd mul 2 long tqbw get TQZ 0
1440ok = false
1441end
1442lbr = add tqlb mul i 4 dd
1443w = write 1 TQY1 0 len TQY1
1444tqname m
1445w = write 1 TQY9 0 len TQY9
1446if eq k 0
1447w = write 1 TQX1 0 len TQX1
1448else
1449w = write 1 TQX2 0 len TQX2
1450end
1451w = write 1 TQY2 0 len TQY2
1452tqsn add get TQT mul mul i 4 3 dd
1453w = write 1 TQY3 0 len TQY3
1454tqsn lbr
1455w = write 1 TQY4 0 len TQY4
1456if ok
1457w = write 1 TQY6 0 len TQY6
1458else
1459w = write 1 TQY0 0 len TQY0
1460end
1461w = write 1 TQY5 0 len TQY5
1462if and ok gt lbr 0
1463w = write 1 TQY7 0 len TQY7
1464else
1465w = write 1 TQY8 0 len TQY8
1466end
1467tqnl
1468end
1469
1470fn tqh1 to void
1471for a ulong 10
1472for k ulong 2
1473tqh1a a k
1474end
1475end
1476for i ulong 3
1477for k ulong 2
1478tqh1t i k
1479end
1480end
1481end
1482
1483note ---------------------------------------------------------------- TPS1, TSY1, model code bytes
1484
1485fn tqtps a ulong to void
1486note TPS1 header and records of arm a (trailer appended by the glue).
1487n = get TQZ 0
1488for i ulong 128
1489put TQOB i 0
1490end
1491tqpb 0 827543636 4
1492tqpb 4 1 2
1493tqpb 6 5 1
1494tqpb 7 16 1
1495tqpb 12 n 8
1496tqpb 20 34 4
1497for i ulong 96
1498put TQOB add 28 i get TQD i
1499end
1500for t ulong n
1501r = get TQDL t
1502p = add 128 mul t 34
1503tqpb p t 8
1504tqpb add p 8 oxseq r 8
1505tqpb add p 16 sub get TQB oxcell r 1 4
1506tqpb add p 20 65536 4
1507for x ulong 5
1508tqpb add add p 24 mul x 2 tqq a r x 2
1509end
1510end
1511tqout add 128 mul n 34
1512end
1513
1514fn tqtsy to void
1515note TSY1 header and symbols (trailer appended by the glue).
1516n = get TQZ 0
1517for i ulong 56
1518put TQOB i 0
1519end
1520tqpb 0 827937620 4
1521tqpb 4 1 2
1522tqpb 6 5 1
1523tqpb 8 n 8
1524for i ulong 32
1525put TQOB add 24 i get TQD add 64 i
1526end
1527for t ulong n
1528put TQOB add 56 t uchar oxbest get TQDL t
1529end
1530tqout add 56 n
1531end
1532
1533fn tqmcb a ulong to void
1534note Model code bytes: ORS1, arm code, code bits MSB first zero padded.
1535l = tqcode a
1536w = write 1 TQRS 0 4
1537put TQOB 0 uchar add a 1
1538tqout 1
1539n = div add l 7 8
1540o = ulong 0
1541while lt o n bound 999999
1542w = write 1 TQW o sub n o
1543if le w 0
1544exit 74
1545end
1546o = add o ulong w
1547end
1548end
1549
1550fn tqrbput p ulong v ulong to void
1551for i ulong 8
1552put RB add p i uchar and shr v mul i 8 255
1553end
1554end
1555
1556fn tqaudr r ulong b ulong to void
1557note ps of arm a of record r = 10^6 (1 + ((a - b) mod 5)).
1558for a ulong 5
1559v = mul 1000000 add 1 rem sub add a 5 b 5
1560p = add mul r 256 mul add 16 a 8
1561tqrbput p v
1562end
1563end
1564
1565fn tqaud nul bool to void
1566note A (C'): in every admitted HELD-OUT record, b = (oxbest + 1) mod 5. N (null fixture): in
1567note every admitted record, b = splitmix64(state 1) mod 5, independent of the state. Then
1568note tqaudr; every other byte unchanged.
1569put TQZ 30 1
1570for r ulong get TQZ 11
1571go = eq oxpart r 2
1572if nul
1573go = lt oxpart r 3
1574end
1575if go
1576b = rem add oxbest r 1 5
1577if nul
1578b = rem tqsm 5
1579end
1580tqaudr r b
1581end
1582end
1583o = ulong 0
1584n = get TQZ 13
1585while lt o n bound 999999
1586w = write 1 RB o sub n o
1587if le w 0
1588exit 74
1589end
1590o = add o ulong w
1591end
1592end
1593
1594fn tqfnvm to int
1595note F: FNV-1a 64 of the rest of the input (at most 524288 bytes) as 8 little-endian bytes,
1596note so the glue never does shell arithmetic on a 64-bit value.
1597n = tqrdrb
1598if gt n 524288
1599ret 68
1600end
1601tqpb 0 oxfnv n 8
1602tqout 8
1603ret 0
1604end
1605
1606fn tqcmp to int
1607note K: two images and an FNV word. Same outside bytes 104..111 and the trailer; the second
1608note image's binding word equals the given FNV and its trailer equals FNV-1a of its prefix.
1609o = ulong 0
1610while lt o 23128 bound 23128
1611r = read 0 SI o sub 23128 o
1612if le r 0
1613ret 68
1614end
1615o = add o ulong r
1616end
1617for i ulong 11560
1618if or lt i 104 and ge i 112 lt i 11552
1619if ne get SI i get SI add 11560 i
1620w = write 1 TQK1 0 len TQK1
1621wnum i
1622tqnl
1623ret 1
1624end
1625end
1626end
1627fv = siget 23120
1628if ne siget 11664 fv
1629w = write 1 TQK1 0 len TQK1
1630wnum 104
1631tqnl
1632ret 1
1633end
1634if ne siget 23112 sifnv 11560 11552
1635w = write 1 TQK1 0 len TQK1
1636wnum 11552
1637tqnl
1638ret 1
1639end
1640w = write 1 TQK0 0 len TQK0
1641ret 0
1642end
1643
1644note ---------------------------------------------------------------- driver
1645
1646fn tqload to int
1647note Read the stream (after the mode byte) and admit; 0 ok, else the exit code.
1648if not tqrdk
1649ret 68
1650end
1651nv = tqkw 1
1652if or gt nv 524288 ne rem nv 256 0
1653ret 68
1654end
1655if not tqrdlb
1656ret 68
1657end
1658if not tqrdd
1659ret 68
1660end
1661if not tqrdi
1662ret 68
1663end
1664if gt nv 0
1665if not tqrdv nv
1666ret 68
1667end
1668end
1669if ne and tqkw 7 1 0
1670if not tqrdc
1671ret 68
1672end
1673end
1674n = tqrdrb
1675if gt n 524288
1676ret 68
1677end
1678put TQZ 13 n
1679st = oxallow get TQZ 14 oxfnv n
1680if lt st 0
1681w = write 1 TQE0 0 len TQE0
1682ret 67
1683end
1684st = oxadm2 n
1685if ne st 0
1686w = write 1 TQE1 0 len TQE1
1687wnum ulong neg long st
1688tqnl
1689ret 66
1690end
1691put TQZ 11 get RC 0
1692tqv1
1693ret 0
1694end
1695
1696fn tqprep to int
1697note Everything before output: orders, rows, code lengths, D, ub, blocks.
1698tqntr
1699tqstat
1700tqorders
1701for k ulong 3
1702st = tqimg k
1703if ne st 0
1704w = write 1 TQE2 0 len TQE2
1705wnum k
1706tqsp
1707tqsn long st
1708tqnl
1709ret 65
1710end
1711tqmord k
1712end
1713tqcount
1714tqrows
1715tqlens
1716tqsel
1717tqubs
1718ret 0
1719end
1720
1721fn tqsynq t ulong r ulong to ulong
1722note Count of rank r in the synthetic coder row of position t.
1723ret rem add mul t 7 mul r 13 50
1724end
1725
1726fn tqsyn sym bool to void
1727note Q / W: a synthetic K = 5 stream of n = config word 0 positions (coder band
1728note qualification, no record involved): row t = F1 of counts (7 t + 13 r) mod 50, symbol t =
1729note splitmix64(state 7) mod 5. Q writes the TPS1 body, W the TSY1 body.
1730n = tqkw 0
1731put TQZ 30 7
1732for i ulong 128
1733put TQOB i 0
1734end
1735if sym
1736tqpb 0 827937620 4
1737tqpb 4 1 2
1738tqpb 6 5 1
1739tqpb 8 n 8
1740for t ulong n
1741put TQOB add 56 t uchar rem tqsm 5
1742end
1743tqout add 56 n
1744ret
1745end
1746tqpb 0 827543636 4
1747tqpb 4 1 2
1748tqpb 6 5 1
1749tqpb 7 16 1
1750tqpb 12 n 8
1751tqpb 20 34 4
1752for t ulong n
1753for r ulong 5
1754put TQN r tqsynq t r
1755end
1756tqrow 0 0
1757p = add 128 mul t 34
1758tqpb p t 8
1759tqpb add p 8 t 8
1760tqpb add p 20 65536 4
1761for r ulong 5
1762tqpb add add p 24 mul r 2 get TQQ r 2
1763end
1764end
1765tqout add 128 mul n 34
1766end
1767
1768fn tqband to int
1769note B: config words 0 n, 1 ideal_ub, 2 coded bits, 3 round trip (1 ok); band of h1_bridge.
1770if not tqrdk
1771ret 68
1772end
1773d = sub mul long tqkw 2 1000000 long tqkw 1
1774ok = and eq tqkw 3 1 le wsabs sub d 448000000 long tqbw tqkw 0
1775w = write 1 TQX5 0 len TQX5
1776tqsn d
1777w = write 1 TQX6 0 len TQX6
1778if ok
1779w = write 1 TQX9 0 len TQX9
1780tqnl
1781ret 0
1782end
1783w = write 1 TQY0 0 len TQY0
1784tqnl
1785ret 1
1786end
1787
1788fn tqmok m ulong to bool
1789note Modes R H P Y M A N Q W.
1790if or eq m 82 or eq m 72 or eq m 80 eq m 89
1791ret true
1792end
1793if or eq m 77 or eq m 65 eq m 78
1794ret true
1795end
1796ret or eq m 81 eq m 87
1797end
1798
1799fn tqrun m ulong to int
1800if not tqmok m
1801ret 64
1802end
1803st = tqload
1804if ne st 0
1805ret st
1806end
1807if or eq m 65 eq m 78
1808tqaud eq m 78
1809ret 0
1810end
1811if or eq m 81 eq m 87
1812if or lt tqkw 0 1 gt tqkw 0 2048
1813ret 68
1814end
1815tqsyn eq m 87
1816ret 0
1817end
1818st = tqprep
1819if ne st 0
1820ret st
1821end
1822if eq m 80
1823a = tqkw 0
1824if or lt a 1 gt a 10
1825ret 68
1826end
1827tqtps sub a 1
1828ret 0
1829end
1830if eq m 89
1831tqtsy
1832ret 0
1833end
1834if eq m 77
1835a = tqkw 0
1836if or lt a 1 gt a 10
1837ret 68
1838end
1839tqmcb sub a 1
1840ret 0
1841end
1842if and eq m 72 eq and tqkw 7 1 0
1843ret 68
1844end
1845tqhead
1846for x ulong 10
1847tqarml x
1848end
1849tqbsum false
1850tqboot get TQZ 2
1851tqtlines
1852nb = tqcfb
1853put TQZ 15 nb
1854tqbsum true
1855tqboot nb
1856tqtcf
1857tqverds
1858if eq m 72
1859tqh1
1860end
1861ret 0
1862end