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 / gpu / nvidia / coder / erc.om

1380 lines · 24210 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4note
5note om/coder/erc.om: ERC1, a lossless row-addressable coder for E4M3 weight matrices (ledger L1331).
6note One static 14-bit frequency model per matrix, one independent byte-wise rANS stream per matrix
7note row. Byte format: om/coder/FORMAT.md. Program mode, host only, no GPU word is used.
8note
9note Build (compiler = the omc named by the repository PIN):
10note omc < om/coder/erc.om > erc
11note
12note CLI (exit 0 ok, 2 usage, 3 refused by a decoder check, 4 I/O error):
13note erc encode RAW OFFSET ROWS COLS OUT code ROWS*COLS bytes of RAW starting at byte OFFSET
14note erc decode IN OUT decode every row, check the matrix CRC, write the matrix
15note erc decode-row IN ROW OUT decode one row alone: reads the header, the tables and that
16note row's stream, nothing else of the file
17note erc info IN check the header and print its fields
18note erc bench IN REPS decode rate in memory, one thread, three modes
19note erc flipscan IN flip every stream bit once, decode that row alone
20note erc fixcrc IN recompute the header CRC in place (test aid for forged files)
21note erc fixrows IN recompute the row CRC-16s and the header CRC (test aid)
22note erc sum IN CRC-32 and CRC-16 of a whole file (known-answer tests)
23note
24note Run-time pools hold the coded image and the row buffers, so this program caps no matrix size;
25note the limits are the field widths: rows, cols and the stream area are u32 (FORMAT.md).
26
27global TAB uint 16384
28global FR ulong 256
29global CRT ulong 256
30global CRH ulong 256
31global CF ulong 16
32global CM ulong 257
33global CNT ulong 256
34global CHK uchar 65536
35global IMG uchar *
36global RB uchar *
37global SC uchar *
38global PA uchar 4096
39global PB uchar 4096
40global PN uchar 64
41global OB uchar 4096
42global OL ulong 1
43global FS ulong 32
44
45note CF slots: 0 rows, 1 cols, 2 stream base, 3 offset table base, 4 matrix crc (stored),
46note 5 image length, 6 write position (encoder), 7 raw offset, 8 raw bytes, 9 raw path length,
47note 10 output path length, 11 raw descriptor, 12 row CRC-16 table base
48
49text ERRN "OK TRUNC MAGIC VERSION FIELD LENGTH HDRCRC FREQ OFFSET STATE EXHAUST LEFTOVERFINAL MATCRC ROWCRC ALLOC "
50text TREF "erc: refused "
51text TROW " row "
52text TIOE "erc: io error "
53text TUSE "usage: erc encode RAW OFFSET ROWS COLS OUT | decode IN OUT | decode-row IN ROW OUT | info IN | bench IN REPS | flipscan IN | fixcrc IN | fixrows IN | sum IN\n"
54text TBAD "erc: bad argument\n"
55text TSHORT "erc: raw file is shorter than OFFSET + ROWS * COLS\n"
56text TWIDE "erc: refused: the stream area is longer than a u32 offset can address\n"
57text WENC "encode"
58text WDEC "decode"
59text WDROW "decode-row"
60text WINFO "info"
61text WBEN "bench"
62text WFLIP "flipscan"
63text WFIX "fixcrc"
64text WFIXR "fixrows"
65text WSUM "sum"
66text KS32 "crc32="
67text KS16 " crc16="
68text KROWS "rows="
69text KCOLS " cols="
70text KRAW " raw_bytes="
71text KCODED " coded_bytes="
72text KSTR " stream_bytes="
73text KCRC " matrix_crc32="
74text KOK "OK\n"
75text KREPS " reps="
76text KNS " ns="
77text KBPS " bytes_per_s="
78text KMODE "mode="
79text MDEC "rans_only"
80text MDRC "row_crc16_and_rans"
81text MDCRC "row_crc16_rans_matrix_crc32"
82text KFLIPS "flips="
83text KRU " rans_only_undetected="
84text KRW " rans_only_undetected_wrong_output="
85text KCU " checked_undetected="
86text KCW " checked_undetected_wrong_output="
87text KCODE " code_"
88text KEQ "="
89text KFIX "header_crc32="
90text KFR " first_undetected_row="
91text KFB " byte_in_stream="
92text KFT " bit="
93
94note ---------------------------------------------------------------- printing
95
96fn pbyte b ulong to void
97n = get OL 0
98if lt n 4096
99put OB n uchar b
100put OL 0 add n 1
101end
102end
103
104fn ptext t view uchar * to void
105for i ulong len t bound 4096
106pbyte ulong get t i
107end
108end
109
110fn pdec v ulong to void
111d = new uchar 24 0
112k = ulong 0
113x = v
114while true bound 21
115put d k uchar add rem x 10 48
116k = add k 1
117x = div x 10
118if eq x 0
119break
120end
121end
122while gt k 0 bound 21
123k = sub k 1
124pbyte ulong get d k
125end
126end
127
128fn phex v ulong to void
129for i ulong 8
130h = and shr v sub 28 mul i 4 15
131if lt h 10
132pbyte add h 48
133else
134pbyte add h 87
135end
136end
137end
138
139fn pflush fd int to void
140n = get OL 0
141o = ulong 0
142while lt o n bound 4097
143k = write fd OB o sub n o
144if le k 0
145break
146end
147o = add o ulong k
148end
149put OL 0 0
150end
151
152fn prefuse code ulong row ulong hasrow ulong to void
153ptext TREF
154for j ulong 8
155b = ulong get ERRN add mul code 8 j
156if ne b 32
157pbyte b
158end
159end
160if ne hasrow 0
161ptext TROW
162pdec row
163end
164pbyte 10
165pflush 2
166end
167
168fn pio code long to void
169ptext TIOE
170if lt code 0
171pbyte 45
172pdec ulong neg code
173else
174pdec ulong code
175end
176pbyte 10
177pflush 2
178end
179
180note ---------------------------------------------------------------- little-endian fields of IMG
181
182fn ld32 o ulong to ulong
183a = ulong get IMG o
184b = ulong get IMG add o 1
185c = ulong get IMG add o 2
186d = ulong get IMG add o 3
187ret or or a shl b 8 or shl c 16 shl d 24
188end
189
190fn st32 o ulong v ulong to void
191put IMG o uchar and v 255
192put IMG add o 1 uchar and shr v 8 255
193put IMG add o 2 uchar and shr v 16 255
194put IMG add o 3 uchar and shr v 24 255
195end
196
197fn st16 o ulong v ulong to void
198put IMG o uchar and v 255
199put IMG add o 1 uchar and shr v 8 255
200end
201
202fn ld16 o ulong to ulong
203a = ulong get IMG o
204b = ulong get IMG add o 1
205ret or a shl b 8
206end
207
208note ---------------------------------------------------------------- CRC-32 (IEEE 802.3, reflected 0xEDB88320)
209note and CRC-16/CCITT-FALSE (poly 0x1021, init 0xFFFF, MSB first, no final xor; "123456789" gives 0x29b1)
210
211fn crcinit to void
212for n ulong 256
213c = n
214for k ulong 8
215if eq and c 1 1
216c = xor shr c 1 3988292384
217else
218c = shr c 1
219end
220end
221put CRT n c
222h = shl n 8
223for k ulong 8
224if ne and h 32768 0
225h = and xor shl h 1 4129 65535
226else
227h = and shl h 1 65535
228end
229end
230put CRH n h
231end
232end
233
234note CRC-16 of IMG[a, b).
235fn crc16img a ulong b ulong to ulong
236x = ulong 65535
237i = a
238while lt i b bound 9223372036854775807
239x = xor and shl x 8 65535 get CRH and xor shr x 8 ulong get IMG i 255
240i = add i 1
241end
242ret x
243end
244
245fn crcimg c ulong a ulong b ulong to ulong
246x = c
247i = a
248while lt i b bound 9223372036854775807
249x = xor shr x 8 get CRT and xor x ulong get IMG i 255
250i = add i 1
251end
252ret x
253end
254
255fn crcrb c ulong n ulong to ulong
256x = c
257i = ulong 0
258while lt i n bound 9223372036854775807
259x = xor shr x 8 get CRT and xor x ulong get RB i 255
260i = add i 1
261end
262ret x
263end
264
265note ---------------------------------------------------------------- frequency model
266
267note Same rule as the measurement prototype rowrans.c: f = floor(count * 2^14 / n), 1 when a present
268note symbol rounds to 0, then +-1 on the first largest symbol until the sum is 2^14.
269fn normalize n ulong to void
270tot = ulong 0
271for s ulong 256
272c = get CNT s
273f = div shl c 14 n
274if ne c 0
275if eq f 0
276f = ulong 1
277end
278end
279put FR s f
280tot = add tot f
281end
282while ne tot 16384 bound 100000
283b = ulong 0
284for s ulong 255
285if gt get FR add s 1 get FR b
286b = add s 1
287end
288end
289if gt tot 16384
290put FR b sub get FR b 1
291tot = sub tot 1
292else
293put FR b add get FR b 1
294tot = add tot 1
295end
296end
297end
298
299note The decode table: slot k of the 2^14 slots maps to symbol | (k - cum[symbol]) << 8.
300fn buildtab to void
301k = ulong 0
302for s ulong 256
303f = get FR s
304j = ulong 0
305while lt j f bound 16384
306put TAB k uint or s shl j 8
307k = add k 1
308j = add j 1
309end
310end
311end
312
313note ---------------------------------------------------------------- encoder
314
315note Encode the cols bytes in RB as one stream into SC; return where it starts (it ends at 2 * cols + 8).
316fn erow to ulong
317c = get CF 1
318p = add mul 2 c 8
319x = ulong 8388608
320i = c
321while gt i 0 bound 9223372036854775807
322i = sub i 1
323s = ulong get RB i
324f = get FR s
325xm = shl f 17
326while ge x xm bound 3
327p = sub p 1
328put SC p uchar and x 255
329x = shr x 8
330end
331x = add add shl div x f 14 rem x f get CM s
332end
333p = sub p 4
334put SC p uchar and x 255
335put SC add p 1 uchar and shr x 8 255
336put SC add p 2 uchar and shr x 16 255
337put SC add p 3 uchar and shr x 24 255
338ret p
339end
340
341note Append SC[p .. 2 * cols + 8) to IMG at CF[6]. 0, or the negative errno when IMG cannot grow.
342fn append p ulong to long
343n = sub add mul 2 get CF 1 8 p
344w = get CF 6
345l = len IMG
346if gt add w n l
347t = add w n
348if lt t mul 2 l
349t = mul 2 l
350end
351r = enlarge IMG t
352if lt r 0
353ret r
354end
355end
356for j ulong n
357put IMG add w j get SC add p j
358end
359put CF 6 add w n
360ret 0
361end
362
363note Count the kk bytes in CHK and fold them into the running CRC state x.
364fn cchunk kk ulong c ulong to ulong
365x = c
366for j ulong kk bound 65536
367b = ulong get CHK j
368put CNT b add get CNT b 1
369x = xor shr x 8 get CRT and xor x b 255
370end
371ret x
372end
373
374fn cpass fd int n ulong c ulong to long
375left = n
376x = c
377while gt left 0 bound 9223372036854775807
378m = ulong 65536
379if lt left m
380m = left
381end
382k = read fd CHK 0 m
383if le k 0
384ret -1
385end
386x = cchunk ulong k x
387left = sub left ulong k
388end
389put CF 4 x
390ret 0
391end
392
393fn rdrow fd int n ulong to long
394o = ulong 0
395while lt o n bound 9223372036854775807
396k = read fd RB o sub n o
397if le k 0
398ret -1
399end
400o = add o ulong k
401end
402ret 0
403end
404
405fn wrall fd int n ulong to long
406o = ulong 0
407while lt o n bound 9223372036854775807
408k = write fd RB o sub n o
409if le k 0
410ret -1
411end
412o = add o ulong k
413end
414ret 0
415end
416
417fn wrimg fd int n ulong to long
418o = ulong 0
419while lt o n bound 9223372036854775807
420k = write fd IMG o sub n o
421if le k 0
422ret -1
423end
424o = add o ulong k
425end
426ret 0
427end
428
429note Read n bytes from the current file position into IMG[o ..]. 0, or -1 when the file ends early.
430fn rdat fd int o ulong n ulong to long
431x = ulong 0
432while lt x n bound 9223372036854775807
433k = read fd IMG add o x sub n x
434if le k 0
435ret -1
436end
437x = add x ulong k
438end
439ret 0
440end
441
442fn argnum idx ulong to ulong
443k = argread idx PN 0 64
444if le k 0
445exit 2
446end
447v = ulong 0
448n = ulong k
449if gt n 19
450exit 2
451end
452for i ulong n bound 64
453d = ulong get PN i
454if or lt d 48 gt d 57
455exit 2
456end
457v = add mul v 10 sub d 48
458end
459ret v
460end
461
462fn argpath idx ulong b ulong to long
463k = long 0
464if eq b 0
465k = argread idx PA 0 4096
466else
467k = argread idx PB 0 4096
468end
469if le k 0
470exit 2
471end
472ret k
473end
474
475fn argis idx ulong t view uchar * to bool
476k = argread idx PN 0 64
477if ne ulong k len t
478ret false
479end
480for i ulong len t bound 64
481if ne get PN i get t i
482ret false
483end
484end
485ret true
486end
487
488note Arguments are parsed into CF: 0 rows, 1 cols, 7 raw offset, 8 raw bytes, 9 raw path length,
489note 10 output path length, 11 raw descriptor.
490fn encargs to int
491put CF 9 ulong argpath 2 0
492put CF 7 argnum 3
493put CF 0 argnum 4
494put CF 1 argnum 5
495put CF 10 ulong argpath 6 1
496rows = get CF 0
497cols = get CF 1
498if or eq rows 0 eq cols 0
499ptext TBAD
500pflush 2
501ret 2
502end
503if or gt rows 4294967295 gt cols 4294967295
504ptext TBAD
505pflush 2
506ret 2
507end
508note A file offset is a signed 64-bit value: OFFSET and ROWS * COLS stay below 2^62 so that their sum fits.
509if or gt get CF 7 4611686018427387903 gt mul rows cols 4611686018427387903
510ptext TBAD
511pflush 2
512ret 2
513end
514put CF 8 mul rows cols
515ret 0
516end
517
518note Open the raw file, count the symbols and the matrix CRC, build the model. 0 or an exit status.
519fn encmodel to int
520fd = int fopen PA 0 get CF 9 int 0 int 0
521if lt fd 0
522pio long fd
523ret 4
524end
525n = get CF 8
526off = get CF 7
527sz = fseek fd long 0 int 2
528if lt sz long add off n
529ptext TSHORT
530pflush 2
531ret 4
532end
533k = fseek fd long off int 0
534for s ulong 256
535put CNT s 0
536end
537if ne cpass fd n 4294967295 0
538pio -5
539ret 4
540end
541put CF 4 xor get CF 4 4294967295
542normalize n
543cum = ulong 0
544for s ulong 256
545put CM s cum
546cum = add cum get FR s
547end
548put CM 256 cum
549k = fseek fd long off int 0
550put CF 11 ulong fd
551ret 0
552end
553
554note Position of the header CRC: 32 header + 512 table + 4 (rows + 1) offsets + 2 rows CRC-16s.
555fn hcpos rows ulong to ulong
556ret add 548 mul 6 rows
557end
558
559note Encode every row of the raw file into IMG after the table area. 0 or an exit status.
560fn encrows to int
561fd = int get CF 11
562rows = get CF 0
563cols = get CF 1
564sb = add hcpos rows 4
565put CF 2 sb
566put CF 6 sb
567put CF 12 add 548 mul 4 rows
568r = enlarge IMG sb
569if lt r 0
570pio r
571ret 4
572end
573r = enlarge RB cols
574if lt r 0
575pio r
576ret 4
577end
578r = enlarge SC add mul 2 cols 16
579if lt r 0
580pio r
581ret 4
582end
583for rr ulong rows bound 4294967296
584if ne rdrow fd cols 0
585pio -5
586ret 4
587end
588s0 = get CF 6
589r = append erow
590if lt r 0
591pio r
592ret 4
593end
594s1 = get CF 6
595w = sub s1 sb
596if gt w 4294967295
597ptext TWIDE
598pflush 2
599ret 3
600end
601st32 add 544 mul 4 add rr 1 w
602st16 add get CF 12 mul 2 rr crc16img s0 s1
603end
604k = fclose fd
605ret 0
606end
607
608note Write the header, the table, the header CRC and the file. 0 or an exit status.
609fn encwrite to int
610rows = get CF 0
611tl = get CF 6
612put IMG 0 69
613put IMG 1 82
614put IMG 2 67
615put IMG 3 49
616st16 4 1
617st16 6 0
618st32 8 rows
619st32 12 get CF 1
620st32 16 14
621st32 20 get CF 4
622st32 24 and tl 4294967295
623st32 28 shr tl 32
624for s ulong 256
625st16 add 32 mul 2 s get FR s
626end
627st32 544 0
628hc = hcpos rows
629st32 hc xor crcimg 4294967295 0 hc 4294967295
630fo = int fopen PB 0 get CF 10 int 577 int 420
631if lt fo 0
632pio long fo
633ret 4
634end
635if ne wrimg fo tl 0
636pio -5
637ret 4
638end
639k = fclose fo
640ptext KROWS
641pdec rows
642ptext KCOLS
643pdec get CF 1
644ptext KRAW
645pdec get CF 8
646ptext KCODED
647pdec tl
648ptext KSTR
649pdec sub tl get CF 2
650ptext KCRC
651phex get CF 4
652pbyte 10
653pflush 1
654ret 0
655end
656
657fn encode to int
658e = encargs
659if ne e 0
660ret e
661end
662e = encmodel
663if ne e 0
664ret e
665end
666e = encrows
667if ne e 0
668ret e
669end
670ret encwrite
671end
672
673note ---------------------------------------------------------------- decoder
674
675note Read the whole file at PA into IMG; return its length or a negative errno.
676fn loadimg la ulong to long
677fd = int fopen PA 0 la int 0 int 0
678if lt fd 0
679ret long fd
680end
681sz = fseek fd long 0 int 2
682k = fseek fd long 0 int 0
683if lt sz 0
684ret sz
685end
686r = enlarge IMG ulong sz
687if lt r 0
688ret r
689end
690if ne rdat fd 0 ulong sz 0
691ret -5
692end
693k = fclose fd
694put CF 5 ulong sz
695ret sz
696end
697
698note Open the file at PA and read only what decode-row needs first: the header, the tables and the
699note header CRC. The descriptor stays in CF[13]. Returns the file length or a negative errno.
700fn loadhead la ulong to long
701fd = int fopen PA 0 la int 0 int 0
702if lt fd 0
703ret long fd
704end
705sz = fseek fd long 0 int 2
706k = fseek fd long 0 int 0
707if lt sz 0
708ret sz
709end
710n = ulong sz
711r = enlarge IMG n
712if lt r 0
713ret r
714end
715put CF 5 n
716put CF 13 ulong fd
717m = n
718if gt m 32
719m = ulong 32
720end
721if ne rdat fd 0 m 0
722ret -5
723end
724if lt n 32
725ret sz
726end
727pre = add hcpos ld32 8 4
728if gt pre n
729pre = n
730end
731if ne rdat fd 32 sub pre 32 0
732ret -5
733end
734ret sz
735end
736
737note Read the stream of row r (checked by openimg) into IMG, and close the file.
738fn loadstream r ulong to long
739fd = int get CF 13
740sb = get CF 2
741a = add sb ld32 add 544 mul 4 r
742b = add sb ld32 add 544 mul 4 add r 1
743k = fseek fd long a int 0
744if ne rdat fd a sub b a 0
745ret -5
746end
747k = fclose fd
748ret 0
749end
750
751note Check the header, the frequency table and the row offsets; build the decode tables. 0 or a code.
752fn openhdr to ulong
753n = get CF 5
754if lt n 32
755ret 1
756end
757if ne get IMG 0 69
758ret 2
759end
760if ne get IMG 1 82
761ret 2
762end
763if ne get IMG 2 67
764ret 2
765end
766if ne get IMG 3 49
767ret 2
768end
769if ne ulong get IMG 4 1
770ret 3
771end
772if ne get IMG 5 0
773ret 3
774end
775if ne get IMG 6 0
776ret 3
777end
778if ne get IMG 7 0
779ret 3
780end
781rows = ld32 8
782cols = ld32 12
783if ne ld32 16 14
784ret 4
785end
786if eq rows 0
787ret 4
788end
789if eq cols 0
790ret 4
791end
792hc = hcpos rows
793sb = add hc 4
794if lt n sb
795ret 1
796end
797tl = or ld32 24 shl ld32 28 32
798if ne tl n
799ret 5
800end
801crc = xor crcimg 4294967295 0 hc 4294967295
802if ne crc ld32 hc
803ret 6
804end
805put CF 0 rows
806put CF 1 cols
807put CF 2 sb
808put CF 3 544
809put CF 12 add 548 mul 4 rows
810put CF 4 ld32 20
811ret 0
812end
813
814note The frequency table and the row offsets, after the header CRC has passed. 0 or a code.
815fn opentab to ulong
816rows = get CF 0
817sb = get CF 2
818tot = ulong 0
819for s ulong 256
820f = ld16 add 32 mul 2 s
821put FR s f
822tot = add tot f
823end
824if ne tot 16384
825ret 7
826end
827if ne ld32 544 0
828ret 8
829end
830prev = ulong 0
831for r ulong rows bound 4294967296
832nx = ld32 add 544 mul 4 add r 1
833if lt nx add prev 4
834ret 8
835end
836prev = nx
837end
838if ne prev sub get CF 5 sb
839ret 8
840end
841buildtab
842ret 0
843end
844
845fn openimg to ulong
846e = openhdr
847if ne e 0
848ret e
849end
850ret opentab
851end
852
853note Decode row r alone into RB[0..cols) with the rANS decoder only. 0 or a code (9 STATE, 10 EXHAUST,
854note 11 LEFTOVER, 12 FINAL).
855fn drowcore r ulong to ulong
856c = get CF 1
857sb = get CF 2
858ob = get CF 3
859p = add sb ld32 add ob mul 4 r
860e = add sb ld32 add ob mul 4 add r 1
861x = ld32 p
862p = add p 4
863if ge x 2147483648
864ret 9
865end
866if lt x 8388608
867ret 9
868end
869i = ulong 0
870while lt i c bound 9223372036854775807
871en = ulong get TAB and x 16383
872x = add mul get FR and en 255 shr x 14 shr en 8
873put RB i uchar and en 255
874while lt x 8388608 bound 3
875if ge p e
876ret 10
877end
878x = or shl x 8 ulong get IMG p
879p = add p 1
880end
881i = add i 1
882end
883if ne p e
884ret 11
885end
886if ne x 8388608
887ret 12
888end
889ret 0
890end
891
892note Decode row r alone: its CRC-16 first, then the rANS decoder. 0 or a code (14 ROWCRC, or drowcore's).
893fn drow r ulong to ulong
894sb = get CF 2
895ob = get CF 3
896a = add sb ld32 add ob mul 4 r
897b = add sb ld32 add ob mul 4 add r 1
898if ne crc16img a b ld16 add get CF 12 mul 2 r
899ret 14
900end
901ret drowcore r
902end
903
904fn setupdec to ulong
905e = openimg
906if ne e 0
907ret e
908end
909r = enlarge RB get CF 1
910if lt r 0
911ret 15
912end
913ret 0
914end
915
916fn decode to int
917li = argpath 2 0
918lo = argpath 3 1
919sz = loadimg ulong li
920if lt sz 0
921pio sz
922ret 4
923end
924e = setupdec
925if ne e 0
926prefuse e 0 0
927ret 3
928end
929rows = get CF 0
930cols = get CF 1
931fo = int fopen PB 0 ulong lo int 577 int 420
932if lt fo 0
933pio long fo
934ret 4
935end
936crc = ulong 4294967295
937bad = ulong 0
938for r ulong rows bound 4294967296
939e = drow r
940if ne e 0
941prefuse e r 1
942bad = ulong 1
943break
944end
945crc = crcrb crc cols
946if ne wrall fo cols 0
947pio -5
948bad = ulong 2
949break
950end
951end
952k = fclose fo
953if eq bad 1
954k = unlink PB 0 ulong lo
955ret 3
956end
957if eq bad 2
958ret 4
959end
960if ne xor crc 4294967295 get CF 4
961prefuse 13 0 0
962k = unlink PB 0 ulong lo
963ret 3
964end
965ptext KROWS
966pdec rows
967ptext KCOLS
968pdec cols
969ptext KCRC
970phex get CF 4
971pbyte 10
972ptext KOK
973pflush 1
974ret 0
975end
976
977fn decoderow to int
978li = argpath 2 0
979rr = argnum 3
980lo = argpath 4 1
981sz = loadhead ulong li
982if lt sz 0
983pio sz
984ret 4
985end
986e = setupdec
987if ne e 0
988prefuse e 0 0
989ret 3
990end
991if ge rr get CF 0
992ptext TBAD
993pflush 2
994ret 2
995end
996if ne loadstream rr 0
997pio -5
998ret 4
999end
1000e = drow rr
1001if ne e 0
1002prefuse e rr 1
1003ret 3
1004end
1005fo = int fopen PB 0 ulong lo int 577 int 420
1006if lt fo 0
1007pio long fo
1008ret 4
1009end
1010if ne wrall fo get CF 1 0
1011pio -5
1012ret 4
1013end
1014k = fclose fo
1015ret 0
1016end
1017
1018fn info to int
1019li = argpath 2 0
1020sz = loadimg ulong li
1021if lt sz 0
1022pio sz
1023ret 4
1024end
1025e = openimg
1026if ne e 0
1027prefuse e 0 0
1028ret 3
1029end
1030ptext KROWS
1031pdec get CF 0
1032ptext KCOLS
1033pdec get CF 1
1034ptext KRAW
1035pdec mul get CF 0 get CF 1
1036ptext KCODED
1037pdec get CF 5
1038ptext KSTR
1039pdec sub get CF 5 get CF 2
1040ptext KCRC
1041phex get CF 4
1042pbyte 10
1043pflush 1
1044ret 0
1045end
1046
1047note One timed pass over all rows, reps times. wc 0 runs the rANS decoder alone, 1 the CRC-16 check and
1048note the decoder, 2 also the CRC-32 of every decoded row. Returns the elapsed nanoseconds, or 0 after
1049note printing a refusal. RB holds one row, so the output stays in cache and no system call is timed.
1050fn benchpass reps ulong wc ulong to ulong
1051rows = get CF 0
1052cols = get CF 1
1053crc = ulong 4294967295
1054e = ulong 0
1055t0 = monons
1056for q ulong reps bound 9223372036854775807
1057for r ulong rows bound 4294967296
1058if eq wc 0
1059e = drowcore r
1060else
1061e = drow r
1062end
1063if ne e 0
1064prefuse e r 1
1065ret 0
1066end
1067if eq wc 2
1068crc = crcrb crc cols
1069end
1070end
1071end
1072t1 = monons
1073ret ulong sub t1 t0
1074end
1075
1076fn benchline mode view uchar * reps ulong ns ulong to void
1077rows = get CF 0
1078cols = get CF 1
1079ptext KMODE
1080ptext mode
1081pbyte 32
1082ptext KROWS
1083pdec rows
1084ptext KCOLS
1085pdec cols
1086ptext KREPS
1087pdec reps
1088ptext KNS
1089pdec ns
1090ptext KBPS
1091pdec div mul mul mul rows cols reps 1000000000 ns
1092pbyte 10
1093end
1094
1095fn bench to int
1096li = argpath 2 0
1097reps = argnum 3
1098if eq reps 0
1099ptext TBAD
1100pflush 2
1101ret 2
1102end
1103sz = loadimg ulong li
1104if lt sz 0
1105pio sz
1106ret 4
1107end
1108e = setupdec
1109if ne e 0
1110prefuse e 0 0
1111ret 3
1112end
1113crc = ulong 4294967295
1114for r ulong get CF 0 bound 4294967296
1115e = drow r
1116if ne e 0
1117prefuse e r 1
1118ret 3
1119end
1120crc = crcrb crc get CF 1
1121end
1122if ne xor crc 4294967295 get CF 4
1123prefuse 13 0 0
1124ret 3
1125end
1126ns0 = benchpass reps 0
1127if eq ns0 0
1128ret 3
1129end
1130ns1 = benchpass reps 1
1131if eq ns1 0
1132ret 3
1133end
1134ns2 = benchpass reps 2
1135if eq ns2 0
1136ret 3
1137end
1138benchline MDEC reps ns0
1139benchline MDRC reps ns1
1140benchline MDCRC reps ns2
1141pflush 1
1142ret 0
1143end
1144
1145note Flip each bit of one row's stream once and decode the row alone twice, with the rANS decoder alone
1146note and with the CRC-16 check in front, counting in FS: slot e (1..15) the checked refusals with code
1147note e, 16 the flips, 17 and 18 the rANS-only decodes that did not refuse and that gave a wrong row,
1148note 22 and 23 the same for the checked decode; 19 to 21 the position of the first rANS-only wrong row.
1149note cc is the CRC-32 state of the clean row.
1150fn flipbits r ulong a ulong n ulong cc ulong to void
1151for o ulong n bound 4294967296
1152for bit ulong 8
1153v = get IMG add a o
1154put IMG add a o uchar xor ulong v shl 1 bit
1155put FS 16 add get FS 16 1
1156e = drowcore r
1157if eq e 0
1158put FS 17 add get FS 17 1
1159if ne crcrb 4294967295 get CF 1 cc
1160if eq get FS 18 0
1161put FS 19 r
1162put FS 20 o
1163put FS 21 bit
1164end
1165put FS 18 add get FS 18 1
1166end
1167end
1168e = drow r
1169if eq e 0
1170put FS 22 add get FS 22 1
1171if ne crcrb 4294967295 get CF 1 cc
1172put FS 23 add get FS 23 1
1173end
1174else
1175put FS e add get FS e 1
1176end
1177put IMG add a o v
1178end
1179end
1180end
1181
1182note Flip every stream bit of the matrix once, each time decoding only the row that holds the bit.
1183fn flipscan to int
1184li = argpath 2 0
1185sz = loadimg ulong li
1186if lt sz 0
1187pio sz
1188ret 4
1189end
1190e = setupdec
1191if ne e 0
1192prefuse e 0 0
1193ret 3
1194end
1195rows = get CF 0
1196sb = get CF 2
1197for q ulong 32
1198put FS q 0
1199end
1200for r ulong rows bound 4294967296
1201e = drow r
1202if ne e 0
1203prefuse e r 1
1204ret 3
1205end
1206a = add sb ld32 add 544 mul 4 r
1207b = add sb ld32 add 544 mul 4 add r 1
1208flipbits r a sub b a crcrb 4294967295 get CF 1
1209end
1210ptext KFLIPS
1211pdec get FS 16
1212ptext KRU
1213pdec get FS 17
1214ptext KRW
1215pdec get FS 18
1216ptext KCU
1217pdec get FS 22
1218ptext KCW
1219pdec get FS 23
1220for q ulong 16
1221if ne get FS q 0
1222ptext KCODE
1223pdec q
1224ptext KEQ
1225pdec get FS q
1226end
1227end
1228if ne get FS 18 0
1229ptext KFR
1230pdec get FS 19
1231ptext KFB
1232pdec get FS 20
1233ptext KFT
1234pdec get FS 21
1235end
1236pbyte 10
1237pflush 1
1238ret 0
1239end
1240
1241note Recompute the header CRC in IMG and write the whole image back over the file at PA. li is the
1242note path length. Test aid: it makes a hand-edited file consistent so only the later checks can refuse.
1243fn savefix li ulong to int
1244hc = hcpos ld32 8
1245st32 hc xor crcimg 4294967295 0 hc 4294967295
1246fd = int fopen PA 0 li int 1 int 0
1247if lt fd 0
1248pio long fd
1249ret 4
1250end
1251if ne wrimg fd get CF 5 0
1252pio -5
1253ret 4
1254end
1255k = fclose fd
1256ptext KFIX
1257phex ld32 hc
1258pbyte 10
1259pflush 1
1260ret 0
1261end
1262
1263note fixcrc IN: recompute only the header CRC.
1264fn fixcrc to int
1265li = argpath 2 0
1266sz = loadimg ulong li
1267if lt sz 0
1268pio sz
1269ret 4
1270end
1271if lt ulong sz 32
1272prefuse 1 0 0
1273ret 3
1274end
1275if lt ulong sz add hcpos ld32 8 4
1276prefuse 1 0 0
1277ret 3
1278end
1279ret savefix ulong li
1280end
1281
1282note Recompute the CRC-16 of row r from the stream bytes its offsets name. 0, or 8 when they leave the file.
1283fn fixone r ulong lim ulong to ulong
1284rows = ld32 8
1285sb = add hcpos rows 4
1286a = add sb ld32 add 544 mul 4 r
1287b = add sb ld32 add 544 mul 4 add r 1
1288if gt b lim
1289ret 8
1290end
1291if lt b a
1292ret 8
1293end
1294st16 add add 548 mul 4 rows mul 2 r crc16img a b
1295ret 0
1296end
1297
1298note fixrows IN: recompute every row CRC-16 from the stream bytes the offsets name, then the header CRC.
1299fn fixrows to int
1300li = argpath 2 0
1301sz = loadimg ulong li
1302if lt sz 0
1303pio sz
1304ret 4
1305end
1306if lt ulong sz 32
1307prefuse 1 0 0
1308ret 3
1309end
1310rows = ld32 8
1311if lt ulong sz add hcpos rows 4
1312prefuse 1 0 0
1313ret 3
1314end
1315for r ulong rows bound 4294967296
1316e = fixone r ulong sz
1317if ne e 0
1318prefuse e r 1
1319ret 3
1320end
1321end
1322ret savefix ulong li
1323end
1324
1325note sum IN: the CRC-32 and CRC-16 of the whole file, for known-answer tests of the two checks.
1326fn sum to int
1327li = argpath 2 0
1328sz = loadimg ulong li
1329if lt sz 0
1330pio sz
1331ret 4
1332end
1333ptext KS32
1334phex xor crcimg 4294967295 0 ulong sz 4294967295
1335ptext KS16
1336phex crc16img 0 ulong sz
1337pbyte 10
1338pflush 1
1339ret 0
1340end
1341
1342fn main to int
1343crcinit
1344n = argcnt
1345if lt n 2
1346ptext TUSE
1347pflush 2
1348ret 2
1349end
1350if argis 1 WENC
1351ret encode
1352end
1353if argis 1 WDEC
1354ret decode
1355end
1356if argis 1 WDROW
1357ret decoderow
1358end
1359if argis 1 WINFO
1360ret info
1361end
1362if argis 1 WBEN
1363ret bench
1364end
1365if argis 1 WFLIP
1366ret flipscan
1367end
1368if argis 1 WFIX
1369ret fixcrc
1370end
1371if argis 1 WFIXR
1372ret fixrows
1373end
1374if argis 1 WSUM
1375ret sum
1376end
1377ptext TUSE
1378pflush 2
1379ret 2
1380end