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_produce.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_produce.om: Predictor side of EXP-001 in pure seeded OM-2. |
| 5 | note Decodes TYM0 models, reads CTR1 search traces, produces (TPS1, TSY1). |
| 6 | note Composed after seed/ng/sha256.om, tc_pstream.om, tc_range_lib.om, tc_rans.om. |
| 7 | |
| 8 | const TP_CTR1_BYTES ulong 247 |
| 9 | const TP_MAX_ROWS ulong 65536 |
| 10 | const TP_MAX_MBUF ulong 4194304 |
| 11 | const TP_MAX_EV ulong 500000 |
| 12 | |
| 13 | text TP_W_MBAD "invalid TYM0 model file" |
| 14 | text TP_W_CBAD "invalid CTR1 trace file" |
| 15 | text TP_W_SYM "event symbol outside model alphabet" |
| 16 | text TP_W_ROW "model row entry out of 1..65535" |
| 17 | text TP_W_SCORE "probability stream ideal differs from model score" |
| 18 | |
| 19 | note Model state: 0=K, 1=mask, 2=keybits, 3=nrows, 4=lm_bits |
| 20 | global tp_mst ulong 5 |
| 21 | global tp_mdef ushort 16 |
| 22 | global tp_mkeys ulong 65536 |
| 23 | global tp_mq ushort 1048576 |
| 24 | global tp_mdig uchar 32 |
| 25 | global tp_mbuf uchar 4194304 |
| 26 | global tp_mbuf_len ulong 1 |
| 27 | |
| 28 | note Bit reader / writer state: 0=nbits, 1=pos, 2=err |
| 29 | global tp_br ulong 3 |
| 30 | |
| 31 | note Key space result: 0=space, 1=bits |
| 32 | global tp_ks ulong 2 |
| 33 | |
| 34 | note CTR1 event stream state |
| 35 | global tp_ev_n ulong 1 |
| 36 | global tp_ev_sym uchar 500000 |
| 37 | global tp_ev_op uchar 500000 |
| 38 | global tp_ev_depth uchar 500000 |
| 39 | global tp_ev_first uchar 500000 |
| 40 | global tp_ev_crumb ushort 500000 |
| 41 | global tp_ev_idx uint 500000 |
| 42 | global tp_cbuf uchar 65536 |
| 43 | note CTR1 reader state: 0=n, 1=prev_idx, 2=crumb |
| 44 | global tp_cst long 3 |
| 45 | |
| 46 | note Context walk prev[0..4] |
| 47 | global tp_walk_p ulong 5 |
| 48 | |
| 49 | note Working digests for produce |
| 50 | global tp_prof_d uchar 32 |
| 51 | global tp_dat_d uchar 32 |
| 52 | |
| 53 | note ---------------------------------------------------------------- key space |
| 54 | |
| 55 | fn tp_key_space mask ulong k ulong to int |
| 56 | if gt mask 255 |
| 57 | ret TC_E_ARG |
| 58 | end |
| 59 | if lt k TC_KMIN |
| 60 | ret TC_E_ARG |
| 61 | end |
| 62 | if gt k TC_KMAX |
| 63 | ret TC_E_ARG |
| 64 | end |
| 65 | lim = ulong 4611686018427387904 |
| 66 | p = ulong 1 |
| 67 | if ne and mask 1 0 |
| 68 | if gt p div lim 16 |
| 69 | ret TC_E_FORMAT |
| 70 | end |
| 71 | p = mul p 16 |
| 72 | end |
| 73 | if ne and mask 2 0 |
| 74 | if gt p div lim 8 |
| 75 | ret TC_E_FORMAT |
| 76 | end |
| 77 | p = mul p 8 |
| 78 | end |
| 79 | kp1 = add k 1 |
| 80 | if ne and mask 4 0 |
| 81 | if gt p div lim kp1 |
| 82 | ret TC_E_FORMAT |
| 83 | end |
| 84 | p = mul p kp1 |
| 85 | end |
| 86 | if ne and mask 8 0 |
| 87 | if gt p div lim kp1 |
| 88 | ret TC_E_FORMAT |
| 89 | end |
| 90 | p = mul p kp1 |
| 91 | end |
| 92 | if ne and mask 16 0 |
| 93 | if gt p div lim kp1 |
| 94 | ret TC_E_FORMAT |
| 95 | end |
| 96 | p = mul p kp1 |
| 97 | end |
| 98 | if ne and mask 64 0 |
| 99 | if gt p div lim kp1 |
| 100 | ret TC_E_FORMAT |
| 101 | end |
| 102 | p = mul p kp1 |
| 103 | end |
| 104 | if ne and mask 128 0 |
| 105 | if gt p div lim kp1 |
| 106 | ret TC_E_FORMAT |
| 107 | end |
| 108 | p = mul p kp1 |
| 109 | end |
| 110 | if ne and mask 32 0 |
| 111 | if gt p div lim 68719476736 |
| 112 | ret TC_E_FORMAT |
| 113 | end |
| 114 | p = mul p 68719476736 |
| 115 | end |
| 116 | put tp_ks 0 p |
| 117 | if le p 1 |
| 118 | put tp_ks 1 0 |
| 119 | else |
| 120 | put tp_ks 1 add tc_hibit sub p 1 1 |
| 121 | end |
| 122 | ret TC_OK |
| 123 | end |
| 124 | |
| 125 | note ---------------------------------------------------------------- TYM0 bit reader/writer |
| 126 | |
| 127 | fn tp_br_get n ulong to ulong |
| 128 | v = ulong 0 |
| 129 | for i ulong n bound 64 |
| 130 | pos = get tp_br 1 |
| 131 | if ge pos get tp_br 0 |
| 132 | put tp_br 2 1 |
| 133 | ret 0 |
| 134 | end |
| 135 | b = ulong get tp_mbuf shr pos 3 |
| 136 | bit = and shr b sub 7 and pos 7 1 |
| 137 | v = or shl v 1 bit |
| 138 | put tp_br 1 add pos 1 |
| 139 | end |
| 140 | ret v |
| 141 | end |
| 142 | |
| 143 | fn tp_bw_put v ulong n ulong to void |
| 144 | i = n |
| 145 | while gt i 0 bound 64 |
| 146 | i = sub i 1 |
| 147 | pos = get tp_br 1 |
| 148 | bi = shr pos 3 |
| 149 | if ne and shr v i 1 0 |
| 150 | cur = ulong get tp_mbuf bi |
| 151 | put tp_mbuf bi uchar or cur shr 128 and pos 7 |
| 152 | end |
| 153 | put tp_br 1 add pos 1 |
| 154 | end |
| 155 | end |
| 156 | |
| 157 | fn tp_model_digest_buf nbytes ulong to void |
| 158 | tc_sha_init |
| 159 | tc_sha_dom TC_DOM_YM |
| 160 | for i ulong nbytes bound 4194304 |
| 161 | tc_sha_byte get tp_mbuf i |
| 162 | end |
| 163 | tc_sha_final |
| 164 | for i ulong 32 |
| 165 | put tp_mdig i get tc_sh_out i |
| 166 | end |
| 167 | end |
| 168 | |
| 169 | fn tp_model_uniform k ulong to int |
| 170 | if lt k TC_KMIN |
| 171 | ret TC_E_ARG |
| 172 | end |
| 173 | if gt k TC_KMAX |
| 174 | ret TC_E_ARG |
| 175 | end |
| 176 | put tp_mst 0 k |
| 177 | put tp_mst 1 0 |
| 178 | put tp_mst 2 0 |
| 179 | put tp_mst 3 0 |
| 180 | qbase = div TC_QONE k |
| 181 | qrem = rem TC_QONE k |
| 182 | for s ulong k bound 16 |
| 183 | v = qbase |
| 184 | if lt s qrem |
| 185 | v = add v 1 |
| 186 | end |
| 187 | put tp_mdef s ushort v |
| 188 | end |
| 189 | for i ulong 64 |
| 190 | put tp_mbuf i 0 |
| 191 | end |
| 192 | put tp_br 1 0 |
| 193 | tp_bw_put 1415138608 32 |
| 194 | tp_bw_put 0 8 |
| 195 | tp_bw_put k 8 |
| 196 | tp_bw_put 0 8 |
| 197 | tp_bw_put 16 8 |
| 198 | tp_bw_put 0 8 |
| 199 | tp_bw_put 0 32 |
| 200 | for s ulong sub k 1 bound 16 |
| 201 | tp_bw_put ulong get tp_mdef s 16 |
| 202 | end |
| 203 | nbits = get tp_br 1 |
| 204 | nbytes = div add nbits 7 8 |
| 205 | put tp_mst 4 nbits |
| 206 | put tp_mbuf_len 0 nbytes |
| 207 | tp_model_digest_buf nbytes |
| 208 | ret TC_OK |
| 209 | end |
| 210 | |
| 211 | fn tp_get_row_def k ulong to int |
| 212 | s = ulong 0 |
| 213 | for x ulong sub k 1 bound 16 |
| 214 | v = tp_br_get 16 |
| 215 | if eq v 0 |
| 216 | ret TC_E_FORMAT |
| 217 | end |
| 218 | put tp_mdef x ushort v |
| 219 | s = add s v |
| 220 | end |
| 221 | if ge s TC_QONE |
| 222 | ret TC_E_FORMAT |
| 223 | end |
| 224 | put tp_mdef sub k 1 ushort sub TC_QONE s |
| 225 | if ne get tp_br 2 0 |
| 226 | ret TC_E_FORMAT |
| 227 | end |
| 228 | ret TC_OK |
| 229 | end |
| 230 | |
| 231 | fn tp_get_row_idx row_i ulong k ulong to int |
| 232 | s = ulong 0 |
| 233 | base = mul row_i 16 |
| 234 | for x ulong sub k 1 bound 16 |
| 235 | v = tp_br_get 16 |
| 236 | if eq v 0 |
| 237 | ret TC_E_FORMAT |
| 238 | end |
| 239 | put tp_mq add base x ushort v |
| 240 | s = add s v |
| 241 | end |
| 242 | if ge s TC_QONE |
| 243 | ret TC_E_FORMAT |
| 244 | end |
| 245 | put tp_mq add base sub k 1 ushort sub TC_QONE s |
| 246 | if ne get tp_br 2 0 |
| 247 | ret TC_E_FORMAT |
| 248 | end |
| 249 | ret TC_OK |
| 250 | end |
| 251 | |
| 252 | fn tp_decode_rows nrows ulong k ulong kb ulong to int |
| 253 | space = get tp_ks 0 |
| 254 | for i ulong nrows bound 65536 |
| 255 | rk = tp_br_get kb |
| 256 | if ge rk space |
| 257 | tc_why_set TP_W_MBAD |
| 258 | ret TC_E_FORMAT |
| 259 | end |
| 260 | if gt i 0 |
| 261 | if le rk get tp_mkeys sub i 1 |
| 262 | tc_why_set TP_W_MBAD |
| 263 | ret TC_E_FORMAT |
| 264 | end |
| 265 | end |
| 266 | put tp_mkeys i rk |
| 267 | if ne tp_get_row_idx i k TC_OK |
| 268 | tc_why_set TP_W_MBAD |
| 269 | ret TC_E_FORMAT |
| 270 | end |
| 271 | end |
| 272 | ret TC_OK |
| 273 | end |
| 274 | |
| 275 | fn tp_model_decode nbytes ulong to int |
| 276 | put tp_br 0 mul nbytes 8 |
| 277 | put tp_br 1 0 |
| 278 | put tp_br 2 0 |
| 279 | mg = tp_br_get 32 |
| 280 | if ne get tp_br 2 0 |
| 281 | tc_why_set TP_W_MBAD |
| 282 | ret TC_E_FORMAT |
| 283 | end |
| 284 | if ne mg 1415138608 |
| 285 | tc_why_set TP_W_MBAD |
| 286 | ret TC_E_FORMAT |
| 287 | end |
| 288 | ver = tp_br_get 8 |
| 289 | k = tp_br_get 8 |
| 290 | mask = tp_br_get 8 |
| 291 | qb = tp_br_get 8 |
| 292 | kb = tp_br_get 8 |
| 293 | nrows = tp_br_get 32 |
| 294 | if ne get tp_br 2 0 |
| 295 | tc_why_set TP_W_MBAD |
| 296 | ret TC_E_FORMAT |
| 297 | end |
| 298 | if ne ver 0 |
| 299 | tc_why_set TP_W_MBAD |
| 300 | ret TC_E_FORMAT |
| 301 | end |
| 302 | if ne qb 16 |
| 303 | tc_why_set TP_W_MBAD |
| 304 | ret TC_E_FORMAT |
| 305 | end |
| 306 | if ne tp_key_space mask k TC_OK |
| 307 | tc_why_set TP_W_MBAD |
| 308 | ret TC_E_FORMAT |
| 309 | end |
| 310 | if ne kb get tp_ks 1 |
| 311 | tc_why_set TP_W_MBAD |
| 312 | ret TC_E_FORMAT |
| 313 | end |
| 314 | row_bits = add kb mul sub k 1 16 |
| 315 | need = add add 104 mul sub k 1 16 mul nrows row_bits |
| 316 | if ne div add need 7 8 nbytes |
| 317 | tc_why_set TP_W_MBAD |
| 318 | ret TC_E_FORMAT |
| 319 | end |
| 320 | if gt nrows TP_MAX_ROWS |
| 321 | tc_why_set TC_W_IO |
| 322 | ret TC_E_IO |
| 323 | end |
| 324 | if ne tp_get_row_def k TC_OK |
| 325 | tc_why_set TP_W_MBAD |
| 326 | ret TC_E_FORMAT |
| 327 | end |
| 328 | if ne tp_decode_rows nrows k kb TC_OK |
| 329 | ret TC_E_FORMAT |
| 330 | end |
| 331 | while lt get tp_br 1 get tp_br 0 bound 8 |
| 332 | if ne tp_br_get 1 0 |
| 333 | tc_why_set TP_W_MBAD |
| 334 | ret TC_E_FORMAT |
| 335 | end |
| 336 | end |
| 337 | put tp_mst 0 k |
| 338 | put tp_mst 1 mask |
| 339 | put tp_mst 2 kb |
| 340 | put tp_mst 3 nrows |
| 341 | put tp_mst 4 need |
| 342 | tp_model_digest_buf nbytes |
| 343 | ret TC_OK |
| 344 | end |
| 345 | |
| 346 | fn tp_copy_io_mbuf tot ulong kn ulong to void |
| 347 | for i ulong kn bound 65536 |
| 348 | put tp_mbuf add tot i get tc_iobuf i |
| 349 | end |
| 350 | end |
| 351 | |
| 352 | fn tc_load_model path view uchar * off ulong plen ulong to int |
| 353 | fd = fopen path off plen 0 0 |
| 354 | if lt fd 0 |
| 355 | tc_why_set TC_W_IO |
| 356 | ret TC_E_IO |
| 357 | end |
| 358 | tot = ulong 0 |
| 359 | while true bound 1000 |
| 360 | k = read int fd tc_iobuf 0 65536 |
| 361 | if lt k 0 |
| 362 | z = fclose int fd |
| 363 | tc_why_set TC_W_IO |
| 364 | ret TC_E_IO |
| 365 | end |
| 366 | if eq k 0 |
| 367 | break |
| 368 | end |
| 369 | kn = ulong k |
| 370 | if gt add tot kn TP_MAX_MBUF |
| 371 | z = fclose int fd |
| 372 | tc_why_set TC_W_IO |
| 373 | ret TC_E_IO |
| 374 | end |
| 375 | tp_copy_io_mbuf tot kn |
| 376 | tot = add tot kn |
| 377 | end |
| 378 | z = fclose int fd |
| 379 | put tp_mbuf_len 0 tot |
| 380 | ret tp_model_decode tot |
| 381 | end |
| 382 | |
| 383 | note ---------------------------------------------------------------- CTR1 reader |
| 384 | |
| 385 | fn tp_cb_u16 o ulong to ulong |
| 386 | b0 = ulong get tp_cbuf o |
| 387 | b1 = ulong get tp_cbuf add o 1 |
| 388 | ret or b0 shl b1 8 |
| 389 | end |
| 390 | |
| 391 | fn tp_cb_u32 o ulong to ulong |
| 392 | b0 = ulong get tp_cbuf o |
| 393 | b1 = ulong get tp_cbuf add o 1 |
| 394 | b2 = ulong get tp_cbuf add o 2 |
| 395 | b3 = ulong get tp_cbuf add o 3 |
| 396 | ret or or b0 shl b1 8 or shl b2 16 shl b3 24 |
| 397 | end |
| 398 | |
| 399 | fn tp_ctr1_chk_hdr o ulong to int |
| 400 | if ne get tp_cbuf o 67 |
| 401 | ret TC_E_FORMAT |
| 402 | end |
| 403 | if ne get tp_cbuf add o 1 84 |
| 404 | ret TC_E_FORMAT |
| 405 | end |
| 406 | if ne get tp_cbuf add o 2 82 |
| 407 | ret TC_E_FORMAT |
| 408 | end |
| 409 | if ne get tp_cbuf add o 3 49 |
| 410 | ret TC_E_FORMAT |
| 411 | end |
| 412 | if ne tp_cb_u16 add o 4 1 |
| 413 | ret TC_E_FORMAT |
| 414 | end |
| 415 | if ne get tp_cbuf add o 22 0 |
| 416 | ret TC_E_FORMAT |
| 417 | end |
| 418 | if gt get tp_cbuf add o 24 4 |
| 419 | ret TC_E_FORMAT |
| 420 | end |
| 421 | if gt get tp_cbuf add o 25 2 |
| 422 | ret TC_E_FORMAT |
| 423 | end |
| 424 | ret TC_OK |
| 425 | end |
| 426 | |
| 427 | fn tp_ctr1_decode_one o ulong prev_idx long out_i ulong to int |
| 428 | if ne tp_ctr1_chk_hdr o TC_OK |
| 429 | ret TC_E_FORMAT |
| 430 | end |
| 431 | kind = ulong get tp_cbuf add o 6 |
| 432 | rc = ulong get tp_cbuf add o 7 |
| 433 | prune = ulong get tp_cbuf add o 23 |
| 434 | sym = ulong 0 |
| 435 | evop = ulong 0 |
| 436 | if eq kind 1 |
| 437 | op = tp_cb_u16 add o 20 |
| 438 | if ge op 15 |
| 439 | ret TC_E_FORMAT |
| 440 | end |
| 441 | if gt prune 4 |
| 442 | ret TC_E_FORMAT |
| 443 | end |
| 444 | if ne prune 0 |
| 445 | if ne rc 1 |
| 446 | ret TC_E_FORMAT |
| 447 | end |
| 448 | sym = sub prune 1 |
| 449 | else |
| 450 | if lt rc 2 |
| 451 | ret TC_E_FORMAT |
| 452 | end |
| 453 | if gt rc 4 |
| 454 | ret TC_E_FORMAT |
| 455 | end |
| 456 | sym = add 4 sub rc 2 |
| 457 | end |
| 458 | evop = op |
| 459 | else |
| 460 | if eq kind 2 |
| 461 | if ne prune 0 |
| 462 | ret TC_E_FORMAT |
| 463 | end |
| 464 | if lt rc 5 |
| 465 | ret TC_E_FORMAT |
| 466 | end |
| 467 | if gt rc 7 |
| 468 | ret TC_E_FORMAT |
| 469 | end |
| 470 | if eq rc 5 |
| 471 | sym = ulong 7 |
| 472 | else |
| 473 | sym = ulong 8 |
| 474 | end |
| 475 | evop = ulong 15 |
| 476 | else |
| 477 | ret TC_E_FORMAT |
| 478 | end |
| 479 | end |
| 480 | put tp_ev_sym out_i uchar sym |
| 481 | put tp_ev_op out_i uchar evop |
| 482 | depth = tp_cb_u16 add o 29 |
| 483 | if gt depth 7 |
| 484 | depth = ulong 7 |
| 485 | end |
| 486 | put tp_ev_depth out_i uchar depth |
| 487 | idx = tp_cb_u32 add o 8 |
| 488 | if eq idx 0 |
| 489 | put tp_ev_first out_i 1 |
| 490 | else |
| 491 | if lt prev_idx 0 |
| 492 | ret TC_E_FORMAT |
| 493 | end |
| 494 | if le long idx prev_idx |
| 495 | ret TC_E_FORMAT |
| 496 | end |
| 497 | put tp_ev_first out_i 0 |
| 498 | end |
| 499 | put tp_ev_idx out_i uint idx |
| 500 | ret TC_OK |
| 501 | end |
| 502 | |
| 503 | fn tp_ctr1_batch nrec ulong to int |
| 504 | n = ulong get tp_cst 0 |
| 505 | prev = get tp_cst 1 |
| 506 | crumb = ulong get tp_cst 2 |
| 507 | for i ulong nrec bound 512 |
| 508 | if ge n TP_MAX_EV |
| 509 | tc_why_set TC_W_IO |
| 510 | ret TC_E_IO |
| 511 | end |
| 512 | rc = tp_ctr1_decode_one mul i TP_CTR1_BYTES prev n |
| 513 | if ne rc TC_OK |
| 514 | tc_why_set TP_W_CBAD |
| 515 | ret TC_E_FORMAT |
| 516 | end |
| 517 | if ne get tp_ev_first n 0 |
| 518 | if gt n 0 |
| 519 | crumb = add crumb 1 |
| 520 | end |
| 521 | end |
| 522 | cv = crumb |
| 523 | if gt cv 65535 |
| 524 | cv = ulong 65535 |
| 525 | end |
| 526 | put tp_ev_crumb n ushort cv |
| 527 | prev = long get tp_ev_idx n |
| 528 | n = add n 1 |
| 529 | end |
| 530 | put tp_cst 0 long n |
| 531 | put tp_cst 1 prev |
| 532 | put tp_cst 2 long crumb |
| 533 | ret TC_OK |
| 534 | end |
| 535 | |
| 536 | fn tp_ctr1_shift used ulong carry ulong to void |
| 537 | for j ulong carry bound 256 |
| 538 | put tp_cbuf j get tp_cbuf add used j |
| 539 | end |
| 540 | end |
| 541 | |
| 542 | fn tp_ctr1_read path view uchar * off ulong plen ulong to int |
| 543 | fd = fopen path off plen 0 0 |
| 544 | if lt fd 0 |
| 545 | tc_why_set TC_W_IO |
| 546 | ret TC_E_IO |
| 547 | end |
| 548 | put tp_cst 0 0 |
| 549 | put tp_cst 1 -1 |
| 550 | put tp_cst 2 0 |
| 551 | carry = ulong 0 |
| 552 | while true bound 100000 |
| 553 | k = read int fd tp_cbuf carry sub 65536 carry |
| 554 | if lt k 0 |
| 555 | z = fclose int fd |
| 556 | tc_why_set TC_W_IO |
| 557 | ret TC_E_IO |
| 558 | end |
| 559 | if eq k 0 |
| 560 | break |
| 561 | end |
| 562 | avail = add carry ulong k |
| 563 | nrec = div avail TP_CTR1_BYTES |
| 564 | rc = tp_ctr1_batch nrec |
| 565 | if ne rc TC_OK |
| 566 | z = fclose int fd |
| 567 | ret rc |
| 568 | end |
| 569 | used = mul nrec TP_CTR1_BYTES |
| 570 | carry = sub avail used |
| 571 | tp_ctr1_shift used carry |
| 572 | end |
| 573 | z = fclose int fd |
| 574 | if ne carry 0 |
| 575 | tc_why_set TP_W_CBAD |
| 576 | ret TC_E_FORMAT |
| 577 | end |
| 578 | put tp_ev_n 0 ulong get tp_cst 0 |
| 579 | ret TC_OK |
| 580 | end |
| 581 | |
| 582 | note ---------------------------------------------------------------- context walk & lookup & tc_produce |
| 583 | |
| 584 | fn tp_walk_key t ulong mask ulong k ulong to ulong |
| 585 | if ne get tp_ev_first t 0 |
| 586 | for i ulong 5 |
| 587 | put tp_walk_p i k |
| 588 | end |
| 589 | end |
| 590 | key = ulong 0 |
| 591 | m = ulong 1 |
| 592 | if ne and mask 1 0 |
| 593 | key = add key mul ulong get tp_ev_op t m |
| 594 | m = mul m 16 |
| 595 | end |
| 596 | if ne and mask 2 0 |
| 597 | key = add key mul ulong get tp_ev_depth t m |
| 598 | m = mul m 8 |
| 599 | end |
| 600 | kp1 = add k 1 |
| 601 | if ne and mask 4 0 |
| 602 | key = add key mul get tp_walk_p 0 m |
| 603 | m = mul m kp1 |
| 604 | end |
| 605 | if ne and mask 8 0 |
| 606 | key = add key mul get tp_walk_p 1 m |
| 607 | m = mul m kp1 |
| 608 | end |
| 609 | if ne and mask 16 0 |
| 610 | key = add key mul get tp_walk_p 2 m |
| 611 | m = mul m kp1 |
| 612 | end |
| 613 | if ne and mask 64 0 |
| 614 | key = add key mul get tp_walk_p 3 m |
| 615 | m = mul m kp1 |
| 616 | end |
| 617 | if ne and mask 128 0 |
| 618 | key = add key mul get tp_walk_p 4 m |
| 619 | m = mul m kp1 |
| 620 | end |
| 621 | if ne and mask 32 0 |
| 622 | idx = ulong get tp_ev_idx t |
| 623 | if ge idx 1048576 |
| 624 | idx = ulong 1048575 |
| 625 | end |
| 626 | posv = or shl ulong get tp_ev_crumb t 20 idx |
| 627 | key = add key mul posv m |
| 628 | end |
| 629 | ret key |
| 630 | end |
| 631 | |
| 632 | fn tp_walk_adv sym ulong to void |
| 633 | put tp_walk_p 4 get tp_walk_p 3 |
| 634 | put tp_walk_p 3 get tp_walk_p 2 |
| 635 | put tp_walk_p 2 get tp_walk_p 1 |
| 636 | put tp_walk_p 1 get tp_walk_p 0 |
| 637 | put tp_walk_p 0 sym |
| 638 | end |
| 639 | |
| 640 | fn tp_lookup key ulong to long |
| 641 | nrows = get tp_mst 3 |
| 642 | lo = ulong 0 |
| 643 | hi = nrows |
| 644 | while lt lo hi bound 32 |
| 645 | mid = add lo shr sub hi lo 1 |
| 646 | if lt get tp_mkeys mid key |
| 647 | lo = add mid 1 |
| 648 | else |
| 649 | hi = mid |
| 650 | end |
| 651 | end |
| 652 | if lt lo nrows |
| 653 | if eq get tp_mkeys lo key |
| 654 | ret long lo |
| 655 | end |
| 656 | end |
| 657 | ret -1 |
| 658 | end |
| 659 | |
| 660 | fn tp_copy_row ridx long qbase ulong k ulong to int |
| 661 | for x ulong k bound 16 |
| 662 | qv = ulong 0 |
| 663 | if ge ridx 0 |
| 664 | qv = ulong get tp_mq add mul ulong ridx 16 x |
| 665 | else |
| 666 | qv = ulong get tp_mdef x |
| 667 | end |
| 668 | if eq qv 0 |
| 669 | tc_why_set TP_W_ROW |
| 670 | ret TC_E_ZERO |
| 671 | end |
| 672 | if ge qv TC_QONE |
| 673 | tc_why_set TP_W_ROW |
| 674 | ret TC_E_ZERO |
| 675 | end |
| 676 | put tc_ps_q add qbase x ushort qv |
| 677 | end |
| 678 | ret TC_OK |
| 679 | end |
| 680 | |
| 681 | fn tp_produce_one t ulong mask ulong k ulong crumb ulong to long |
| 682 | sv = ulong get tp_ev_sym t |
| 683 | if ge sv k |
| 684 | tc_why_set TP_W_SYM |
| 685 | ret -116 |
| 686 | end |
| 687 | key = tp_walk_key t mask k |
| 688 | ridx = tp_lookup key |
| 689 | put tc_ps_key t key |
| 690 | put tc_ps_crumb t uint crumb |
| 691 | qbase = mul t k |
| 692 | if ne tp_copy_row ridx qbase k TC_OK |
| 693 | ret -106 |
| 694 | end |
| 695 | put tc_sy_sym t uchar sv |
| 696 | sq = ulong get tc_ps_q add qbase sv |
| 697 | sb = tc_ubits_q16 sq |
| 698 | if lt sb 0 |
| 699 | tc_why_set TP_W_ROW |
| 700 | ret -106 |
| 701 | end |
| 702 | tp_walk_adv sv |
| 703 | ret sb |
| 704 | end |
| 705 | |
| 706 | fn tc_produce to int |
| 707 | k = get tp_mst 0 |
| 708 | mask = get tp_mst 1 |
| 709 | n = get tp_ev_n 0 |
| 710 | if lt k TC_KMIN |
| 711 | ret TC_E_ARG |
| 712 | end |
| 713 | if gt k TC_KMAX |
| 714 | ret TC_E_ARG |
| 715 | end |
| 716 | if gt n TC_MAX_N |
| 717 | ret TC_E_IO |
| 718 | end |
| 719 | if gt mul n k TC_MAX_Q |
| 720 | ret TC_E_IO |
| 721 | end |
| 722 | put tc_ps_k 0 k |
| 723 | put tc_ps_n 0 n |
| 724 | put tc_sy_k 0 k |
| 725 | put tc_sy_n 0 n |
| 726 | for i ulong 32 |
| 727 | put tc_ps_prof i get tp_prof_d i |
| 728 | put tc_ps_mod i get tp_mdig i |
| 729 | put tc_ps_dat i get tp_dat_d i |
| 730 | put tc_sy_dat i get tp_dat_d i |
| 731 | end |
| 732 | for i ulong 5 |
| 733 | put tp_walk_p i k |
| 734 | end |
| 735 | crumb = ulong 0 |
| 736 | want = long 0 |
| 737 | for t ulong n bound 500000 |
| 738 | if ne get tp_ev_first t 0 |
| 739 | if gt t 0 |
| 740 | crumb = add crumb 1 |
| 741 | end |
| 742 | end |
| 743 | sb = tp_produce_one t mask k crumb |
| 744 | if lt sb 0 |
| 745 | ret int sb |
| 746 | end |
| 747 | want = add want sb |
| 748 | end |
| 749 | rc = tc_ps_check_rows |
| 750 | if ne rc TC_OK |
| 751 | ret rc |
| 752 | end |
| 753 | got = tc_ideal_ub 0 n |
| 754 | if ne want got |
| 755 | tc_why_set TP_W_SCORE |
| 756 | ret TC_E_CORRUPT |
| 757 | end |
| 758 | ret TC_OK |
| 759 | end |