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