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
omega-mv / src / turing / tc_range.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note |
| 4 | note src/turing/tc_range.om: TURING Yield coder A (TCR1, range coder) written in Omega, the peer |
| 5 | note of src/turing/tc_range.c (ledger L111). Normative text: calibration/docs/CODER_SPEC.md |
| 6 | note sections 2 (TPS1), 3 (TSY1), 4 (coded header), 5 (range coder), 8 (ideal length) and |
| 7 | note docs/turing/TURING_YIELD_RANGE_CODER_OM.md (the length bound and its proof). |
| 8 | note |
| 9 | note Program mode, OM-1 code in an OM-2 unit: composed after the FIPS 180-4 unit seed/ng/sha256.om |
| 10 | note (prefix sh_), with an om2 first line for the unsized text views: |
| 11 | note { printf 'om2\n'; cat seed/ng/sha256.om src/turing/tc_range.om; } | seed/out/omc > tcr-om |
| 12 | note |
| 13 | note CLI (same shape as turing-coder, range only): |
| 14 | note tcr-om encode range IN.tps IN.tsy OUT.tcr |
| 15 | note tcr-om decode range IN.tps IN.tcr OUT.tsy |
| 16 | note tcr-om verify range IN.tps IN.tsy IN.tcr |
| 17 | note tcr-om selftest |
| 18 | note Exit 0 accepted, 2 refused (one line on stderr with the CODER_SPEC s.10 code), 1 usage, |
| 19 | note 3 ALARM: a proved property failed on this run (length identity, upper or lower bound, ub rule, |
| 20 | note or a micro-bit sum that would wrap). |
| 21 | note Refusals come in turing-coder's order (encode: TPS1, TSY1, pair; decode: TPS1, coded file, |
| 22 | note decode; verify: TPS1, coded file and decode, TSY1 and pair, byte identity). |
| 23 | note |
| 24 | note Every file is streamed through fixed buffers, so memory does not grow with the number of |
| 25 | note symbols. The micro-bit sums are 64-bit, and one past either ceiling wraps 2^64-1 (Wolfram W3): |
| 26 | note tpsload refuses n > 1152514738906 with ARG, before the body-size check, because |
| 27 | note 16005647*n + 480000000 no longer fits |
| 28 | note report refuses a coded file of more than 2305843009213 bytes with ARG, because |
| 29 | note coded_bytes*8*1000000 no longer fits |
| 30 | note These are the word limits of the check, not a buffer size. A sum that would wrap anyway |
| 31 | note raises ALARM 7 (uadd / umul). Each input is read twice: a validation pass (trailer digest, |
| 32 | note header, every record or symbol, in the refusal order of src/turing/tc_pstream.c) and the |
| 33 | note coding pass, which re-checks every record and symbol and re-hashes the file (DIGEST if it |
| 34 | note changed in between). An output path naming an input is refused; a refusal after the output |
| 35 | note was created, including IO (102) from a failed fsync or fclose in ofinish, removes it. |
| 36 | note |
| 37 | note Length bounds. The Lean file docs/turing/proofs/TcrBound.lean proves the natural-number model |
| 38 | note (step, renorm, run), not encs. The log step and Wolfram receipts are in |
| 39 | note docs/turing/TURING_YIELD_RANGE_CODER_OM.md. |
| 40 | note payload_bytes = renorm shifts + 4, coded_bits = 8 * shifts + 480 |
| 41 | note encode: shifts are EN[5], one per encs renorm; payload is the bytes emitted (EN[6]) |
| 42 | note decode and verify: shifts are DC[5], one per decs renorm byte (range < 2^24). The four |
| 43 | note bytes decbeg reads are not shifts. Payload is the declared length. ST[7] is DC[5], |
| 44 | note not payload - 4. ALARM 1 if they disagree. |
| 45 | note selftest: both comparisons, on the encoded bytes and after a successful decode |
| 46 | note upper: 1000000 * coded_bits <= ideal_ub + 5647 * n + 480000000 |
| 47 | note lower: 1000000 * coded_bits >= ideal_ub + 472000000 - n - gain_ub, gain_ub = sum over symbols |
| 48 | note coded as the last of their row of an upper bound of 1e6 * log2(1 + 256/q) (gub). |
| 49 | note That integer test is a necessary consequence of coded_bits >= T + 472 - G, not a |
| 50 | note sufficient one. lower=PASS certifies coded_bits >= T + 472 - G - |
| 51 | note (1.49984 n + 24.37 m) / 1e6, m = last-symbol count (see the range-coder doc). |
| 52 | note The upper and lower comparisons run on every encode, decode, verify and selftest case. |
| 53 | |
| 54 | global RB uchar 196608 |
| 55 | global RS ulong 24 |
| 56 | global OB uchar 65536 |
| 57 | global OS ulong 8 |
| 58 | global MB uchar 16777216 |
| 59 | global HB uchar 256 |
| 60 | global HS uint 16 |
| 61 | global HP uchar 128 |
| 62 | global HN ulong 2 |
| 63 | global Q ulong 16 |
| 64 | global EN ulong 8 |
| 65 | global DC ulong 8 |
| 66 | global ST ulong 32 |
| 67 | global UBT ulong 65537 |
| 68 | global AR uchar 24576 |
| 69 | global AL ulong 8 |
| 70 | global DG uchar 256 |
| 71 | global INO ulong 8 |
| 72 | global HSEL ulong 4 |
| 73 | global FS uchar 128 |
| 74 | global PB uchar 4096 |
| 75 | global PL ulong 2 |
| 76 | global RG ulong 2 |
| 77 | |
| 78 | text DPS "turing.tc.pstream.v1" |
| 79 | text DSY "turing.tc.symbols.v1" |
| 80 | text ENAM "ARG IO FORMAT DIGEST NORM ZERO PROFILE MODEL DATASET COUNT HEADER BINDING TRUNC TRAIL CORRUPT SYMBOL INDEX " |
| 81 | text TREF "tcr-om: refused -" |
| 82 | text TALM "tcr-om: ALARM " |
| 83 | text TAT " at " |
| 84 | text TUSE "usage: tcr-om encode|decode|verify range IN.tps IN.tsy|IN.tcr OUT|IN, or tcr-om selftest\n" |
| 85 | text KMODE "mode=" |
| 86 | text KCNT "count=" |
| 87 | text KIDL "ideal_ub=" |
| 88 | text KCB "coded_bytes=" |
| 89 | text KCBT "coded_bits=" |
| 90 | text KPAY "payload_bytes=" |
| 91 | text KSHF "shifts=" |
| 92 | text KBND "bound_ub=" |
| 93 | text KSLK "slack_ub=" |
| 94 | text KOVH "overhead_ub=" |
| 95 | text KTPS "tps1_digest=" |
| 96 | text KTSY "tsy1_digest=" |
| 97 | text KBOK "bound=PASS\n" |
| 98 | text KLOW "lower_ub=" |
| 99 | text KLOK "lower=PASS\n" |
| 100 | text KRT "roundtrip=byte-exact\n" |
| 101 | text KCASE "selftest cases=" |
| 102 | text KSYM " symbols=" |
| 103 | text KFLIP " flips_detected=" |
| 104 | text KMIN " min_slack_ub=" |
| 105 | text KMIN1 " min_slack_n_ge_1_ub=" |
| 106 | text KLMIN " min_lower_slack_ub=" |
| 107 | text KBELOW " max_below_ideal_ub=" |
| 108 | text KPASS "\nselftest=PASS\n" |
| 109 | text WENC "encode" |
| 110 | text WDEC "decode" |
| 111 | text WVER "verify" |
| 112 | text WSLF "selftest" |
| 113 | text WRNG "range" |
| 114 | text ALID "length identity payload_bytes != shifts + 4" |
| 115 | text ALBD "length bound 1e6*coded_bits > ideal_ub + 5647*n + 480e6" |
| 116 | text ALRT "selftest round trip" |
| 117 | text ALFL "selftest corruption not detected" |
| 118 | text ALUB "ub rule differs from the Wolfram receipt" |
| 119 | text ALLO "lower bound 1e6*coded_bits < ideal_ub + 472e6 - n - gain_ub" |
| 120 | text ALOV "length sum would wrap the 64-bit micro-bit word" |
| 121 | text WOVF "overflow" |
| 122 | text KUB "ub_rule q=1..65535 sum=" |
| 123 | text KUBQ " qsum=" |
| 124 | |
| 125 | note ---------------------------------------------------------------- printing (PB to fd 1 or 2) |
| 126 | |
| 127 | fn pbyte b ulong to void |
| 128 | n = get PL 0 |
| 129 | if lt n 4096 |
| 130 | put PB n uchar b |
| 131 | put PL 0 add n 1 |
| 132 | end |
| 133 | end |
| 134 | |
| 135 | fn ptext t view uchar * to void |
| 136 | for i ulong len t bound 4096 |
| 137 | pbyte ulong get t i |
| 138 | end |
| 139 | end |
| 140 | |
| 141 | fn pdec v ulong to void |
| 142 | d = new uchar 24 0 |
| 143 | k = ulong 0 |
| 144 | x = v |
| 145 | while true bound 21 |
| 146 | put d k uchar add rem x 10 48 |
| 147 | k = add k 1 |
| 148 | x = div x 10 |
| 149 | if eq x 0 |
| 150 | break |
| 151 | end |
| 152 | end |
| 153 | while gt k 0 bound 21 |
| 154 | k = sub k 1 |
| 155 | pbyte ulong get d k |
| 156 | end |
| 157 | end |
| 158 | |
| 159 | fn phex o ulong to void |
| 160 | for i ulong 32 |
| 161 | b = ulong get DG add o i |
| 162 | h = shr b 4 |
| 163 | l = and b 15 |
| 164 | if lt h 10 |
| 165 | pbyte add h 48 |
| 166 | else |
| 167 | pbyte add h 87 |
| 168 | end |
| 169 | if lt l 10 |
| 170 | pbyte add l 48 |
| 171 | else |
| 172 | pbyte add l 87 |
| 173 | end |
| 174 | end |
| 175 | end |
| 176 | |
| 177 | fn pkv k view uchar * v ulong to void |
| 178 | ptext k |
| 179 | pdec v |
| 180 | pbyte 10 |
| 181 | end |
| 182 | |
| 183 | fn pflush fd int to void |
| 184 | n = get PL 0 |
| 185 | o = ulong 0 |
| 186 | while lt o n bound 4096 |
| 187 | k = write fd PB o sub n o |
| 188 | if le k 0 |
| 189 | break |
| 190 | end |
| 191 | o = add o ulong k |
| 192 | end |
| 193 | put PL 0 0 |
| 194 | end |
| 195 | |
| 196 | note ---------------------------------------------------------------- refusal and cleanup |
| 197 | |
| 198 | fn cleanup to void |
| 199 | note Remove a partly written output file (encode, decode) after a refusal. |
| 200 | if eq get OS 4 1 |
| 201 | f = int get OS 0 |
| 202 | z = fclose f |
| 203 | o = mul get OS 5 4096 |
| 204 | k = unlink AR o get AL get OS 5 |
| 205 | put OS 4 0 |
| 206 | end |
| 207 | end |
| 208 | |
| 209 | fn refuse c ulong t ulong to void |
| 210 | note c = 101..117 (CODER_SPEC s.10), t = the record or symbol index (or 0). |
| 211 | cleanup |
| 212 | put PL 0 0 |
| 213 | ptext TREF |
| 214 | pdec c |
| 215 | pbyte 32 |
| 216 | o = mul sub c 101 8 |
| 217 | for i ulong 8 |
| 218 | b = ulong get ENAM add o i |
| 219 | if ne b 32 |
| 220 | pbyte b |
| 221 | end |
| 222 | end |
| 223 | ptext TAT |
| 224 | pdec t |
| 225 | pbyte 10 |
| 226 | pflush 2 |
| 227 | exit 2 |
| 228 | end |
| 229 | |
| 230 | fn alarm w ulong to void |
| 231 | cleanup |
| 232 | pflush 1 |
| 233 | ptext TALM |
| 234 | if eq w 1 |
| 235 | ptext ALID |
| 236 | end |
| 237 | if eq w 2 |
| 238 | ptext ALBD |
| 239 | end |
| 240 | if eq w 3 |
| 241 | ptext ALRT |
| 242 | end |
| 243 | if eq w 4 |
| 244 | ptext ALFL |
| 245 | end |
| 246 | if eq w 5 |
| 247 | ptext ALUB |
| 248 | end |
| 249 | if eq w 6 |
| 250 | ptext ALLO |
| 251 | end |
| 252 | if eq w 7 |
| 253 | ptext ALOV |
| 254 | end |
| 255 | pbyte 10 |
| 256 | pflush 2 |
| 257 | exit 3 |
| 258 | end |
| 259 | |
| 260 | note ---------------------------------------------------------------- input streams (0 TPS, 1 TSY, 2 coded) |
| 261 | |
| 262 | fn rdfill s ulong to ulong |
| 263 | b = mul s 8 |
| 264 | f = int get RS b |
| 265 | k = read f RB mul s 65536 65536 |
| 266 | if lt k 0 |
| 267 | refuse 102 0 |
| 268 | end |
| 269 | put RS add b 1 0 |
| 270 | put RS add b 2 ulong k |
| 271 | ret ulong k |
| 272 | end |
| 273 | |
| 274 | fn rdb s ulong to ulong |
| 275 | note Next byte of stream s, or 256 at end of file. HSEL[s] = 1 + h feeds the byte to hash stream h. |
| 276 | b = mul s 8 |
| 277 | if ge get RS add b 1 get RS add b 2 |
| 278 | k = rdfill s |
| 279 | if eq k 0 |
| 280 | ret 256 |
| 281 | end |
| 282 | end |
| 283 | p = get RS add b 1 |
| 284 | put RS add b 1 add p 1 |
| 285 | v = ulong get RB add mul s 65536 p |
| 286 | h = get HSEL s |
| 287 | if ne h 0 |
| 288 | hbyte sub h 1 v |
| 289 | end |
| 290 | ret v |
| 291 | end |
| 292 | |
| 293 | fn lefs o ulong to ulong |
| 294 | v = ulong 0 |
| 295 | for i ulong 8 |
| 296 | v = or shl v 8 ulong get FS sub add o 7 i |
| 297 | end |
| 298 | ret v |
| 299 | end |
| 300 | |
| 301 | fn sopen s ulong a ulong to ulong |
| 302 | note Open argument a read-only as stream s; returns its size in bytes and keeps its device and |
| 303 | note inode (INO[2s], INO[2s+1]) so that an output path naming an input is refused. |
| 304 | o = mul a 4096 |
| 305 | f = fopen AR o get AL a 0 0 |
| 306 | if lt f 0 |
| 307 | refuse 102 0 |
| 308 | end |
| 309 | b = mul s 8 |
| 310 | put RS b ulong f |
| 311 | put RS add b 1 0 |
| 312 | put RS add b 2 0 |
| 313 | put HSEL s 0 |
| 314 | k = fstat int f FS 0 128 |
| 315 | if lt k 0 |
| 316 | refuse 102 0 |
| 317 | end |
| 318 | put INO mul s 2 lefs 0 |
| 319 | put INO add mul s 2 1 lefs 8 |
| 320 | ret lefs 48 |
| 321 | end |
| 322 | |
| 323 | fn srew s ulong to void |
| 324 | note Back to byte 0 of stream s. |
| 325 | b = mul s 8 |
| 326 | f = int get RS b |
| 327 | k = fseek f long 0 int 0 |
| 328 | if ne k 0 |
| 329 | refuse 102 0 |
| 330 | end |
| 331 | put RS add b 1 0 |
| 332 | put RS add b 2 0 |
| 333 | end |
| 334 | |
| 335 | fn rdn s ulong n ulong to void |
| 336 | note n bytes of stream s into HB[0..n); a short file here is an IO refusal (sizes are checked first). |
| 337 | for i ulong n bound 256 |
| 338 | b = rdb s |
| 339 | if eq b 256 |
| 340 | refuse 102 0 |
| 341 | end |
| 342 | put HB i uchar b |
| 343 | end |
| 344 | end |
| 345 | |
| 346 | fn lev o ulong w ulong to ulong |
| 347 | note Little-endian value of w bytes at HB[o]. |
| 348 | v = ulong 0 |
| 349 | for i ulong w bound 8 |
| 350 | v = or shl v 8 ulong get HB sub add o sub w 1 i |
| 351 | end |
| 352 | ret v |
| 353 | end |
| 354 | |
| 355 | fn magic a ulong b ulong c ulong d ulong to bool |
| 356 | r = true |
| 357 | if ne ulong get HB 0 a |
| 358 | r = false |
| 359 | end |
| 360 | if ne ulong get HB 1 b |
| 361 | r = false |
| 362 | end |
| 363 | if ne ulong get HB 2 c |
| 364 | r = false |
| 365 | end |
| 366 | if ne ulong get HB 3 d |
| 367 | r = false |
| 368 | end |
| 369 | ret r |
| 370 | end |
| 371 | |
| 372 | note ---------------------------------------------------------------- SHA-256 streams over sh_ (h 0, 1) |
| 373 | |
| 374 | fn hblk h ulong to void |
| 375 | for j ulong 8 |
| 376 | put sh_h j get HS add mul h 8 j |
| 377 | end |
| 378 | for j ulong 64 |
| 379 | put sh_pad j get HP add mul h 64 j |
| 380 | end |
| 381 | sh_blk 1 0 |
| 382 | for j ulong 8 |
| 383 | put HS add mul h 8 j get sh_h j |
| 384 | end |
| 385 | end |
| 386 | |
| 387 | fn hbyte h ulong b ulong to void |
| 388 | n = get HN h |
| 389 | put HP add mul h 64 rem n 64 uchar b |
| 390 | n = add n 1 |
| 391 | put HN h n |
| 392 | if eq rem n 64 0 |
| 393 | hblk h |
| 394 | end |
| 395 | end |
| 396 | |
| 397 | fn hbeg h ulong d ulong to void |
| 398 | note Start stream h with domain d (0 pstream, 1 symbols) and its 0x00 separator. |
| 399 | sh_init |
| 400 | for j ulong 8 |
| 401 | put HS add mul h 8 j get sh_k add j 64 |
| 402 | end |
| 403 | put HN h 0 |
| 404 | for i ulong 20 |
| 405 | if eq d 0 |
| 406 | hbyte h ulong get DPS i |
| 407 | else |
| 408 | hbyte h ulong get DSY i |
| 409 | end |
| 410 | end |
| 411 | hbyte h 0 |
| 412 | end |
| 413 | |
| 414 | fn hend h ulong o ulong to void |
| 415 | note Finish stream h; the 32-byte digest goes to DG[o..o+32). |
| 416 | bits = mul get HN h 8 |
| 417 | hbyte h 128 |
| 418 | while ne rem get HN h 64 56 bound 64 |
| 419 | hbyte h 0 |
| 420 | end |
| 421 | for j ulong 8 |
| 422 | hbyte h and shr bits mul sub 7 j 8 255 |
| 423 | end |
| 424 | for j ulong 8 |
| 425 | put sh_h j get HS add mul h 8 j |
| 426 | end |
| 427 | sh_out |
| 428 | for i ulong 32 |
| 429 | put DG add o i get sh_dig i |
| 430 | end |
| 431 | end |
| 432 | |
| 433 | fn dgeq a ulong b ulong to bool |
| 434 | r = true |
| 435 | for i ulong 32 |
| 436 | if ne get DG add a i get DG add b i |
| 437 | r = false |
| 438 | end |
| 439 | end |
| 440 | ret r |
| 441 | end |
| 442 | |
| 443 | fn hfile s ulong d ulong sz ulong o ulong to bool |
| 444 | note Validation pass: hash domain d || bytes[0..sz-32) of stream s, read the 32-byte trailer |
| 445 | note into DG[o+32..o+64) and the computed digest into DG[o..o+32); true when they agree. |
| 446 | hbeg 0 d |
| 447 | m = sub sz 32 |
| 448 | x = ulong 0 |
| 449 | while lt x m bound 9223372036854775807 |
| 450 | b = rdb s |
| 451 | if eq b 256 |
| 452 | refuse 102 0 |
| 453 | end |
| 454 | hbyte 0 b |
| 455 | x = add x 1 |
| 456 | end |
| 457 | for i ulong 32 |
| 458 | b = rdb s |
| 459 | if eq b 256 |
| 460 | refuse 102 0 |
| 461 | end |
| 462 | put DG add add o 32 i uchar b |
| 463 | end |
| 464 | hend 0 o |
| 465 | ret dgeq o add o 32 |
| 466 | end |
| 467 | |
| 468 | note ---------------------------------------------------------------- ideal length (CODER_SPEC s.8) |
| 469 | |
| 470 | fn sq62 m ulong to ulong |
| 471 | note floor(m * m / 2^62) for m < 2^63 through 32-bit limbs (as tqsq, native/turing/origsel.om). |
| 472 | a = shr m 32 |
| 473 | b = and m 4294967295 |
| 474 | p0 = mul b b |
| 475 | p1 = mul a b |
| 476 | p2 = mul a a |
| 477 | c = shl p1 1 |
| 478 | mid = add shr p0 32 and c 4294967295 |
| 479 | lo = or and p0 4294967295 shl and mid 4294967295 32 |
| 480 | hi = add add p2 shr c 32 shr mid 32 |
| 481 | ret or shl hi 2 shr lo 62 |
| 482 | end |
| 483 | |
| 484 | fn hibit q ulong to ulong |
| 485 | i = ulong 63 |
| 486 | while eq and shr q i 1 0 bound 64 |
| 487 | i = sub i 1 |
| 488 | end |
| 489 | ret i |
| 490 | end |
| 491 | |
| 492 | fn ubcalc q ulong to ulong |
| 493 | ip = hibit q |
| 494 | m = shl q sub 62 ip |
| 495 | f = ulong 0 |
| 496 | for i ulong 32 |
| 497 | m = sq62 m |
| 498 | if ge m 9223372036854775808 |
| 499 | f = or f shl 1 sub 31 i |
| 500 | m = shr m 1 |
| 501 | end |
| 502 | end |
| 503 | l = or shl ip 32 f |
| 504 | x = sub 68719476736 l |
| 505 | ret shr add mul x 1000000 2147483648 32 |
| 506 | end |
| 507 | |
| 508 | fn ub q ulong to ulong |
| 509 | note ub(q) in micro-bits, memoised (UBT holds ub + 1, 0 = not yet computed). |
| 510 | v = get UBT q |
| 511 | if eq v 0 |
| 512 | v = add ubcalc q 1 |
| 513 | put UBT q v |
| 514 | end |
| 515 | ret sub v 1 |
| 516 | end |
| 517 | |
| 518 | note ---------------------------------------------------------------- payload byte sink and source |
| 519 | |
| 520 | fn oflush to void |
| 521 | n = get OS 1 |
| 522 | f = int get OS 0 |
| 523 | o = ulong 0 |
| 524 | while lt o n bound 65536 |
| 525 | k = write f OB o sub n o |
| 526 | if le k 0 |
| 527 | refuse 102 0 |
| 528 | end |
| 529 | o = add o ulong k |
| 530 | end |
| 531 | put OS 1 0 |
| 532 | end |
| 533 | |
| 534 | fn obyte b ulong to void |
| 535 | note Output byte to the file buffer (OS[3] = 0) or to the selftest memory MB (OS[3] = 1). |
| 536 | if eq get OS 3 1 |
| 537 | p = get OS 2 |
| 538 | put MB p uchar b |
| 539 | put OS 2 add p 1 |
| 540 | ret |
| 541 | end |
| 542 | n = get OS 1 |
| 543 | put OB n uchar b |
| 544 | put OS 1 add n 1 |
| 545 | if eq add n 1 65536 |
| 546 | oflush |
| 547 | end |
| 548 | end |
| 549 | |
| 550 | fn nxtb to ulong |
| 551 | note Next payload byte for the decoder, or 256 when the declared payload is used up. |
| 552 | p = get DC 2 |
| 553 | if ge p get DC 3 |
| 554 | ret 256 |
| 555 | end |
| 556 | put DC 2 add p 1 |
| 557 | if eq get DC 4 1 |
| 558 | ret ulong get MB p |
| 559 | end |
| 560 | b = rdb 2 |
| 561 | if eq b 256 |
| 562 | refuse 102 0 |
| 563 | end |
| 564 | ret b |
| 565 | end |
| 566 | |
| 567 | note ---------------------------------------------------------------- range coder (CODER_SPEC s.5) |
| 568 | |
| 569 | fn shlow to ulong |
| 570 | note shift_low; returns 0. A carry before the first cached byte has nowhere to land, so the |
| 571 | note emitted length cannot equal the renorm shifts. That is the length identity, ALARM 1, |
| 572 | note not a bad input (decode still reports CORRUPT). |
| 573 | lo = get EN 0 |
| 574 | c = shr lo 32 |
| 575 | if or lt and lo 4294967295 4278190080 ne c 0 |
| 576 | if eq get EN 3 1 |
| 577 | obyte and add get EN 2 c 255 |
| 578 | put EN 6 add get EN 6 1 |
| 579 | else |
| 580 | if ne c 0 |
| 581 | alarm 1 |
| 582 | end |
| 583 | end |
| 584 | while gt get EN 4 0 bound 9223372036854775807 |
| 585 | obyte and add 255 c 255 |
| 586 | put EN 6 add get EN 6 1 |
| 587 | put EN 4 sub get EN 4 1 |
| 588 | end |
| 589 | put EN 2 and shr lo 24 255 |
| 590 | put EN 3 1 |
| 591 | else |
| 592 | put EN 4 add get EN 4 1 |
| 593 | end |
| 594 | put EN 0 shl and lo 16777215 8 |
| 595 | ret 0 |
| 596 | end |
| 597 | |
| 598 | fn encbeg to void |
| 599 | put EN 0 0 |
| 600 | put EN 1 4294967295 |
| 601 | put EN 2 0 |
| 602 | put EN 3 0 |
| 603 | put EN 4 0 |
| 604 | put EN 5 0 |
| 605 | put EN 6 0 |
| 606 | end |
| 607 | |
| 608 | fn encs s ulong to ulong |
| 609 | note Code symbol s with the row Q[0..K), K = ST[2]; returns 0 or a refusal number. |
| 610 | k = get ST 2 |
| 611 | cum = ulong 0 |
| 612 | for x ulong s bound 16 |
| 613 | cum = add cum get Q x |
| 614 | end |
| 615 | rg = get EN 1 |
| 616 | r = shr rg 16 |
| 617 | put EN 0 add get EN 0 mul r cum |
| 618 | if eq s sub k 1 |
| 619 | rg = sub rg mul r cum |
| 620 | else |
| 621 | rg = mul r get Q s |
| 622 | end |
| 623 | while lt rg 16777216 bound 2 |
| 624 | rg = shl rg 8 |
| 625 | put EN 5 add get EN 5 1 |
| 626 | e = shlow |
| 627 | if ne e 0 |
| 628 | ret e |
| 629 | end |
| 630 | end |
| 631 | put EN 1 rg |
| 632 | ret 0 |
| 633 | end |
| 634 | |
| 635 | fn encend to ulong |
| 636 | for i ulong 5 |
| 637 | e = shlow |
| 638 | if ne e 0 |
| 639 | ret e |
| 640 | end |
| 641 | end |
| 642 | ret 0 |
| 643 | end |
| 644 | |
| 645 | fn decbeg to ulong |
| 646 | note DC: 0 code, 1 range, 2 payload bytes consumed, 3 payload bytes declared, 4 source, |
| 647 | note 5 renorm shifts (one per decs byte, not these four). |
| 648 | if lt get DC 3 4 |
| 649 | ret 113 |
| 650 | end |
| 651 | put DC 2 0 |
| 652 | put DC 5 0 |
| 653 | c = ulong 0 |
| 654 | for i ulong 4 |
| 655 | c = or shl c 8 nxtb |
| 656 | end |
| 657 | put DC 0 c |
| 658 | put DC 1 4294967295 |
| 659 | ret 0 |
| 660 | end |
| 661 | |
| 662 | fn decs to ulong |
| 663 | note Decode one symbol with the row Q[0..K); returns it, or 1000 + a refusal number. |
| 664 | k = get ST 2 |
| 665 | code = get DC 0 |
| 666 | rg = get DC 1 |
| 667 | if ge code rg |
| 668 | ret 1115 |
| 669 | end |
| 670 | r = shr rg 16 |
| 671 | cum = ulong 0 |
| 672 | s = ulong 0 |
| 673 | nk = sub k 1 |
| 674 | while lt s nk bound 16 |
| 675 | nx = add cum get Q s |
| 676 | if lt code mul r nx |
| 677 | break |
| 678 | end |
| 679 | cum = nx |
| 680 | s = add s 1 |
| 681 | end |
| 682 | code = sub code mul r cum |
| 683 | if eq s nk |
| 684 | rg = sub rg mul r cum |
| 685 | else |
| 686 | rg = mul r get Q s |
| 687 | end |
| 688 | while lt rg 16777216 bound 2 |
| 689 | b = nxtb |
| 690 | if gt b 255 |
| 691 | ret 1113 |
| 692 | end |
| 693 | code = and or shl code 8 b 4294967295 |
| 694 | rg = shl rg 8 |
| 695 | put DC 5 add get DC 5 1 |
| 696 | end |
| 697 | put DC 0 code |
| 698 | put DC 1 rg |
| 699 | ret s |
| 700 | end |
| 701 | |
| 702 | fn decend to ulong |
| 703 | if ne get DC 0 0 |
| 704 | ret 115 |
| 705 | end |
| 706 | if ne get DC 2 get DC 3 |
| 707 | ret 114 |
| 708 | end |
| 709 | ret 0 |
| 710 | end |
| 711 | |
| 712 | note ---------------------------------------------------------------- TPS1 (stream 0) and TSY1 (stream 1) |
| 713 | |
| 714 | fn tpsrow t ulong chk ulong to void |
| 715 | note Read record t into Q (K = ST[2], record bytes ST[3]); chk 1 applies INDEX, ZERO, NORM. |
| 716 | k = get ST 2 |
| 717 | rdn 0 get ST 3 |
| 718 | if eq chk 1 |
| 719 | if ne lev 0 8 t |
| 720 | refuse 117 t |
| 721 | end |
| 722 | end |
| 723 | sum = ulong 0 |
| 724 | zero = false |
| 725 | for x ulong k bound 16 |
| 726 | v = lev add 24 mul x 2 2 |
| 727 | put Q x v |
| 728 | sum = add sum v |
| 729 | if eq v 0 |
| 730 | zero = true |
| 731 | end |
| 732 | end |
| 733 | if eq chk 1 |
| 734 | if zero |
| 735 | refuse 106 t |
| 736 | end |
| 737 | if ne sum 65536 |
| 738 | refuse 105 t |
| 739 | end |
| 740 | if ne lev 20 4 65536 |
| 741 | refuse 105 t |
| 742 | end |
| 743 | end |
| 744 | end |
| 745 | |
| 746 | fn tpshdr chk ulong to void |
| 747 | note Read the 128-byte header; chk 1 applies the FORMAT checks and fills ST and DG. |
| 748 | rdn 0 128 |
| 749 | if eq chk 0 |
| 750 | ret |
| 751 | end |
| 752 | k = ulong get HB 6 |
| 753 | ok = true |
| 754 | if ne lev 4 2 1 |
| 755 | ok = false |
| 756 | end |
| 757 | if ne ulong get HB 7 16 |
| 758 | ok = false |
| 759 | end |
| 760 | if ne lev 8 4 0 |
| 761 | ok = false |
| 762 | end |
| 763 | if ne lev 24 4 0 |
| 764 | ok = false |
| 765 | end |
| 766 | if ne lev 124 4 0 |
| 767 | ok = false |
| 768 | end |
| 769 | if lt k 2 |
| 770 | ok = false |
| 771 | end |
| 772 | if gt k 16 |
| 773 | ok = false |
| 774 | end |
| 775 | if not ok |
| 776 | refuse 103 0 |
| 777 | end |
| 778 | put ST 2 k |
| 779 | put ST 0 lev 12 8 |
| 780 | put ST 3 add 24 mul k 2 |
| 781 | if ne lev 20 4 get ST 3 |
| 782 | refuse 103 0 |
| 783 | end |
| 784 | for i ulong 32 |
| 785 | put DG add 64 i get HB add 92 i |
| 786 | end |
| 787 | end |
| 788 | |
| 789 | fn uadd a ulong b ulong to ulong |
| 790 | note a + b, or ALARM 7 if the sum would wrap 2^64. |
| 791 | if gt a sub 18446744073709551615 b |
| 792 | alarm 7 |
| 793 | end |
| 794 | ret add a b |
| 795 | end |
| 796 | |
| 797 | fn umul a ulong b ulong to ulong |
| 798 | note a * b, or ALARM 7 if the product would wrap 2^64. |
| 799 | if eq a 0 |
| 800 | ret 0 |
| 801 | end |
| 802 | if eq b 0 |
| 803 | ret 0 |
| 804 | end |
| 805 | if gt a div 18446744073709551615 b |
| 806 | alarm 7 |
| 807 | end |
| 808 | ret mul a b |
| 809 | end |
| 810 | |
| 811 | fn ncap n ulong to void |
| 812 | note ARG when n is past the last count whose maximum upper sum fits in 64 bits. |
| 813 | note 16005647 * n + 480000000 <= 2^64-1 for n = 1152514738906 and not for n + 1 (Wolfram W3). |
| 814 | if gt n 1152514738906 |
| 815 | refuse 101 0 |
| 816 | end |
| 817 | end |
| 818 | |
| 819 | fn bndsum idl ulong n ulong to ulong |
| 820 | note ideal_ub + 5647*n + 480000000. ALARM 7 if that sum would wrap. |
| 821 | ret uadd uadd idl umul n 5647 480000000 |
| 822 | end |
| 823 | |
| 824 | fn cubit cb ulong to ulong |
| 825 | note 1000000 * coded_bits = cb * 8 * 1000000. ALARM 7 if that product would wrap. |
| 826 | ret umul umul cb 8 1000000 |
| 827 | end |
| 828 | |
| 829 | fn tpsload a ulong to void |
| 830 | note Validate TPS1 argument a fully (CODER_SPEC s.2 order). DG[0..32) = its digest, |
| 831 | note DG[64..96) = its dataset digest, ST[0] = count, ST[2] = K, ST[3] = record bytes. |
| 832 | sz = sopen 0 a |
| 833 | if lt sz 160 |
| 834 | refuse 103 0 |
| 835 | end |
| 836 | rdn 0 4 |
| 837 | if not magic 84 80 83 49 |
| 838 | refuse 103 0 |
| 839 | end |
| 840 | srew 0 |
| 841 | if not hfile 0 0 sz 0 |
| 842 | refuse 104 0 |
| 843 | end |
| 844 | srew 0 |
| 845 | tpshdr 1 |
| 846 | n = get ST 0 |
| 847 | note Before the body-size check. A short file whose header count is past the cap must refuse |
| 848 | note ARG here; the size check would otherwise report FORMAT and the cap would be unreachable |
| 849 | note without a multi-terabyte body. |
| 850 | ncap n |
| 851 | rb = get ST 3 |
| 852 | if gt n div sub sz 160 rb |
| 853 | refuse 103 0 |
| 854 | end |
| 855 | if ne mul n rb sub sz 160 |
| 856 | refuse 103 0 |
| 857 | end |
| 858 | t = ulong 0 |
| 859 | while lt t n bound 9223372036854775807 |
| 860 | tpsrow t 1 |
| 861 | t = add t 1 |
| 862 | end |
| 863 | end |
| 864 | |
| 865 | fn tsyload a ulong to void |
| 866 | note Validate TSY1 argument a (s.3) and the pair check against the loaded TPS1. |
| 867 | sz = sopen 1 a |
| 868 | if lt sz 88 |
| 869 | refuse 103 0 |
| 870 | end |
| 871 | rdn 1 4 |
| 872 | if not magic 84 83 89 49 |
| 873 | refuse 103 0 |
| 874 | end |
| 875 | srew 1 |
| 876 | if not hfile 1 1 sz 96 |
| 877 | refuse 104 0 |
| 878 | end |
| 879 | srew 1 |
| 880 | rdn 1 56 |
| 881 | k = ulong get HB 6 |
| 882 | ok = true |
| 883 | if ne lev 4 2 1 |
| 884 | ok = false |
| 885 | end |
| 886 | if ne ulong get HB 7 0 |
| 887 | ok = false |
| 888 | end |
| 889 | if ne lev 16 8 0 |
| 890 | ok = false |
| 891 | end |
| 892 | if lt k 2 |
| 893 | ok = false |
| 894 | end |
| 895 | if gt k 16 |
| 896 | ok = false |
| 897 | end |
| 898 | if not ok |
| 899 | refuse 103 0 |
| 900 | end |
| 901 | n = lev 8 8 |
| 902 | if ne n sub sz 88 |
| 903 | refuse 110 0 |
| 904 | end |
| 905 | for i ulong 32 |
| 906 | put DG add 128 i get HB add 24 i |
| 907 | end |
| 908 | put ST 4 k |
| 909 | put ST 5 n |
| 910 | t = ulong 0 |
| 911 | while lt t n bound 9223372036854775807 |
| 912 | b = rdb 1 |
| 913 | if ge b k |
| 914 | refuse 116 t |
| 915 | end |
| 916 | t = add t 1 |
| 917 | end |
| 918 | if ne k get ST 2 |
| 919 | refuse 110 0 |
| 920 | end |
| 921 | if ne n get ST 0 |
| 922 | refuse 110 0 |
| 923 | end |
| 924 | if not dgeq 64 128 |
| 925 | refuse 109 0 |
| 926 | end |
| 927 | end |
| 928 | |
| 929 | note ---------------------------------------------------------------- arguments |
| 930 | |
| 931 | fn argget i ulong to void |
| 932 | o = mul i 4096 |
| 933 | k = argread i AR o 4096 |
| 934 | if lt k 0 |
| 935 | refuse 101 0 |
| 936 | end |
| 937 | put AL i ulong k |
| 938 | end |
| 939 | |
| 940 | fn argis i ulong t view uchar * to bool |
| 941 | if ne get AL i len t |
| 942 | ret false |
| 943 | end |
| 944 | o = mul i 4096 |
| 945 | r = true |
| 946 | for j ulong len t bound 4096 |
| 947 | if ne get AR add o j get t j |
| 948 | r = false |
| 949 | end |
| 950 | end |
| 951 | ret r |
| 952 | end |
| 953 | |
| 954 | note ---------------------------------------------------------------- report |
| 955 | |
| 956 | fn gub q ulong to ulong |
| 957 | note Upper bound, in ub, of 1e6 * log2(1 + 256/q): the most the last symbol of a row can gain |
| 958 | note over its ideal (lower bound, TcrBound.lean encode_lower). Each ub is within |
| 959 | note [-0.500192, +0.49984] of exact (Wolfram W2), so the difference plus 2 is safe; for q > 65280 |
| 960 | note the gain is below its value at 65281, 1e6 * log2(1 + 256/65281) = 5646.48 (Wolfram W3). |
| 961 | if le q 65280 |
| 962 | ret add sub ub q ub add q 256 2 |
| 963 | end |
| 964 | ret 5649 |
| 965 | end |
| 966 | |
| 967 | fn acc s ulong to void |
| 968 | note Add symbol s (row Q) to the ideal length ST[1] and, for the last symbol of the row, to the |
| 969 | note lower-bound gain ST[8]. |
| 970 | q = get Q s |
| 971 | put ST 1 uadd get ST 1 ub q |
| 972 | if eq s sub get ST 2 1 |
| 973 | put ST 8 uadd get ST 8 gub q |
| 974 | end |
| 975 | end |
| 976 | |
| 977 | fn report to void |
| 978 | note ST: 0 n, 1 ideal_ub, 6 payload bytes, 7 renorm shifts, 8 last-symbol gain. Prints the length |
| 979 | note lines and checks the identity and both bounds; a failure is an ALARM (exit 3). |
| 980 | note n is capped in tpsload. cb above 2305843009213 is ARG: coded_bytes*8*1000000 would not fit. |
| 981 | note The additions themselves alarm if they would wrap, which is what a bypassed cap would hit. |
| 982 | n = get ST 0 |
| 983 | pay = get ST 6 |
| 984 | cb = uadd pay 56 |
| 985 | if gt cb 2305843009213 |
| 986 | refuse 101 0 |
| 987 | end |
| 988 | pkv KCNT n |
| 989 | pkv KIDL get ST 1 |
| 990 | pkv KCB cb |
| 991 | pkv KCBT mul cb 8 |
| 992 | pkv KPAY pay |
| 993 | pkv KSHF get ST 7 |
| 994 | bnd = bndsum get ST 1 n |
| 995 | cub = cubit cb |
| 996 | pkv KBND bnd |
| 997 | if ne pay add get ST 7 4 |
| 998 | alarm 1 |
| 999 | end |
| 1000 | if gt cub bnd |
| 1001 | alarm 2 |
| 1002 | end |
| 1003 | pkv KSLK sub bnd cub |
| 1004 | lw = uadd get ST 1 472000000 |
| 1005 | g = uadd n get ST 8 |
| 1006 | lo = ulong 0 |
| 1007 | if gt lw g |
| 1008 | lo = sub lw g |
| 1009 | end |
| 1010 | pkv KLOW lo |
| 1011 | if lt cub lo |
| 1012 | alarm 6 |
| 1013 | end |
| 1014 | ptext KOVH |
| 1015 | if ge cub get ST 1 |
| 1016 | pdec sub cub get ST 1 |
| 1017 | else |
| 1018 | pbyte 45 |
| 1019 | pdec sub get ST 1 cub |
| 1020 | end |
| 1021 | pbyte 10 |
| 1022 | ptext KBOK |
| 1023 | ptext KLOK |
| 1024 | end |
| 1025 | |
| 1026 | fn hdrw t view uchar * to void |
| 1027 | note The 56-byte coded header (s.4) into HB: magic t, version 1, coder 1, count, TPS1 digest, payload. |
| 1028 | for i ulong 56 |
| 1029 | put HB i 0 |
| 1030 | end |
| 1031 | for i ulong 4 |
| 1032 | put HB i get t i |
| 1033 | end |
| 1034 | put HB 4 1 |
| 1035 | put HB 5 1 |
| 1036 | n = get ST 0 |
| 1037 | for i ulong 8 |
| 1038 | put HB add 8 i uchar and shr n mul i 8 255 |
| 1039 | end |
| 1040 | for i ulong 32 |
| 1041 | put HB add 16 i get DG i |
| 1042 | end |
| 1043 | p = get ST 6 |
| 1044 | for i ulong 8 |
| 1045 | put HB add 48 i uchar and shr p mul i 8 255 |
| 1046 | end |
| 1047 | end |
| 1048 | |
| 1049 | text MTCR "TCR1" |
| 1050 | |
| 1051 | fn sameio d ulong i ulong to bool |
| 1052 | r = false |
| 1053 | for s ulong 3 |
| 1054 | if eq get INO mul s 2 d |
| 1055 | if eq get INO add mul s 2 1 i |
| 1056 | r = true |
| 1057 | end |
| 1058 | end |
| 1059 | end |
| 1060 | ret r |
| 1061 | end |
| 1062 | |
| 1063 | fn ocreate a ulong to void |
| 1064 | note Refuse (ARG) an output path that names one of the open inputs (same device and inode): |
| 1065 | note O_TRUNC would destroy the input and the refusal cleanup would then remove it. |
| 1066 | o = mul a 4096 |
| 1067 | k = stat AR o get AL a FS 0 128 |
| 1068 | if eq k 0 |
| 1069 | if sameio lefs 0 lefs 8 |
| 1070 | refuse 101 0 |
| 1071 | end |
| 1072 | end |
| 1073 | f = fopen AR o get AL a 577 420 |
| 1074 | if lt f 0 |
| 1075 | refuse 102 0 |
| 1076 | end |
| 1077 | put OS 0 ulong f |
| 1078 | put OS 1 0 |
| 1079 | put OS 3 0 |
| 1080 | put OS 4 1 |
| 1081 | put OS 5 a |
| 1082 | end |
| 1083 | |
| 1084 | fn ofinish to void |
| 1085 | note Flush, fsync and close. Refuse IO (102) on fsync or fclose failure before clearing |
| 1086 | note OS[4], so cleanup still unlinks the partial output. After a successful close, |
| 1087 | note zero OS[0] so the stale descriptor is not reused by a later fclose. |
| 1088 | oflush |
| 1089 | f = int get OS 0 |
| 1090 | k = fsync f |
| 1091 | z = fclose f |
| 1092 | if lt k 0 |
| 1093 | refuse 102 0 |
| 1094 | end |
| 1095 | if lt z 0 |
| 1096 | refuse 102 0 |
| 1097 | end |
| 1098 | put OS 0 0 |
| 1099 | put OS 4 0 |
| 1100 | end |
| 1101 | |
| 1102 | fn rehbeg s ulong h ulong d ulong to void |
| 1103 | note Coding pass: re-read stream s from byte 0 through hash stream h (domain d). |
| 1104 | srew s |
| 1105 | hbeg h d |
| 1106 | put HSEL s add h 1 |
| 1107 | end |
| 1108 | |
| 1109 | fn rehend s ulong h ulong o ulong w ulong to void |
| 1110 | note End of a coding pass: skip the 32-byte trailer, finish hash h into DG[o..o+32) and refuse |
| 1111 | note DIGEST unless it equals the validation-pass digest DG[w..w+32) (the file changed between |
| 1112 | note the passes). |
| 1113 | put HSEL s 0 |
| 1114 | rdn s 32 |
| 1115 | hend h o |
| 1116 | if not dgeq o w |
| 1117 | refuse 104 0 |
| 1118 | end |
| 1119 | end |
| 1120 | |
| 1121 | note ---------------------------------------------------------------- modes |
| 1122 | |
| 1123 | fn cmdenc to int |
| 1124 | tpsload 3 |
| 1125 | tsyload 4 |
| 1126 | ocreate 5 |
| 1127 | for i ulong 56 |
| 1128 | obyte 0 |
| 1129 | end |
| 1130 | rehbeg 0 0 0 |
| 1131 | tpshdr 0 |
| 1132 | rehbeg 1 1 1 |
| 1133 | rdn 1 56 |
| 1134 | encbeg |
| 1135 | n = get ST 0 |
| 1136 | k = get ST 2 |
| 1137 | put ST 1 0 |
| 1138 | put ST 8 0 |
| 1139 | t = ulong 0 |
| 1140 | while lt t n bound 9223372036854775807 |
| 1141 | tpsrow t 1 |
| 1142 | s = rdb 1 |
| 1143 | if ge s k |
| 1144 | refuse 116 t |
| 1145 | end |
| 1146 | e = encs s |
| 1147 | if ne e 0 |
| 1148 | refuse e t |
| 1149 | end |
| 1150 | acc s |
| 1151 | t = add t 1 |
| 1152 | end |
| 1153 | e = encend |
| 1154 | if ne e 0 |
| 1155 | refuse e n |
| 1156 | end |
| 1157 | rehend 0 0 192 0 |
| 1158 | rehend 1 1 224 96 |
| 1159 | put ST 6 get EN 6 |
| 1160 | put ST 7 get EN 5 |
| 1161 | oflush |
| 1162 | hdrw MTCR |
| 1163 | f = int get OS 0 |
| 1164 | w = pwrite f HB 0 56 long 0 |
| 1165 | if ne w 56 |
| 1166 | refuse 102 0 |
| 1167 | end |
| 1168 | ofinish |
| 1169 | ptext KMODE |
| 1170 | ptext WENC |
| 1171 | pbyte 10 |
| 1172 | ptext KTPS |
| 1173 | phex 0 |
| 1174 | pbyte 10 |
| 1175 | report |
| 1176 | pflush 1 |
| 1177 | ret 0 |
| 1178 | end |
| 1179 | |
| 1180 | fn codhdr a ulong to void |
| 1181 | note Check the coded file header of argument a as stream 2 (s.4 order); DC[3] = payload bytes. |
| 1182 | sz = sopen 2 a |
| 1183 | if lt sz 56 |
| 1184 | refuse 111 0 |
| 1185 | end |
| 1186 | rdn 2 56 |
| 1187 | if not magic 84 67 82 49 |
| 1188 | refuse 111 0 |
| 1189 | end |
| 1190 | if ne ulong get HB 4 1 |
| 1191 | refuse 111 0 |
| 1192 | end |
| 1193 | if ne ulong get HB 5 1 |
| 1194 | refuse 111 0 |
| 1195 | end |
| 1196 | if ne lev 6 2 0 |
| 1197 | refuse 111 0 |
| 1198 | end |
| 1199 | for i ulong 32 |
| 1200 | put DG add 32 i get HB add 16 i |
| 1201 | end |
| 1202 | if not dgeq 0 32 |
| 1203 | refuse 112 0 |
| 1204 | end |
| 1205 | if ne lev 8 8 get ST 0 |
| 1206 | refuse 110 0 |
| 1207 | end |
| 1208 | p = lev 48 8 |
| 1209 | if lt sub sz 56 p |
| 1210 | refuse 113 0 |
| 1211 | end |
| 1212 | if gt sub sz 56 p |
| 1213 | refuse 114 0 |
| 1214 | end |
| 1215 | put DC 3 p |
| 1216 | put DC 4 0 |
| 1217 | end |
| 1218 | |
| 1219 | fn tsyhdr w ulong to void |
| 1220 | note The TSY1 header of the decoded symbols (K, count, dataset digest of the TPS1), fed to hash |
| 1221 | note stream 1 and, when w = 1, written to the output. |
| 1222 | for i ulong 56 |
| 1223 | put HB i 0 |
| 1224 | end |
| 1225 | put HB 0 84 |
| 1226 | put HB 1 83 |
| 1227 | put HB 2 89 |
| 1228 | put HB 3 49 |
| 1229 | put HB 4 1 |
| 1230 | put HB 6 uchar get ST 2 |
| 1231 | n = get ST 0 |
| 1232 | for i ulong 8 |
| 1233 | put HB add 8 i uchar and shr n mul i 8 255 |
| 1234 | end |
| 1235 | for i ulong 32 |
| 1236 | put HB add 24 i get DG add 64 i |
| 1237 | end |
| 1238 | for i ulong 56 |
| 1239 | b = ulong get HB i |
| 1240 | if eq w 1 |
| 1241 | obyte b |
| 1242 | end |
| 1243 | hbyte 1 b |
| 1244 | end |
| 1245 | end |
| 1246 | |
| 1247 | fn decrun w ulong to void |
| 1248 | note Decode all symbols into hash stream 1 (and, w = 1, the output file). Fills ST[1] (ideal), |
| 1249 | note ST[6], ST[7], ST[8]. The TPS1 is re-hashed and its rows re-checked on the way. |
| 1250 | rehbeg 0 0 0 |
| 1251 | tpshdr 0 |
| 1252 | e = decbeg |
| 1253 | if ne e 0 |
| 1254 | refuse e 0 |
| 1255 | end |
| 1256 | n = get ST 0 |
| 1257 | put ST 1 0 |
| 1258 | put ST 8 0 |
| 1259 | t = ulong 0 |
| 1260 | while lt t n bound 9223372036854775807 |
| 1261 | tpsrow t 1 |
| 1262 | s = decs |
| 1263 | if gt s 999 |
| 1264 | refuse sub s 1000 t |
| 1265 | end |
| 1266 | if eq w 1 |
| 1267 | obyte s |
| 1268 | end |
| 1269 | hbyte 1 s |
| 1270 | acc s |
| 1271 | t = add t 1 |
| 1272 | end |
| 1273 | e = decend |
| 1274 | if ne e 0 |
| 1275 | refuse e n |
| 1276 | end |
| 1277 | rehend 0 0 192 0 |
| 1278 | put ST 6 get DC 3 |
| 1279 | put ST 7 get DC 5 |
| 1280 | end |
| 1281 | |
| 1282 | fn cmddec to int |
| 1283 | tpsload 3 |
| 1284 | codhdr 4 |
| 1285 | ocreate 5 |
| 1286 | hbeg 1 1 |
| 1287 | tsyhdr 1 |
| 1288 | decrun 1 |
| 1289 | hend 1 96 |
| 1290 | for i ulong 32 |
| 1291 | obyte ulong get DG add 96 i |
| 1292 | end |
| 1293 | ofinish |
| 1294 | ptext KMODE |
| 1295 | ptext WDEC |
| 1296 | pbyte 10 |
| 1297 | ptext KTSY |
| 1298 | phex 96 |
| 1299 | pbyte 10 |
| 1300 | report |
| 1301 | pflush 1 |
| 1302 | ret 0 |
| 1303 | end |
| 1304 | |
| 1305 | fn cmdver to int |
| 1306 | note Same order as turing-coder verify: probability stream, coded file and full decode, then the |
| 1307 | note symbol file and the pair check, then byte identity of the symbol file with the decoded one |
| 1308 | note (compared through their TSY1 digests: equal digests of domain || header || symbols). |
| 1309 | tpsload 3 |
| 1310 | codhdr 5 |
| 1311 | hbeg 1 1 |
| 1312 | tsyhdr 0 |
| 1313 | decrun 0 |
| 1314 | hend 1 160 |
| 1315 | tsyload 4 |
| 1316 | if not dgeq 160 96 |
| 1317 | refuse 116 0 |
| 1318 | end |
| 1319 | ptext KMODE |
| 1320 | ptext WVER |
| 1321 | pbyte 10 |
| 1322 | ptext KTPS |
| 1323 | phex 0 |
| 1324 | pbyte 10 |
| 1325 | report |
| 1326 | ptext KRT |
| 1327 | pflush 1 |
| 1328 | ret 0 |
| 1329 | end |
| 1330 | |
| 1331 | note ---------------------------------------------------------------- selftest (synthetic streams in memory) |
| 1332 | |
| 1333 | fn rnd to ulong |
| 1334 | x = add mul get RG 0 6364136223846793005 1442695040888963407 |
| 1335 | put RG 0 x |
| 1336 | ret shr x 33 |
| 1337 | end |
| 1338 | |
| 1339 | fn genrow k ulong md ulong to void |
| 1340 | note Row Q[0..k) summing to 65536, every entry >= 1. md 0 random weights, 1 one dominant |
| 1341 | note entry (the rest 1), 2 two-level skew, 3 uniform-ish. |
| 1342 | if eq md 1 |
| 1343 | for x ulong k bound 16 |
| 1344 | put Q x 1 |
| 1345 | end |
| 1346 | d = rem rnd k |
| 1347 | put Q d sub 65537 k |
| 1348 | ret |
| 1349 | end |
| 1350 | w = new ulong 16 0 |
| 1351 | tot = ulong 0 |
| 1352 | for x ulong k bound 16 |
| 1353 | v = add rem rnd 1000 1 |
| 1354 | if eq md 2 |
| 1355 | if eq rem rnd 4 0 |
| 1356 | v = add v 40000 |
| 1357 | end |
| 1358 | end |
| 1359 | if eq md 3 |
| 1360 | v = ulong 1000 |
| 1361 | end |
| 1362 | put w x v |
| 1363 | tot = add tot v |
| 1364 | end |
| 1365 | rest = sub 65536 k |
| 1366 | used = ulong 0 |
| 1367 | for x ulong k bound 16 |
| 1368 | v = add div mul get w x rest tot 1 |
| 1369 | put Q x v |
| 1370 | used = add used v |
| 1371 | end |
| 1372 | d = rem rnd k |
| 1373 | put Q d add get Q d sub 65536 used |
| 1374 | end |
| 1375 | |
| 1376 | fn gensym k ulong md ulong to ulong |
| 1377 | note md 0: sample from Q; md 1: the least likely symbol (worst information per symbol); |
| 1378 | note md 2: always the last symbol of the row (the lower side of the bound). |
| 1379 | if eq md 2 |
| 1380 | ret sub k 1 |
| 1381 | end |
| 1382 | if eq md 1 |
| 1383 | b = ulong 0 |
| 1384 | for x ulong k bound 16 |
| 1385 | if lt get Q x get Q b |
| 1386 | b = x |
| 1387 | end |
| 1388 | end |
| 1389 | ret b |
| 1390 | end |
| 1391 | u = rem rnd 65536 |
| 1392 | c = ulong 0 |
| 1393 | for x ulong k bound 16 |
| 1394 | c = add c get Q x |
| 1395 | if lt u c |
| 1396 | ret x |
| 1397 | end |
| 1398 | end |
| 1399 | ret sub k 1 |
| 1400 | end |
| 1401 | |
| 1402 | fn stenc seed ulong n ulong k ulong md ulong to ulong |
| 1403 | note Encode case (seed, n, k, md) into MB; returns ideal_ub (gain in ST[8]). md: low 2 bits row |
| 1404 | note mode, md >> 2 symbol mode. |
| 1405 | put RG 0 seed |
| 1406 | put ST 2 k |
| 1407 | put OS 3 1 |
| 1408 | put OS 2 0 |
| 1409 | encbeg |
| 1410 | put ST 1 0 |
| 1411 | put ST 8 0 |
| 1412 | t = ulong 0 |
| 1413 | while lt t n bound 9223372036854775807 |
| 1414 | genrow k and md 3 |
| 1415 | s = gensym k shr md 2 |
| 1416 | e = encs s |
| 1417 | if ne e 0 |
| 1418 | alarm 3 |
| 1419 | end |
| 1420 | acc s |
| 1421 | t = add t 1 |
| 1422 | end |
| 1423 | if ne encend 0 |
| 1424 | alarm 3 |
| 1425 | end |
| 1426 | ret get ST 1 |
| 1427 | end |
| 1428 | |
| 1429 | fn stdec seed ulong n ulong k ulong md ulong to ulong |
| 1430 | note Decode MB[0..OS[2]) against the regenerated case; 0 = identical and terminated, 1 = refused or different. |
| 1431 | put RG 0 seed |
| 1432 | put ST 2 k |
| 1433 | put DC 3 get OS 2 |
| 1434 | put DC 4 1 |
| 1435 | if ne decbeg 0 |
| 1436 | ret 1 |
| 1437 | end |
| 1438 | t = ulong 0 |
| 1439 | while lt t n bound 9223372036854775807 |
| 1440 | genrow k and md 3 |
| 1441 | s = gensym k shr md 2 |
| 1442 | d = decs |
| 1443 | if ne d s |
| 1444 | ret 1 |
| 1445 | end |
| 1446 | t = add t 1 |
| 1447 | end |
| 1448 | if ne decend 0 |
| 1449 | ret 1 |
| 1450 | end |
| 1451 | ret 0 |
| 1452 | end |
| 1453 | |
| 1454 | fn flip pay ulong to void |
| 1455 | note XOR one random payload byte of MB[0..pay) with a nonzero value. |
| 1456 | p = rem rnd pay |
| 1457 | x = add rem rnd 255 1 |
| 1458 | put MB p uchar xor ulong get MB p x |
| 1459 | end |
| 1460 | |
| 1461 | fn stlow n ulong idl ulong cub ulong to void |
| 1462 | note Lower bound for a selftest case (ST[8] = last-symbol gain), its smallest slack (ST[27]) and |
| 1463 | note the largest amount any case codes below its ideal (ST[28]). |
| 1464 | lw = uadd idl 472000000 |
| 1465 | g = uadd n get ST 8 |
| 1466 | lo = ulong 0 |
| 1467 | if gt lw g |
| 1468 | lo = sub lw g |
| 1469 | end |
| 1470 | if lt cub lo |
| 1471 | alarm 6 |
| 1472 | end |
| 1473 | if lt sub cub lo get ST 27 |
| 1474 | put ST 27 sub cub lo |
| 1475 | end |
| 1476 | if gt idl cub |
| 1477 | if gt sub idl cub get ST 28 |
| 1478 | put ST 28 sub idl cub |
| 1479 | end |
| 1480 | end |
| 1481 | end |
| 1482 | |
| 1483 | fn stcase seed ulong n ulong k ulong md ulong to void |
| 1484 | note One case: round trip, length identity, both bounds, then flip one payload byte and require |
| 1485 | note detection. |
| 1486 | idl = stenc seed n k md |
| 1487 | pay = get OS 2 |
| 1488 | if ne pay add get EN 5 4 |
| 1489 | alarm 1 |
| 1490 | end |
| 1491 | bnd = bndsum idl n |
| 1492 | cub = cubit uadd pay 56 |
| 1493 | if gt cub bnd |
| 1494 | alarm 2 |
| 1495 | end |
| 1496 | stlow n idl cub |
| 1497 | if lt sub bnd cub get ST 20 |
| 1498 | put ST 20 sub bnd cub |
| 1499 | end |
| 1500 | if gt n 0 |
| 1501 | if lt sub bnd cub get ST 24 |
| 1502 | put ST 24 sub bnd cub |
| 1503 | end |
| 1504 | end |
| 1505 | if ne stdec seed n k md 0 |
| 1506 | alarm 3 |
| 1507 | end |
| 1508 | if ne get DC 3 add get DC 5 4 |
| 1509 | alarm 1 |
| 1510 | end |
| 1511 | flip pay |
| 1512 | if eq stdec seed n k md 0 |
| 1513 | alarm 4 |
| 1514 | end |
| 1515 | put ST 21 add get ST 21 1 |
| 1516 | put ST 22 add get ST 22 1 |
| 1517 | put ST 23 add get ST 23 n |
| 1518 | end |
| 1519 | |
| 1520 | fn ubchk to void |
| 1521 | note Exhaustive check of the s.8 ub rule: sum of ub(q) and sum of q * ub(q) mod 2^64 over |
| 1522 | note q = 1..65535 must equal the values Wolfram computed from the same rule |
| 1523 | note (docs/turing/TURING_YIELD_RANGE_CODER_OM.md, receipt W2): 94539136349 and 1549081999916376. |
| 1524 | note The same Wolfram run gives max(exact - ub) = 0.49984 ub, inside the 0.501 the bound uses. |
| 1525 | s1 = ulong 0 |
| 1526 | s2 = ulong 0 |
| 1527 | q = ulong 1 |
| 1528 | while lt q 65536 bound 65535 |
| 1529 | v = ub q |
| 1530 | s1 = add s1 v |
| 1531 | s2 = add s2 mul q v |
| 1532 | q = add q 1 |
| 1533 | end |
| 1534 | put ST 25 s1 |
| 1535 | put ST 26 s2 |
| 1536 | if ne s1 94539136349 |
| 1537 | alarm 5 |
| 1538 | end |
| 1539 | if ne s2 1549081999916376 |
| 1540 | alarm 5 |
| 1541 | end |
| 1542 | end |
| 1543 | |
| 1544 | fn ceilchk to void |
| 1545 | note The last legal n and the last legal coded length still fit. cmdovf is one past the n cap. |
| 1546 | note A constant that no longer fits alarms here and selftest does not pass. |
| 1547 | ncap 1152514738906 |
| 1548 | z = bndsum umul 1152514738906 16000000 1152514738906 |
| 1549 | y = cubit 2305843009213 |
| 1550 | if eq z 0 |
| 1551 | alarm 7 |
| 1552 | end |
| 1553 | if eq y 0 |
| 1554 | alarm 7 |
| 1555 | end |
| 1556 | end |
| 1557 | |
| 1558 | fn cmdovf to int |
| 1559 | note n = cap+1 at ub(q=1) per symbol. 16005647*(cap+1) + 480000000 does not fit in 64 bits. |
| 1560 | note Must ALARM 7. Returning 0 means the sum wrapped or the check was removed. |
| 1561 | z = bndsum umul 1152514738907 16000000 1152514738907 |
| 1562 | put ST 29 z |
| 1563 | ret 0 |
| 1564 | end |
| 1565 | |
| 1566 | fn cmdself to int |
| 1567 | note Cases: K 2..16, all four row modes, three symbol modes, n from 0 to 200000. |
| 1568 | ceilchk |
| 1569 | put ST 20 18446744073709551615 |
| 1570 | put ST 24 18446744073709551615 |
| 1571 | put ST 27 18446744073709551615 |
| 1572 | put ST 28 0 |
| 1573 | ubchk |
| 1574 | put ST 21 0 |
| 1575 | put ST 22 0 |
| 1576 | put ST 23 0 |
| 1577 | seed = ulong 1 |
| 1578 | for k ulong 15 |
| 1579 | for md ulong 12 |
| 1580 | stcase seed 0 add k 2 md |
| 1581 | seed = add seed 1 |
| 1582 | stcase seed 1 add k 2 md |
| 1583 | seed = add seed 1 |
| 1584 | stcase seed 37 add k 2 md |
| 1585 | seed = add seed 1 |
| 1586 | stcase seed 4000 add k 2 md |
| 1587 | seed = add seed 1 |
| 1588 | end |
| 1589 | end |
| 1590 | stcase 777 200000 9 0 |
| 1591 | stcase 778 200000 2 4 |
| 1592 | stcase 779 200000 16 5 |
| 1593 | stcase 780 200000 16 7 |
| 1594 | stcase 781 200000 16 9 |
| 1595 | stcase 782 200000 2 9 |
| 1596 | ptext KUB |
| 1597 | pdec get ST 25 |
| 1598 | ptext KUBQ |
| 1599 | pdec get ST 26 |
| 1600 | pbyte 10 |
| 1601 | ptext KCASE |
| 1602 | pdec get ST 22 |
| 1603 | ptext KSYM |
| 1604 | pdec get ST 23 |
| 1605 | ptext KFLIP |
| 1606 | pdec get ST 21 |
| 1607 | ptext KMIN |
| 1608 | pdec get ST 20 |
| 1609 | ptext KMIN1 |
| 1610 | pdec get ST 24 |
| 1611 | ptext KLMIN |
| 1612 | pdec get ST 27 |
| 1613 | ptext KBELOW |
| 1614 | pdec get ST 28 |
| 1615 | ptext KPASS |
| 1616 | pflush 1 |
| 1617 | ret 0 |
| 1618 | end |
| 1619 | |
| 1620 | fn main to int |
| 1621 | for i ulong 8 |
| 1622 | put INO i 18446744073709551615 |
| 1623 | end |
| 1624 | c = argcnt |
| 1625 | if eq c 2 |
| 1626 | argget 1 |
| 1627 | if argis 1 WSLF |
| 1628 | ret cmdself |
| 1629 | end |
| 1630 | end |
| 1631 | if eq c 3 |
| 1632 | argget 1 |
| 1633 | if argis 1 WSLF |
| 1634 | argget 2 |
| 1635 | if argis 2 WOVF |
| 1636 | ret cmdovf |
| 1637 | end |
| 1638 | end |
| 1639 | end |
| 1640 | if ne c 6 |
| 1641 | ptext TUSE |
| 1642 | pflush 2 |
| 1643 | ret 1 |
| 1644 | end |
| 1645 | for i ulong 6 |
| 1646 | argget i |
| 1647 | end |
| 1648 | if not argis 2 WRNG |
| 1649 | ptext TUSE |
| 1650 | pflush 2 |
| 1651 | ret 1 |
| 1652 | end |
| 1653 | if argis 1 WENC |
| 1654 | ret cmdenc |
| 1655 | end |
| 1656 | if argis 1 WDEC |
| 1657 | ret cmddec |
| 1658 | end |
| 1659 | if argis 1 WVER |
| 1660 | ret cmdver |
| 1661 | end |
| 1662 | ptext TUSE |
| 1663 | pflush 2 |
| 1664 | ret 1 |
| 1665 | end |