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 / core / sovereign-core / om / expert-bytes / expert-bytes.om
| 1 | note om/expert-bytes/expert-bytes.om: per-expert byte accounting for Qwen3.6-35B-A3B-FP8 from the safetensors |
| 2 | note headers alone (DRA-63, aien-dev/aien-sovereign-core-mv#855). Usage: expert-bytes MODEL_DIR WIDTH SCALE_BYTES. |
| 3 | note Reads the 8-byte header length and the JSON header of every *.safetensors file in MODEL_DIR; never reads |
| 4 | note tensor data. Matches model.language_model.layers.L.mlp.experts.E.{gate,up,down}_proj.{weight,weight_scale_inv} |
| 5 | note for L 0..39 and E 0..255 and requires, for the expected WIDTH (moe_intermediate_size) and SCALE_BYTES (2 = BF16, |
| 6 | note 4 = F32) at hidden 2048 and 128 x 128 scale blocks: |
| 7 | note weight dtype F8_E4M3, shape gate/up [WIDTH, 2048], down [2048, WIDTH], 2048 * WIDTH bytes each; |
| 8 | note scale dtype BF16 or F32, shape gate/up [WIDTH/128, 16], down [16, WIDTH/128]; |
| 9 | note every data_offsets span equal to the element count times the header dtype size; |
| 10 | note per expert, header weight bytes = 3 * 2048 * WIDTH and header scale bytes = 3 * (WIDTH/128) * 16 * SCALE_BYTES; |
| 11 | note all six tensors present exactly once for each of the 40 * 256 experts. |
| 12 | note MTP experts (mtp.*) are counted and excluded. Exit 0 PASS, 1 FAIL (any mismatch), 2 usage, 3 io, 4 header. |
| 13 | note Build: { printf 'om2\n'; cat om/vendor/json.om om/expert-bytes/expert-bytes.om; } | "$AIEN_OMC" > expert-bytes |
| 14 | |
| 15 | const ebx ulong 999999999 |
| 16 | const ebh ulong 2048 |
| 17 | const ebly ulong 40 |
| 18 | const ebne ulong 256 |
| 19 | |
| 20 | note gst slots |
| 21 | const kw ulong 0 |
| 22 | const ks ulong 1 |
| 23 | const kop ulong 3 |
| 24 | const ktn ulong 4 |
| 25 | const kdt ulong 5 |
| 26 | const ksh ulong 6 |
| 27 | const kext ulong 7 |
| 28 | const kmt ulong 8 |
| 29 | const ksz ulong 9 |
| 30 | const kdl ulong 10 |
| 31 | const kmb ulong 11 |
| 32 | const kfil ulong 12 |
| 33 | const koor ulong 13 |
| 34 | const kdup ulong 14 |
| 35 | const ktot ulong 15 |
| 36 | const knp ulong 16 |
| 37 | const kok ulong 17 |
| 38 | const krk ulong 18 |
| 39 | const kd0 ulong 19 |
| 40 | const kd1 ulong 20 |
| 41 | const kdsz ulong 21 |
| 42 | const kdok ulong 22 |
| 43 | const kb ulong 23 |
| 44 | const ke ulong 24 |
| 45 | const kfn ulong 26 |
| 46 | const kmiss ulong 27 |
| 47 | const kmin ulong 28 |
| 48 | const kmax ulong 29 |
| 49 | const kwbad ulong 30 |
| 50 | const ksbad ulong 31 |
| 51 | const kfo ulong 32 |
| 52 | const kep ulong 33 |
| 53 | |
| 54 | global ga uchar 8192 |
| 55 | global gas ulong 4 |
| 56 | global gal ulong 4 |
| 57 | global gpth uchar 4352 |
| 58 | global gdb uchar 32768 |
| 59 | global gnm uchar 16384 |
| 60 | global gno ulong 256 |
| 61 | global gnl ulong 256 |
| 62 | global gstb uchar 128 |
| 63 | global gh8 uchar 8 |
| 64 | global gwb ulong 10240 |
| 65 | global gsb ulong 10240 |
| 66 | global gmk uchar 10240 |
| 67 | global gbx uchar 10240 |
| 68 | global gst ulong 48 |
| 69 | global gob uchar 2048 |
| 70 | global geb uchar 512 |
| 71 | global gdg uchar 24 |
| 72 | |
| 73 | text tusage "usage: expert-bytes MODEL_DIR WIDTH SCALE_BYTES (WIDTH a positive multiple of 128, SCALE_BYTES 2 or 4)" |
| 74 | text tio "io" |
| 75 | text tnreg "not-regular-file" |
| 76 | text thlen "header-length" |
| 77 | text thjs "header-json" |
| 78 | text thent "header-entry" |
| 79 | text tnames "name-capacity" |
| 80 | text tref "expert-bytes refused " |
| 81 | text tsuf ".safetensors" |
| 82 | text tmeta "__metadata__" |
| 83 | text tmtp "mtp." |
| 84 | text tlay "model.language_model.layers." |
| 85 | text texps "mlp.experts." |
| 86 | text tgw "gate_proj.weight" |
| 87 | text tgs "gate_proj.weight_scale_inv" |
| 88 | text tuw "up_proj.weight" |
| 89 | text tus "up_proj.weight_scale_inv" |
| 90 | text tdw "down_proj.weight" |
| 91 | text tds "down_proj.weight_scale_inv" |
| 92 | text tdtype "dtype" |
| 93 | text tshape "shape" |
| 94 | text tdoff "data_offsets" |
| 95 | text tf8 "F8_E4M3" |
| 96 | text tbf16 "BF16" |
| 97 | text tf16 "F16" |
| 98 | text tf32 "F32" |
| 99 | text thead "expert-bytes verdict " |
| 100 | text tpass "PASS" |
| 101 | text tfail "FAIL" |
| 102 | text o1 "width" |
| 103 | text o2 "scale_bytes" |
| 104 | text o3 "layers" |
| 105 | text o4 "experts" |
| 106 | text o5 "tensors" |
| 107 | text o6 "expect_expert" |
| 108 | text o7 "expect_weight" |
| 109 | text o8 "expect_scale" |
| 110 | text o9 "seen_expert_min" |
| 111 | text o10 "seen_expert_max" |
| 112 | text o11 "seen_weight_0_0" |
| 113 | text o12 "seen_scale_0_0" |
| 114 | text o13 "experts_ok" |
| 115 | text o14 "weight_bad" |
| 116 | text o15 "scale_bad" |
| 117 | text o16 "missing" |
| 118 | text o17 "dtype_bad" |
| 119 | text o18 "shape_bad" |
| 120 | text o19 "extent_bad" |
| 121 | text o20 "duplicate" |
| 122 | text o21 "out_of_range" |
| 123 | text o22 "mtp_excluded_tensors" |
| 124 | text o23 "mtp_excluded_bytes" |
| 125 | text o24 "total_bytes" |
| 126 | text o25 "files" |
| 127 | |
| 128 | note ======== output ======== |
| 129 | |
| 130 | fn eb_dec v ulong to ulong |
| 131 | n = ulong 0 |
| 132 | x = v |
| 133 | while true bound 25 |
| 134 | put gdg n add 48 uchar rem x 10 |
| 135 | n = add n 1 |
| 136 | x = div x 10 |
| 137 | if eq x 0 |
| 138 | break |
| 139 | end |
| 140 | end |
| 141 | ret n |
| 142 | end |
| 143 | |
| 144 | fn eb_ob c uchar to void |
| 145 | p = get gst kop |
| 146 | if lt p len gob |
| 147 | put gob p c |
| 148 | put gst kop add p 1 |
| 149 | end |
| 150 | end |
| 151 | |
| 152 | fn eb_otx t view uchar * to void |
| 153 | for i ulong len t |
| 154 | eb_ob get t i |
| 155 | end |
| 156 | end |
| 157 | |
| 158 | fn eb_ou v ulong to void |
| 159 | n = eb_dec v |
| 160 | for i ulong n |
| 161 | eb_ob get gdg sub sub n i 1 |
| 162 | end |
| 163 | end |
| 164 | |
| 165 | fn eb_kv t view uchar * v ulong to void |
| 166 | eb_ob 32 |
| 167 | eb_otx t |
| 168 | eb_ob 32 |
| 169 | eb_ou v |
| 170 | end |
| 171 | |
| 172 | fn eb_oflush fd int to void |
| 173 | o = ulong 0 |
| 174 | m = get gst kop |
| 175 | while lt o m bound 4096 |
| 176 | w = write fd gob o sub m o |
| 177 | if le w 0 |
| 178 | exit 3 |
| 179 | end |
| 180 | o = add o ulong w |
| 181 | end |
| 182 | put gst kop 0 |
| 183 | end |
| 184 | |
| 185 | fn eb_die c ulong t view uchar * to void |
| 186 | put gst kop 0 |
| 187 | eb_otx tref |
| 188 | eb_ou c |
| 189 | eb_ob 32 |
| 190 | eb_otx t |
| 191 | eb_ob 10 |
| 192 | eb_oflush 2 |
| 193 | exit int c |
| 194 | end |
| 195 | |
| 196 | note ======== names ======== |
| 197 | |
| 198 | note eb_pre: nm[p ..] starts with t within n bytes; eb_rest: nm[p .. n) equals t exactly |
| 199 | fn eb_pre nm view uchar * n ulong p ulong t view uchar * to bool |
| 200 | if gt p n |
| 201 | ret false |
| 202 | end |
| 203 | if gt len t sub n p |
| 204 | ret false |
| 205 | end |
| 206 | for i ulong len t |
| 207 | if ne get nm add p i get t i |
| 208 | ret false |
| 209 | end |
| 210 | end |
| 211 | ret true |
| 212 | end |
| 213 | |
| 214 | fn eb_rest nm view uchar * n ulong p ulong t view uchar * to bool |
| 215 | if not eb_pre nm n p t |
| 216 | ret false |
| 217 | end |
| 218 | ret eq add p len t n |
| 219 | end |
| 220 | |
| 221 | note eb_has: t occurs anywhere in nm[0 .. n) |
| 222 | fn eb_has nm view uchar * n ulong t view uchar * to bool |
| 223 | if gt len t n |
| 224 | ret false |
| 225 | end |
| 226 | for i ulong add sub n len t 1 |
| 227 | if eb_pre nm n i t |
| 228 | ret true |
| 229 | end |
| 230 | end |
| 231 | ret false |
| 232 | end |
| 233 | |
| 234 | note eb_num: 1..3 decimal digits at nm[p] without a leading zero, then '.'; the value (ebx when |
| 235 | note malformed) and the position after the '.' in gst knp |
| 236 | fn eb_num nm view uchar * n ulong p ulong to ulong |
| 237 | v = ulong 0 |
| 238 | q = p |
| 239 | for i ulong 4 |
| 240 | if ge q n |
| 241 | ret ebx |
| 242 | end |
| 243 | c = get nm q |
| 244 | if eq c 46 |
| 245 | if or eq q p and gt sub q p 1 eq get nm p 48 |
| 246 | ret ebx |
| 247 | end |
| 248 | put gst knp add q 1 |
| 249 | ret v |
| 250 | end |
| 251 | if or lt c 48 gt c 57 |
| 252 | ret ebx |
| 253 | end |
| 254 | v = add mul v 10 sub ulong c 48 |
| 255 | q = add q 1 |
| 256 | end |
| 257 | ret ebx |
| 258 | end |
| 259 | |
| 260 | note eb_part: the tensor part after "mlp.experts.E.": 2 * proj (gate 0, up 1, down 2) + kind (weight 0, scale 1) |
| 261 | fn eb_part nm view uchar * n ulong p ulong to ulong |
| 262 | if eb_rest nm n p tgw |
| 263 | ret 0 |
| 264 | end |
| 265 | if eb_rest nm n p tgs |
| 266 | ret 1 |
| 267 | end |
| 268 | if eb_rest nm n p tuw |
| 269 | ret 2 |
| 270 | end |
| 271 | if eb_rest nm n p tus |
| 272 | ret 3 |
| 273 | end |
| 274 | if eb_rest nm n p tdw |
| 275 | ret 4 |
| 276 | end |
| 277 | if eb_rest nm n p tds |
| 278 | ret 5 |
| 279 | end |
| 280 | ret ebx |
| 281 | end |
| 282 | |
| 283 | note ======== expectations from WIDTH and SCALE_BYTES ======== |
| 284 | |
| 285 | note eb_edim: expected dimension i (0 or 1) of part pt; down_proj is [2048, w], gate/up are [w, 2048]; |
| 286 | note a scale divides both by the 128 x 128 block |
| 287 | fn eb_edim pt ulong i ulong to ulong |
| 288 | w = get gst kw |
| 289 | h = ebh |
| 290 | if eq rem pt 2 1 |
| 291 | w = div w 128 |
| 292 | h = div h 128 |
| 293 | end |
| 294 | dn = eq div pt 2 2 |
| 295 | if eq i 0 |
| 296 | if dn |
| 297 | ret h |
| 298 | end |
| 299 | ret w |
| 300 | end |
| 301 | if dn |
| 302 | ret w |
| 303 | end |
| 304 | ret h |
| 305 | end |
| 306 | |
| 307 | fn eb_ewt to ulong |
| 308 | ret mul 3 mul ebh get gst kw |
| 309 | end |
| 310 | |
| 311 | fn eb_esc to ulong |
| 312 | ret mul mul 3 mul div get gst kw 128 div ebh 128 get gst ks |
| 313 | end |
| 314 | |
| 315 | note ======== files ======== |
| 316 | |
| 317 | fn eb_le64 o ulong to ulong |
| 318 | v = ulong 0 |
| 319 | for i ulong 8 |
| 320 | v = or v shl ulong get gstb add o i mul 8 i |
| 321 | end |
| 322 | ret v |
| 323 | end |
| 324 | |
| 325 | fn eb_h64 to ulong |
| 326 | v = ulong 0 |
| 327 | for i ulong 8 |
| 328 | v = or v shl ulong get gh8 i mul 8 i |
| 329 | end |
| 330 | ret v |
| 331 | end |
| 332 | |
| 333 | note eb_open: open gpth[0 .. n) with O_RDONLY | O_CLOEXEC | O_NOFOLLOW (arm64 557056), require a regular |
| 334 | note file (st_mode & S_IFMT 61440 == S_IFREG 32768), leave st_size in gst ksz |
| 335 | fn eb_open n ulong to long |
| 336 | fd = fopen gpth 0 n 557056 0 |
| 337 | if lt fd 0 |
| 338 | if eq fd -40 |
| 339 | eb_die 3 tnreg |
| 340 | end |
| 341 | eb_die 3 tio |
| 342 | end |
| 343 | r = fstat int fd gstb 0 128 |
| 344 | if ne r 0 |
| 345 | eb_die 3 tio |
| 346 | end |
| 347 | md = or ulong get gstb 16 shl ulong get gstb 17 8 |
| 348 | if ne and md 61440 32768 |
| 349 | eb_die 3 tnreg |
| 350 | end |
| 351 | put gst ksz eb_le64 48 |
| 352 | ret fd |
| 353 | end |
| 354 | |
| 355 | fn eb_pfull fd long b edit uchar * n ulong pos ulong to void |
| 356 | o = ulong 0 |
| 357 | while lt o n bound 999999 |
| 358 | r = pread int fd b o sub n o long add pos o |
| 359 | if le r 0 |
| 360 | eb_die 3 tio |
| 361 | end |
| 362 | o = add o ulong r |
| 363 | end |
| 364 | end |
| 365 | |
| 366 | fn eb_close fd long to void |
| 367 | r = fclose int fd |
| 368 | if ne r 0 |
| 369 | eb_die 3 tio |
| 370 | end |
| 371 | end |
| 372 | |
| 373 | note eb_argp: copy argument a into gpth; its length |
| 374 | fn eb_argp a ulong to ulong |
| 375 | n = get gal a |
| 376 | if or eq n 0 gt n 4000 |
| 377 | eb_die 2 tusage |
| 378 | end |
| 379 | o = get gas a |
| 380 | for i ulong n |
| 381 | put gpth i get ga add o i |
| 382 | end |
| 383 | ret n |
| 384 | end |
| 385 | |
| 386 | note eb_adec: argument a as a decimal of 1..6 digits; ebx otherwise |
| 387 | fn eb_adec a ulong to ulong |
| 388 | n = get gal a |
| 389 | if or eq n 0 gt n 6 |
| 390 | ret ebx |
| 391 | end |
| 392 | o = get gas a |
| 393 | v = ulong 0 |
| 394 | for i ulong n |
| 395 | c = get ga add o i |
| 396 | if or lt c 48 gt c 57 |
| 397 | ret ebx |
| 398 | end |
| 399 | v = add mul v 10 sub ulong c 48 |
| 400 | end |
| 401 | ret v |
| 402 | end |
| 403 | |
| 404 | note eb_dname: keep the directory entry name at gdb[s ..] (NUL-terminated within cap) when it ends in .safetensors |
| 405 | fn eb_dname s ulong cap ulong to void |
| 406 | n = ulong 0 |
| 407 | while lt n cap bound 4096 |
| 408 | if eq get gdb add s n 0 |
| 409 | break |
| 410 | end |
| 411 | n = add n 1 |
| 412 | end |
| 413 | if le n len tsuf |
| 414 | ret |
| 415 | end |
| 416 | q = add s sub n len tsuf |
| 417 | for i0 ulong len tsuf |
| 418 | if ne get gdb add q i0 get tsuf i0 |
| 419 | ret |
| 420 | end |
| 421 | end |
| 422 | f = get gst kfn |
| 423 | o = get gst kfo |
| 424 | if or ge f len gno gt add o n len gnm |
| 425 | eb_die 3 tnames |
| 426 | end |
| 427 | for i ulong n |
| 428 | put gnm add o i get gdb add s i |
| 429 | end |
| 430 | put gno f o |
| 431 | put gnl f n |
| 432 | put gst kfn add f 1 |
| 433 | put gst kfo add o n |
| 434 | end |
| 435 | |
| 436 | fn eb_list to void |
| 437 | n = eb_argp 1 |
| 438 | fd = fopen gpth 0 n 540672 0 |
| 439 | if lt fd 0 |
| 440 | eb_die 3 tio |
| 441 | end |
| 442 | while true bound 999 |
| 443 | r = getdnts int fd gdb 0 len gdb |
| 444 | if lt r 0 |
| 445 | eb_die 3 tio |
| 446 | end |
| 447 | if eq r 0 |
| 448 | break |
| 449 | end |
| 450 | o = ulong 0 |
| 451 | while lt o ulong r bound 999999 |
| 452 | rl = or ulong get gdb add o 16 shl ulong get gdb add o 17 8 |
| 453 | if lt rl 20 |
| 454 | eb_die 3 tio |
| 455 | end |
| 456 | eb_dname add o 19 sub rl 19 |
| 457 | o = add o rl |
| 458 | end |
| 459 | end |
| 460 | eb_close fd |
| 461 | end |
| 462 | |
| 463 | note eb_path: gpth = MODEL_DIR '/' name j; its length |
| 464 | fn eb_path j ulong to ulong |
| 465 | d = eb_argp 1 |
| 466 | n = get gnl j |
| 467 | if gt add add d 1 n 4095 |
| 468 | eb_die 3 tnames |
| 469 | end |
| 470 | put gpth d 47 |
| 471 | o = get gno j |
| 472 | for i ulong n |
| 473 | put gpth add add d 1 i get gnm add o i |
| 474 | end |
| 475 | ret add add d 1 n |
| 476 | end |
| 477 | |
| 478 | note ======== one header entry ======== |
| 479 | |
| 480 | fn eb_dsz src view uchar * o ulong to ulong |
| 481 | if js_streq src o tf8 |
| 482 | ret 1 |
| 483 | end |
| 484 | if js_streq src o tbf16 |
| 485 | ret 2 |
| 486 | end |
| 487 | if js_streq src o tf16 |
| 488 | ret 2 |
| 489 | end |
| 490 | if js_streq src o tf32 |
| 491 | ret 4 |
| 492 | end |
| 493 | ret 0 |
| 494 | end |
| 495 | |
| 496 | fn eb_dtok src view uchar * o ulong pt ulong to bool |
| 497 | if eq rem pt 2 0 |
| 498 | ret js_streq src o tf8 |
| 499 | end |
| 500 | if eq get gst ks 2 |
| 501 | ret js_streq src o tbf16 |
| 502 | end |
| 503 | ret js_streq src o tf32 |
| 504 | end |
| 505 | |
| 506 | note eb_shp: rank and the first two dimensions of shape array x into gst krk, kd0, kd1 |
| 507 | fn eb_shp src view uchar * tape view uint * x ulong to void |
| 508 | if ne js_type tape x jsarr |
| 509 | eb_die 4 thent |
| 510 | end |
| 511 | put gst krk js_nkids tape x |
| 512 | put gst kd0 ebx |
| 513 | put gst kd1 ebx |
| 514 | e = js_first tape x |
| 515 | i = ulong 0 |
| 516 | while ne e jsnone bound 64 |
| 517 | if ne js_type tape e jsnum |
| 518 | eb_die 4 thent |
| 519 | end |
| 520 | o = js_off tape e |
| 521 | if not js_isu src o |
| 522 | eb_die 4 thent |
| 523 | end |
| 524 | if lt i 2 |
| 525 | put gst add kd0 i js_tou src o |
| 526 | end |
| 527 | i = add i 1 |
| 528 | e = js_enext tape e |
| 529 | end |
| 530 | end |
| 531 | |
| 532 | fn eb_off src view uchar * tape view uint * x ulong to void |
| 533 | if ne js_type tape x jsarr |
| 534 | eb_die 4 thent |
| 535 | end |
| 536 | if ne js_nkids tape x 2 |
| 537 | eb_die 4 thent |
| 538 | end |
| 539 | e = js_first tape x |
| 540 | f = js_enext tape e |
| 541 | if or ne js_type tape e jsnum ne js_type tape f jsnum |
| 542 | eb_die 4 thent |
| 543 | end |
| 544 | if not and js_isu src js_off tape e js_isu src js_off tape f |
| 545 | eb_die 4 thent |
| 546 | end |
| 547 | put gst kb js_tou src js_off tape e |
| 548 | put gst ke js_tou src js_off tape f |
| 549 | end |
| 550 | |
| 551 | note eb_val: entry v must be exactly {dtype, shape, data_offsets}; observations into gst; the dtype checked |
| 552 | note against part pt's expectation (pt ebx: no expectation) |
| 553 | fn eb_val src view uchar * tape view uint * v ulong pt ulong to void |
| 554 | if ne js_type tape v jsobj |
| 555 | eb_die 4 thent |
| 556 | end |
| 557 | if ne js_nkids tape v 3 |
| 558 | eb_die 4 thent |
| 559 | end |
| 560 | put gst kdok 0 |
| 561 | f = ulong 0 |
| 562 | k = js_first tape v |
| 563 | for i ulong 3 |
| 564 | o = js_off tape k |
| 565 | x = add k 1 |
| 566 | if js_streq src o tdtype |
| 567 | if ne and f 1 0 |
| 568 | eb_die 4 thent |
| 569 | end |
| 570 | f = or f 1 |
| 571 | if ne js_type tape x jsstr |
| 572 | eb_die 4 thent |
| 573 | end |
| 574 | put gst kdsz eb_dsz src js_off tape x |
| 575 | if ne pt ebx |
| 576 | if eb_dtok src js_off tape x pt |
| 577 | put gst kdok 1 |
| 578 | end |
| 579 | end |
| 580 | else |
| 581 | if js_streq src o tshape |
| 582 | if ne and f 2 0 |
| 583 | eb_die 4 thent |
| 584 | end |
| 585 | f = or f 2 |
| 586 | eb_shp src tape x |
| 587 | else |
| 588 | if not js_streq src o tdoff |
| 589 | eb_die 4 thent |
| 590 | end |
| 591 | if ne and f 4 0 |
| 592 | eb_die 4 thent |
| 593 | end |
| 594 | f = or f 4 |
| 595 | eb_off src tape x |
| 596 | end |
| 597 | end |
| 598 | k = js_mnext tape k |
| 599 | end |
| 600 | end |
| 601 | |
| 602 | fn eb_inc s ulong to void |
| 603 | put gst s add get gst s 1 |
| 604 | end |
| 605 | |
| 606 | note eb_chk: count dtype, shape and extent mismatches for part pt; true when there are none |
| 607 | fn eb_chk pt ulong to bool |
| 608 | g = true |
| 609 | if eq get gst kdok 0 |
| 610 | eb_inc kdt |
| 611 | g = false |
| 612 | end |
| 613 | d0 = get gst kd0 |
| 614 | d1 = get gst kd1 |
| 615 | rk = get gst krk |
| 616 | if or ne rk 2 or ne d0 eb_edim pt 0 ne d1 eb_edim pt 1 |
| 617 | eb_inc ksh |
| 618 | g = false |
| 619 | end |
| 620 | b = get gst kb |
| 621 | e = get gst ke |
| 622 | if or gt b e gt e get gst kdl |
| 623 | eb_inc kext |
| 624 | ret false |
| 625 | end |
| 626 | if or ne rk 2 eq get gst kdsz 0 |
| 627 | eb_inc kext |
| 628 | ret false |
| 629 | end |
| 630 | if ne sub e b mul mul d0 d1 get gst kdsz |
| 631 | eb_inc kext |
| 632 | g = false |
| 633 | end |
| 634 | ret g |
| 635 | end |
| 636 | |
| 637 | note eb_acct: a part that failed eb_chk (g false) marks its expert bad in gbx |
| 638 | fn eb_acct ly ulong ex ulong pt ulong g bool to void |
| 639 | i = add mul ly ebne ex |
| 640 | if not g |
| 641 | put gbx i 1 |
| 642 | end |
| 643 | bit = shl ulong 1 pt |
| 644 | m = ulong get gmk i |
| 645 | if ne and m bit 0 |
| 646 | eb_inc kdup |
| 647 | put gbx i 1 |
| 648 | ret |
| 649 | end |
| 650 | put gmk i uchar or m bit |
| 651 | sp = sub get gst ke get gst kb |
| 652 | if eq rem pt 2 0 |
| 653 | put gwb i add get gwb i sp |
| 654 | else |
| 655 | put gsb i add get gsb i sp |
| 656 | end |
| 657 | eb_inc ktn |
| 658 | put gst ktot add get gst ktot sp |
| 659 | end |
| 660 | |
| 661 | fn eb_mtp src view uchar * tape view uint * v ulong to void |
| 662 | eb_val src tape v ebx |
| 663 | eb_inc kmt |
| 664 | put gst kmb add get gst kmb sub get gst ke get gst kb |
| 665 | end |
| 666 | |
| 667 | fn eb_entry src view uchar 983040 tape view uint 262160 nb edit uchar 1024 k ulong to void |
| 668 | r = js_sget src js_off tape k nb |
| 669 | if lt r 0 |
| 670 | eb_die 4 thent |
| 671 | end |
| 672 | n = ulong r |
| 673 | if eb_rest nb n 0 tmeta |
| 674 | ret |
| 675 | end |
| 676 | if eb_pre nb n 0 tmtp |
| 677 | if eb_has nb n texps |
| 678 | eb_mtp src tape add k 1 |
| 679 | end |
| 680 | ret |
| 681 | end |
| 682 | if not eb_pre nb n 0 tlay |
| 683 | ret |
| 684 | end |
| 685 | ly = eb_num nb n len tlay |
| 686 | if eq ly ebx |
| 687 | ret |
| 688 | end |
| 689 | p = get gst knp |
| 690 | if not eb_pre nb n p texps |
| 691 | ret |
| 692 | end |
| 693 | ex = eb_num nb n add p len texps |
| 694 | if eq ex ebx |
| 695 | ret |
| 696 | end |
| 697 | pt = eb_part nb n get gst knp |
| 698 | if eq pt ebx |
| 699 | ret |
| 700 | end |
| 701 | if or ge ly ebly ge ex ebne |
| 702 | eb_inc koor |
| 703 | ret |
| 704 | end |
| 705 | eb_val src tape add k 1 pt |
| 706 | g = eb_chk pt |
| 707 | eb_acct ly ex pt g |
| 708 | end |
| 709 | |
| 710 | fn eb_file src edit uchar 983040 tape edit uint 262160 nb edit uchar 1024 j ulong to void |
| 711 | fd = eb_open eb_path j |
| 712 | sz = get gst ksz |
| 713 | if lt sz 8 |
| 714 | eb_die 4 thlen |
| 715 | end |
| 716 | r8 = pread int fd gh8 0 8 long 0 |
| 717 | if ne r8 8 |
| 718 | eb_die 3 tio |
| 719 | end |
| 720 | hl = eb_h64 |
| 721 | if or lt hl 2 gt hl len src |
| 722 | eb_die 4 thlen |
| 723 | end |
| 724 | if gt hl sub sz 8 |
| 725 | eb_die 4 thlen |
| 726 | end |
| 727 | eb_pfull fd src hl 8 |
| 728 | eb_close fd |
| 729 | put gst kdl sub sub sz 8 hl |
| 730 | r = js_init tape hl 64 |
| 731 | if lt r 0 |
| 732 | eb_die 4 thjs |
| 733 | end |
| 734 | r = js_parse src tape hl |
| 735 | if lt r 0 |
| 736 | eb_die 4 thjs |
| 737 | end |
| 738 | if ne js_type tape 0 jsobj |
| 739 | eb_die 4 thent |
| 740 | end |
| 741 | k = js_first tape 0 |
| 742 | while ne k jsnone bound 999999 |
| 743 | eb_entry src tape nb k |
| 744 | k = js_mnext tape k |
| 745 | end |
| 746 | eb_inc kfil |
| 747 | end |
| 748 | |
| 749 | note ======== verdict ======== |
| 750 | |
| 751 | fn eb_one i ulong to void |
| 752 | w = get gwb i |
| 753 | s = get gsb i |
| 754 | t = add w s |
| 755 | if lt t get gst kmin |
| 756 | put gst kmin t |
| 757 | end |
| 758 | if gt t get gst kmax |
| 759 | put gst kmax t |
| 760 | end |
| 761 | ok = true |
| 762 | if ne w eb_ewt |
| 763 | eb_inc kwbad |
| 764 | ok = false |
| 765 | end |
| 766 | if ne s eb_esc |
| 767 | eb_inc ksbad |
| 768 | ok = false |
| 769 | end |
| 770 | if ne get gbx i 0 |
| 771 | ok = false |
| 772 | end |
| 773 | if ok |
| 774 | eb_inc kok |
| 775 | end |
| 776 | end |
| 777 | |
| 778 | fn eb_scan to void |
| 779 | put gst kmin ebx |
| 780 | put gst kmax 0 |
| 781 | for i ulong mul ebly ebne |
| 782 | if ne get gmk i 63 |
| 783 | eb_inc kmiss |
| 784 | else |
| 785 | eb_one i |
| 786 | end |
| 787 | end |
| 788 | end |
| 789 | |
| 790 | fn eb_pass to bool |
| 791 | if ne get gst kok mul ebly ebne |
| 792 | ret false |
| 793 | end |
| 794 | if ne get gst ktn mul 6 mul ebly ebne |
| 795 | ret false |
| 796 | end |
| 797 | if ne or or get gst kdt get gst ksh get gst kext 0 |
| 798 | ret false |
| 799 | end |
| 800 | ret eq or or get gst kdup get gst koor get gst kmiss 0 |
| 801 | end |
| 802 | |
| 803 | fn eb_rep1 to void |
| 804 | eb_kv o1 get gst kw |
| 805 | eb_kv o2 get gst ks |
| 806 | eb_kv o3 ebly |
| 807 | eb_kv o4 ebne |
| 808 | eb_kv o5 get gst ktn |
| 809 | eb_kv o6 add eb_ewt eb_esc |
| 810 | eb_kv o7 eb_ewt |
| 811 | eb_kv o8 eb_esc |
| 812 | eb_kv o9 get gst kmin |
| 813 | eb_kv o10 get gst kmax |
| 814 | eb_kv o11 get gwb 0 |
| 815 | eb_kv o12 get gsb 0 |
| 816 | end |
| 817 | |
| 818 | fn eb_rep2 to void |
| 819 | eb_kv o13 get gst kok |
| 820 | eb_kv o14 get gst kwbad |
| 821 | eb_kv o15 get gst ksbad |
| 822 | eb_kv o16 get gst kmiss |
| 823 | eb_kv o17 get gst kdt |
| 824 | eb_kv o18 get gst ksh |
| 825 | eb_kv o19 get gst kext |
| 826 | eb_kv o20 get gst kdup |
| 827 | eb_kv o21 get gst koor |
| 828 | eb_kv o22 get gst kmt |
| 829 | eb_kv o23 get gst kmb |
| 830 | eb_kv o24 get gst ktot |
| 831 | eb_kv o25 get gst kfil |
| 832 | eb_ob 10 |
| 833 | end |
| 834 | |
| 835 | fn main to int |
| 836 | c = argcnt |
| 837 | if ne c 4 |
| 838 | eb_die 2 tusage |
| 839 | end |
| 840 | o = ulong 0 |
| 841 | for i ulong 4 |
| 842 | r = argread i ga o sub len ga o |
| 843 | if lt r 0 |
| 844 | eb_die 2 tusage |
| 845 | end |
| 846 | put gas i o |
| 847 | put gal i ulong r |
| 848 | o = add o ulong r |
| 849 | end |
| 850 | w = eb_adec 2 |
| 851 | s = eb_adec 3 |
| 852 | if or or eq w ebx eq w 0 ne rem w 128 0 |
| 853 | eb_die 2 tusage |
| 854 | end |
| 855 | if and ne s 2 ne s 4 |
| 856 | eb_die 2 tusage |
| 857 | end |
| 858 | put gst kw w |
| 859 | put gst ks s |
| 860 | eb_list |
| 861 | src = new uchar 983040 0 |
| 862 | tape = new uint 262160 0 |
| 863 | nb = new uchar 1024 0 |
| 864 | for j ulong get gst kfn |
| 865 | eb_file src tape nb j |
| 866 | end |
| 867 | eb_scan |
| 868 | pass = eb_pass |
| 869 | put gst kop 0 |
| 870 | eb_otx thead |
| 871 | if pass |
| 872 | eb_otx tpass |
| 873 | else |
| 874 | eb_otx tfail |
| 875 | end |
| 876 | eb_rep1 |
| 877 | eb_rep2 |
| 878 | eb_oflush 1 |
| 879 | if pass |
| 880 | ret 0 |
| 881 | end |
| 882 | ret 1 |
| 883 | end |