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 / aienux / lm1b / lm1b.in.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | note lm1b: Llama-3.2-1B-Instruct Q4_K_M forward pass in seeded Omega (aienux native/om/lm1b, arch#314). |
| 3 | note TEMPLATE. gen.sh expands the marker lines (GEN_...) into lm1b.om; edit this file, then run gen.sh. |
| 4 | note Contract v1: fd 3 = model (pread only), fd 0 = request lines, fd 1 = response lines. See README.md. |
| 5 | note Numbers mirror aienos-infer (src/math.rs, src/quant.rs, src/decode.rs, src/lib.rs) on its plain f32 |
| 6 | note path: same operations in the same order, one IEEE rounding per word, no fused multiply-add. |
| 7 | note omc refuses a function that returns f32 (error 5), so float helpers return the f32 bits as uint. |
| 8 | const NE ulong 2048 |
| 9 | const NF ulong 8192 |
| 10 | const NV ulong 128256 |
| 11 | const CTX ulong 256 |
| 12 | const KVD ulong 512 |
| 13 | const AX ulong 0 |
| 14 | const AH ulong 2048 |
| 15 | const AQ ulong 4096 |
| 16 | const AK ulong 6144 |
| 17 | const AV ulong 6656 |
| 18 | const AAT ulong 7168 |
| 19 | const AG ulong 9216 |
| 20 | const AU ulong 17408 |
| 21 | const AT ulong 25600 |
| 22 | const ASC ulong 27648 |
| 23 | const ARC ulong 27904 |
| 24 | const ARS ulong 27936 |
| 25 | const ALG ulong 32768 |
| 26 | const K3 ulong 0 |
| 27 | const K5 ulong 1 |
| 28 | const K7 ulong 2 |
| 29 | const K9 ulong 3 |
| 30 | const K6 ulong 4 |
| 31 | const K24 ulong 5 |
| 32 | const K120 ulong 6 |
| 33 | const K5040 ulong 7 |
| 34 | const K362 ulong 8 |
| 35 | const K720 ulong 9 |
| 36 | const K40320 ulong 10 |
| 37 | const KPI ulong 11 |
| 38 | const KTAU ulong 12 |
| 39 | const KPI2 ulong 13 |
| 40 | const KLN2 ulong 14 |
| 41 | const KL2E ulong 15 |
| 42 | const KSQ2 ulong 16 |
| 43 | const KHI ulong 17 |
| 44 | const KLO ulong 18 |
| 45 | const KNINF ulong 19 |
| 46 | const KEPS ulong 20 |
| 47 | const KNZ ulong 21 |
| 48 | const KSCALE ulong 22 |
| 49 | const KTS ulong 23 |
| 50 | const KM87 ulong 24 |
| 51 | const K88 ulong 25 |
| 52 | const SOB ulong 0 |
| 53 | const SCUR ulong 1 |
| 54 | const SMF ulong 2 |
| 55 | const SRB ulong 3 |
| 56 | const SDS ulong 4 |
| 57 | const SFLT ulong 5 |
| 58 | const SFILL ulong 6 |
| 59 | const SSTART ulong 7 |
| 60 | const SEOF ulong 8 |
| 61 | const SSKIP ulong 9 |
| 62 | const SLS ulong 10 |
| 63 | const SPC ulong 11 |
| 64 | const SPE ulong 12 |
| 65 | const SERR ulong 13 |
| 66 | const SIDS ulong 14 |
| 67 | const SIDN ulong 15 |
| 68 | const SNID ulong 16 |
| 69 | const SGEN ulong 17 |
| 70 | const SBUD ulong 18 |
| 71 | const SSTOP ulong 19 |
| 72 | const SFAULT ulong 20 |
| 73 | const SNTOK ulong 21 |
| 74 | const SCAN ulong 22 |
| 75 | const IBCAP ulong 65536 |
| 76 | const WLOAD ulong 0 |
| 77 | const WOK ulong 1 |
| 78 | const WBYTES ulong 2 |
| 79 | const WTENS ulong 3 |
| 80 | const WSHA ulong 4 |
| 81 | const WPASSES ulong 5 |
| 82 | const WJOB ulong 6 |
| 83 | const WSTATUS ulong 7 |
| 84 | const WCANCEL ulong 8 |
| 85 | const WREFUSED ulong 9 |
| 86 | const WREASON ulong 10 |
| 87 | const WTOKENS ulong 11 |
| 88 | const WBITS ulong 12 |
| 89 | const WACCT ulong 13 |
| 90 | const WROWB ulong 14 |
| 91 | const WSYNTAX ulong 15 |
| 92 | const WTOOLONG ulong 16 |
| 93 | const WTOOMANY ulong 17 |
| 94 | const WCONTEXT ulong 18 |
| 95 | const WBADID ulong 19 |
| 96 | const WRANGE ulong 20 |
| 97 | const WEMPTY ulong 21 |
| 98 | const WSIZE ulong 22 |
| 99 | const WMAGIC ulong 23 |
| 100 | const WHEADER ulong 24 |
| 101 | const WMANI ulong 25 |
| 102 | const WQMARK ulong 26 |
| 103 | const WGEN ulong 27 |
| 104 | const WBUDGET ulong 28 |
| 105 | const WSTOP ulong 29 |
| 106 | const WFAULT ulong 30 |
| 107 | const WIDS ulong 31 |
| 108 | const WREAD ulong 32 |
| 109 | const FSIZE ulong 807694368 |
| 110 | const NTENS ulong 147 |
| 111 | @@GEN_GLOBALS@@ |
| 112 | global rb uchar 8192 |
| 113 | global ac f32 161024 |
| 114 | global kc f32 2097152 |
| 115 | global vc f32 2097152 |
| 116 | global nw f32 67584 |
| 117 | global rf f32 32 |
| 118 | global cf f32 32 |
| 119 | global kd f32 8 |
| 120 | global ko ulong 8 |
| 121 | global tof ulong 160 |
| 122 | global tty ulong 160 |
| 123 | global toff ulong 160 |
| 124 | global ttyp ulong 160 |
| 125 | global st ulong 32 |
| 126 | global ob uchar 65536 |
| 127 | global ib uchar 65536 |
| 128 | global mf uchar 65536 |
| 129 | global one uchar 8 |
| 130 | global ids ulong 512 |
| 131 | global tk ulong 512 |
| 132 | global tb ulong 512 |
| 133 | text tw "load ok bytes tensors sha256 passes job status cancelled refused reason tokens bits acct rowbytes syntax toolong toomany context badid range empty size magic header manifest ? gen budget stop fault ids read " |
| 134 | @@GEN_MANIFEST@@ |
| 135 | note ---- vendored SHA-256 (benchmarks-mv om/data/sha256.om at 567c5da, unchanged; see README) ---- |
| 136 | @@SHA256@@ |
| 137 | note ---- output: one line at a time in ob, flushed on newline ---- |
| 138 | fn oc c uchar to void |
| 139 | n = get st SOB |
| 140 | put ob n c |
| 141 | put st SOB add n 1 |
| 142 | end |
| 143 | fn ow k ulong to void |
| 144 | for i ulong 16 |
| 145 | c = get tw add mul k 16 i |
| 146 | if eq c 32 |
| 147 | ret |
| 148 | end |
| 149 | oc c |
| 150 | end |
| 151 | end |
| 152 | fn osp to void |
| 153 | oc 32 |
| 154 | end |
| 155 | fn ou v ulong to void |
| 156 | d = new uchar 24 0 |
| 157 | n = ulong 0 |
| 158 | x = v |
| 159 | while true bound 30 |
| 160 | put d n uchar add 48 rem x 10 |
| 161 | n = add n 1 |
| 162 | x = div x 10 |
| 163 | if eq x 0 |
| 164 | break |
| 165 | end |
| 166 | end |
| 167 | for i ulong n |
| 168 | oc get d sub sub n 1 i |
| 169 | end |
| 170 | end |
| 171 | fn onl to void |
| 172 | oc 10 |
| 173 | n = get st SOB |
| 174 | r = write int 1 ob 0 n |
| 175 | if ne r long n |
| 176 | exit 4 |
| 177 | end |
| 178 | put st SOB 0 |
| 179 | end |
| 180 | fn refuse w ulong to void |
| 181 | put st SOB 0 |
| 182 | ow WLOAD |
| 183 | osp |
| 184 | ow WREFUSED |
| 185 | osp |
| 186 | ow WREASON |
| 187 | osp |
| 188 | ow w |
| 189 | onl |
| 190 | exit 3 |
| 191 | end |
| 192 | note ---- manifest text rebuilt from the header, compared byte for byte with the pinned text ---- |
| 193 | fn mc c uchar to void |
| 194 | n = get st SMF |
| 195 | if ge n 65536 |
| 196 | refuse WMANI |
| 197 | end |
| 198 | put mf n c |
| 199 | put st SMF add n 1 |
| 200 | end |
| 201 | fn mu v ulong to void |
| 202 | d = new uchar 24 0 |
| 203 | n = ulong 0 |
| 204 | x = v |
| 205 | while true bound 30 |
| 206 | put d n uchar add 48 rem x 10 |
| 207 | n = add n 1 |
| 208 | x = div x 10 |
| 209 | if eq x 0 |
| 210 | break |
| 211 | end |
| 212 | end |
| 213 | for i ulong n |
| 214 | mc get d sub sub n 1 i |
| 215 | end |
| 216 | end |
| 217 | fn msg v ulong nb ulong to void |
| 218 | note signed decimal of the low nb bits of v (nb 8, 16, 32 or 64) |
| 219 | x = v |
| 220 | neg1 = false |
| 221 | if eq nb 64 |
| 222 | if ne shr x 63 0 |
| 223 | neg1 = true |
| 224 | end |
| 225 | else |
| 226 | if ne and shr x sub nb 1 1 0 |
| 227 | neg1 = true |
| 228 | x = or x not sub shl ulong 1 nb 1 |
| 229 | end |
| 230 | end |
| 231 | if neg1 |
| 232 | mc 45 |
| 233 | mu neg x |
| 234 | else |
| 235 | mu x |
| 236 | end |
| 237 | end |
| 238 | fn hrd to ulong |
| 239 | c = get st SCUR |
| 240 | if ge c 16777216 |
| 241 | refuse WHEADER |
| 242 | end |
| 243 | put st SCUR add c 1 |
| 244 | ret ulong get m0 c |
| 245 | end |
| 246 | fn hrn n ulong to ulong |
| 247 | v = ulong 0 |
| 248 | for i ulong n |
| 249 | v = or v shl hrd mul i 8 |
| 250 | end |
| 251 | ret v |
| 252 | end |
| 253 | fn mstr to void |
| 254 | note a GGUF string: its length, then the bytes when short and plain (rule shared with the Rust dumper) |
| 255 | n = hrn 8 |
| 256 | c = get st SCUR |
| 257 | if gt n 16777216 |
| 258 | refuse WHEADER |
| 259 | end |
| 260 | if gt add c n 16777216 |
| 261 | refuse WHEADER |
| 262 | end |
| 263 | ok = le n 40 |
| 264 | for i ulong n |
| 265 | b = get m0 add c i |
| 266 | if or or lt b 32 gt b 126 or eq b 34 eq b 92 |
| 267 | ok = false |
| 268 | end |
| 269 | end |
| 270 | mu n |
| 271 | if ok |
| 272 | mc 32 |
| 273 | for i ulong n |
| 274 | mc get m0 add c i |
| 275 | end |
| 276 | end |
| 277 | put st SCUR add c n |
| 278 | end |
| 279 | fn tsize e ulong to ulong |
| 280 | if or or eq e 0 eq e 1 eq e 7 |
| 281 | ret 1 |
| 282 | end |
| 283 | if or eq e 2 eq e 3 |
| 284 | ret 2 |
| 285 | end |
| 286 | if or or eq e 4 eq e 5 eq e 6 |
| 287 | ret 4 |
| 288 | end |
| 289 | if or or eq e 10 eq e 11 eq e 12 |
| 290 | ret 8 |
| 291 | end |
| 292 | ret 0 |
| 293 | end |
| 294 | fn mval t ulong to void |
| 295 | note one metadata value of type t, as the manifest prints it |
| 296 | if or or eq t 0 eq t 2 eq t 4 |
| 297 | mu hrn tsize t |
| 298 | ret |
| 299 | end |
| 300 | if eq t 10 |
| 301 | mu hrn 8 |
| 302 | ret |
| 303 | end |
| 304 | if or eq t 6 eq t 12 |
| 305 | mu hrn tsize t |
| 306 | ret |
| 307 | end |
| 308 | if eq t 7 |
| 309 | mu hrn 1 |
| 310 | ret |
| 311 | end |
| 312 | if or or eq t 1 eq t 3 eq t 5 |
| 313 | sz = tsize t |
| 314 | msg hrn sz mul sz 8 |
| 315 | ret |
| 316 | end |
| 317 | if eq t 11 |
| 318 | msg hrn 8 64 |
| 319 | ret |
| 320 | end |
| 321 | if eq t 8 |
| 322 | mstr |
| 323 | ret |
| 324 | end |
| 325 | if eq t 9 |
| 326 | e = hrn 4 |
| 327 | n = hrn 8 |
| 328 | mu e |
| 329 | mc 32 |
| 330 | mu n |
| 331 | sz = tsize e |
| 332 | if ne sz 0 |
| 333 | c = get st SCUR |
| 334 | if gt n 16777216 |
| 335 | refuse WHEADER |
| 336 | end |
| 337 | put st SCUR add c mul n sz |
| 338 | ret |
| 339 | end |
| 340 | if ne e 8 |
| 341 | refuse WHEADER |
| 342 | end |
| 343 | if gt n 999999 |
| 344 | refuse WHEADER |
| 345 | end |
| 346 | for i ulong n |
| 347 | k = hrn 8 |
| 348 | if gt k 16777216 |
| 349 | refuse WHEADER |
| 350 | end |
| 351 | put st SCUR add get st SCUR k |
| 352 | end |
| 353 | ret |
| 354 | end |
| 355 | refuse WHEADER |
| 356 | end |
| 357 | fn header to void |
| 358 | put st SCUR 0 |
| 359 | put st SMF 0 |
| 360 | if ne hrn 4 1179993927 |
| 361 | refuse WMAGIC |
| 362 | end |
| 363 | v = hrn 4 |
| 364 | nt = hrn 8 |
| 365 | nk = hrn 8 |
| 366 | if or gt nt 160 gt nk 1024 |
| 367 | refuse WHEADER |
| 368 | end |
| 369 | mc 103 |
| 370 | mc 103 |
| 371 | mc 117 |
| 372 | mc 102 |
| 373 | mc 32 |
| 374 | mu v |
| 375 | mc 32 |
| 376 | mu nt |
| 377 | mc 32 |
| 378 | mu nk |
| 379 | mc 10 |
| 380 | hkvs nk |
| 381 | htens nt |
| 382 | hdone nt |
| 383 | end |
| 384 | fn hkvs nk ulong to void |
| 385 | for i ulong nk |
| 386 | mc 107 |
| 387 | mc 118 |
| 388 | mc 32 |
| 389 | mstr |
| 390 | t = hrn 4 |
| 391 | mc 32 |
| 392 | mu t |
| 393 | mc 32 |
| 394 | mval t |
| 395 | mc 10 |
| 396 | end |
| 397 | end |
| 398 | fn htens nt ulong to void |
| 399 | for i ulong nt |
| 400 | mc 116 |
| 401 | mc 32 |
| 402 | n = hrn 8 |
| 403 | c = get st SCUR |
| 404 | if gt n 255 |
| 405 | refuse WHEADER |
| 406 | end |
| 407 | for j ulong n |
| 408 | mc get m0 add c j |
| 409 | end |
| 410 | put st SCUR add c n |
| 411 | nd = hrn 4 |
| 412 | if or lt nd 1 gt nd 2 |
| 413 | refuse WMANI |
| 414 | end |
| 415 | d0 = hrn 8 |
| 416 | d1 = ulong 1 |
| 417 | if eq nd 2 |
| 418 | d1 = hrn 8 |
| 419 | end |
| 420 | ty = hrn 4 |
| 421 | of = hrn 8 |
| 422 | mc 32 |
| 423 | mu ty |
| 424 | mc 32 |
| 425 | mu nd |
| 426 | mc 32 |
| 427 | mu d0 |
| 428 | mc 32 |
| 429 | mu d1 |
| 430 | mc 32 |
| 431 | mu of |
| 432 | mc 10 |
| 433 | put toff i of |
| 434 | put ttyp i ty |
| 435 | end |
| 436 | end |
| 437 | fn hdone nt ulong to void |
| 438 | ds = and add get st SCUR 31 not ulong 31 |
| 439 | mc 100 |
| 440 | mc 97 |
| 441 | mc 116 |
| 442 | mc 97 |
| 443 | mc 32 |
| 444 | mu ds |
| 445 | mc 10 |
| 446 | put st SDS ds |
| 447 | n = get st SMF |
| 448 | if ne n len man |
| 449 | refuse WMANI |
| 450 | end |
| 451 | for i ulong n |
| 452 | if ne get mf i get man i |
| 453 | refuse WMANI |
| 454 | end |
| 455 | end |
| 456 | for i ulong nt |
| 457 | s = ulong 0 |
| 458 | for j ulong 3 |
| 459 | s = add mul s 10 ulong sub get tslot add mul i 3 j 48 |
| 460 | end |
| 461 | put tof s add ds get toff i |
| 462 | put tty s get ttyp i |
| 463 | end |
| 464 | end |
| 465 | note ---- f16 and the K-quant dot products (src/half.rs, src/quant.rs) ---- |
| 466 | fn f16 lo uchar hi uchar to uint |
| 467 | h = or uint lo shl uint hi 8 |
| 468 | sg = shl shr h 15 31 |
| 469 | ex = and shr h 10 31 |
| 470 | mn = and h 1023 |
| 471 | if eq ex 0 |
| 472 | if eq mn 0 |
| 473 | ret sg |
| 474 | end |
| 475 | m = mn |
| 476 | sh = uint 0 |
| 477 | while lt m 1024 bound 16 |
| 478 | m = shl m 1 |
| 479 | sh = add sh 1 |
| 480 | end |
| 481 | ret or or sg shl sub 113 sh 23 shl and m 1023 13 |
| 482 | end |
| 483 | if eq ex 31 |
| 484 | ret or or sg 2139095040 shl mn 13 |
| 485 | end |
| 486 | ret or or sg shl add ex 112 23 shl mn 13 |
| 487 | end |
| 488 | fn scm o ulong j ulong to uint |
| 489 | note scale_min_k4: scale in the low byte, min in the next byte |
| 490 | if lt j 4 |
| 491 | a = get rb add o j |
| 492 | b = get rb add o add j 4 |
| 493 | ret or uint and a 63 shl uint and b 63 8 |
| 494 | end |
| 495 | a = get rb add o add j 4 |
| 496 | b = get rb add o sub j 4 |
| 497 | c = get rb add o j |
| 498 | sc = or and a 15 shl shr b 6 4 |
| 499 | m = or shr a 4 shl shr c 6 4 |
| 500 | ret or uint sc shl uint m 8 |
| 501 | end |
| 502 | fn dq4h s f32 qo ulong xj ulong sh ulong to uint |
| 503 | note 32 weights of one Q4_K sub-block (low nibbles when sh is 0, high when 4), summed in order |
| 504 | d1 = get kd 2 |
| 505 | n1 = get kd 3 |
| 506 | a = s |
| 507 | for l ulong 32 |
| 508 | q = shr get rb add qo l sh |
| 509 | w = sub mul d1 f32 and q 15 n1 |
| 510 | a = add a mul w get ac add xj l |
| 511 | end |
| 512 | ret bitcast uint a |
| 513 | end |
| 514 | fn dq4j o ulong xb ulong j ulong s f32 to uint |
| 515 | note sub-blocks 2 j and 2 j + 1; kd holds d, dmin, then this sub-block scale and min |
| 516 | p = scm add o 4 mul j 2 |
| 517 | put kd 2 mul get kd 0 f32 and p 255 |
| 518 | put kd 3 mul get kd 1 f32 shr p 8 |
| 519 | t = bitcast f32 dq4h s add o add 16 mul j 32 add xb mul j 64 0 |
| 520 | p = scm add o 4 add mul j 2 1 |
| 521 | put kd 2 mul get kd 0 f32 and p 255 |
| 522 | put kd 3 mul get kd 1 f32 shr p 8 |
| 523 | ret dq4h t add o add 16 mul j 32 add add xb mul j 64 32 4 |
| 524 | end |
| 525 | fn dq4b o ulong xb ulong to uint |
| 526 | note one Q4_K block (144 bytes at rb o) against 256 activations at ac xb: dequantize, then sum in order |
| 527 | put kd 0 bitcast f32 f16 get rb o get rb add o 1 |
| 528 | put kd 1 bitcast f32 f16 get rb add o 2 get rb add o 3 |
| 529 | s = get cf KNZ |
| 530 | for j ulong 4 |
| 531 | s = bitcast f32 dq4j o xb j s |
| 532 | end |
| 533 | ret bitcast uint s |
| 534 | end |
| 535 | fn dq4 nb ulong xo ulong to uint |
| 536 | acc = f32 0.0 |
| 537 | for b ulong nb |
| 538 | acc = add acc bitcast f32 dq4b mul b 144 add xo mul b 256 |
| 539 | end |
| 540 | ret bitcast uint acc |
| 541 | end |
| 542 | fn i8f u uchar to uint |
| 543 | v = int u |
| 544 | if ge v 128 |
| 545 | v = sub v 256 |
| 546 | end |
| 547 | ret bitcast uint f32 v |
| 548 | end |
| 549 | fn q6v k ulong p ulong to int |
| 550 | note one 6-bit Q6_K weight minus 32 at k past ko 0 (low bits) and ko 1 (high bits); pass p picks |
| 551 | note the nibble (p / 2) and the two high bits (2 p) |
| 552 | lo = and shr get rb add get ko 0 k mul shr p 1 4 15 |
| 553 | hi = and shr get rb add get ko 1 k mul p 2 3 |
| 554 | ret sub int or lo shl hi 4 32 |
| 555 | end |
| 556 | fn dq6g s f32 p ulong to uint |
| 557 | note 16 weights sharing one scale (kd 4); activations at ac ko 2 |
| 558 | a = s |
| 559 | xp = get ko 2 |
| 560 | ds = get kd 4 |
| 561 | for k ulong 16 |
| 562 | q = q6v k p |
| 563 | a = add a mul mul ds f32 q get ac add xp k |
| 564 | end |
| 565 | ret bitcast uint a |
| 566 | end |
| 567 | fn dq6p s f32 to uint |
| 568 | note pass of a Q6_K block: 32 weights, two scales; kd 0 holds d; ko 3..6 hold the pass bases |
| 569 | a = s |
| 570 | for g ulong 2 |
| 571 | put ko 0 add get ko 3 mul g 16 |
| 572 | put ko 1 add get ko 4 mul g 16 |
| 573 | put ko 2 add get ko 5 mul g 16 |
| 574 | c = get rb add get ko 6 g |
| 575 | put kd 4 mul get kd 0 bitcast f32 i8f c |
| 576 | a = bitcast f32 dq6g a get ko 7 |
| 577 | end |
| 578 | ret bitcast uint a |
| 579 | end |
| 580 | fn q6set o ulong xb ulong hf ulong p ulong to void |
| 581 | note bases of pass p of half hf: ko 3 low bits, ko 4 high bits, ko 5 activations, ko 6 scales, ko 7 p |
| 582 | put ko 3 add o mul hf 64 |
| 583 | put ko 3 add get ko 3 mul and p 1 32 |
| 584 | put ko 4 add add o 128 mul hf 32 |
| 585 | put ko 5 add xb mul hf 128 |
| 586 | put ko 5 add get ko 5 mul p 32 |
| 587 | put ko 6 add add o 192 mul hf 8 |
| 588 | put ko 6 add get ko 6 mul p 2 |
| 589 | put ko 7 p |
| 590 | end |
| 591 | fn dq6b o ulong xb ulong to uint |
| 592 | note one Q6_K block (210 bytes at rb o) against 256 activations at ac xb, in index order |
| 593 | put kd 0 bitcast f32 f16 get rb add o 208 get rb add o 209 |
| 594 | s = get cf KNZ |
| 595 | for hf ulong 2 |
| 596 | for p ulong 4 |
| 597 | q6set o xb hf p |
| 598 | s = bitcast f32 dq6p s |
| 599 | end |
| 600 | end |
| 601 | ret bitcast uint s |
| 602 | end |
| 603 | fn dq6 nb ulong xo ulong to uint |
| 604 | acc = f32 0.0 |
| 605 | for b ulong nb |
| 606 | acc = add acc bitcast f32 dq6b mul b 210 add xo mul b 256 |
| 607 | end |
| 608 | ret bitcast uint acc |
| 609 | end |
| 610 | fn dv6g p ulong to void |
| 611 | yp = get ko 2 |
| 612 | ds = get kd 4 |
| 613 | for k ulong 16 |
| 614 | q = q6v k p |
| 615 | put ac add yp k mul ds f32 q |
| 616 | end |
| 617 | end |
| 618 | fn dv6 nb ulong yo ulong to void |
| 619 | note dequantize the Q6_K row in rb into ac at yo (the embedding lookup) |
| 620 | for b ulong nb |
| 621 | o = mul b 210 |
| 622 | put kd 0 bitcast f32 f16 get rb add o 208 get rb add o 209 |
| 623 | for hf ulong 2 |
| 624 | for p ulong 4 |
| 625 | q6set o add yo mul b 256 hf p |
| 626 | for g ulong 2 |
| 627 | put ko 0 add get ko 3 mul g 16 |
| 628 | put ko 1 add get ko 4 mul g 16 |
| 629 | put ko 2 add get ko 5 mul g 16 |
| 630 | c = get rb add get ko 6 g |
| 631 | put kd 4 mul get kd 0 bitcast f32 i8f c |
| 632 | dv6g p |
| 633 | end |
| 634 | end |
| 635 | end |
| 636 | end |
| 637 | end |
| 638 | @@GEN_CPSEG@@ |
| 639 | @@GEN_FB@@ |
| 640 | fn rowcp a ulong n ulong to void |
| 641 | p = shr a 24 |
| 642 | o = and a 16777215 |
| 643 | k = n |
| 644 | if gt add o n 16777216 |
| 645 | k = sub 16777216 o |
| 646 | end |
| 647 | cpseg p o 0 k |
| 648 | if lt k n |
| 649 | cpseg add p 1 0 k sub n k |
| 650 | end |
| 651 | end |
| 652 | fn mv ti ulong xo ulong yo ulong rows ulong cols ulong to void |
| 653 | ty = get tty ti |
| 654 | nb = div cols 256 |
| 655 | rbs = mul nb 144 |
| 656 | if eq ty 14 |
| 657 | rbs = mul nb 210 |
| 658 | else |
| 659 | if ne ty 12 |
| 660 | trap 10 |
| 661 | end |
| 662 | end |
| 663 | base = get tof ti |
| 664 | for r ulong rows |
| 665 | rowcp add base mul r rbs rbs |
| 666 | v = f32 0.0 |
| 667 | if eq ty 12 |
| 668 | v = bitcast f32 dq4 nb xo |
| 669 | else |
| 670 | v = bitcast f32 dq6 nb xo |
| 671 | end |
| 672 | put ac add yo r v |
| 673 | end |
| 674 | put st SRB add get st SRB mul rows rbs |
| 675 | end |
| 676 | note ---- scalar math (src/math.rs) ---- |
| 677 | fn msqrt x f32 to uint |
| 678 | if le x 0.0 |
| 679 | ret uint 0 |
| 680 | end |
| 681 | y = bitcast f32 add shr bitcast uint x 1 532676608 |
| 682 | for i ulong 5 |
| 683 | y = mul 0.5 add y div x y |
| 684 | end |
| 685 | ret bitcast uint y |
| 686 | end |
| 687 | fn mexp a f32 to uint |
| 688 | x = a |
| 689 | if lt x get cf KM87 |
| 690 | x = get cf KM87 |
| 691 | end |
| 692 | if gt x get cf K88 |
| 693 | x = get cf K88 |
| 694 | end |
| 695 | sc = mul x get cf KL2E |
| 696 | n = int 0 |
| 697 | if ge sc 0.0 |
| 698 | n = int add sc 0.5 |
| 699 | else |
| 700 | n = int sub sc 0.5 |
| 701 | end |
| 702 | nf = f32 n |
| 703 | r = sub sub x mul nf get cf KHI mul nf get cf KLO |
| 704 | t = add get cf K120 div r 720.0 |
| 705 | t = add get cf K24 mul r t |
| 706 | t = add get cf K6 mul r t |
| 707 | t = add 0.5 mul r t |
| 708 | t = add 1.0 mul r t |
| 709 | p = add 1.0 mul r t |
| 710 | ret bitcast uint mul p bitcast f32 shl uint add n 127 23 |
| 711 | end |
| 712 | fn mln a f32 to uint |
| 713 | b = bitcast uint a |
| 714 | e = sub int and shr b 23 255 127 |
| 715 | m = bitcast f32 or and b 8388607 1065353216 |
| 716 | if gt m get cf KSQ2 |
| 717 | m = mul m 0.5 |
| 718 | e = add e 1 |
| 719 | end |
| 720 | y = div sub m 1.0 add m 1.0 |
| 721 | y2 = mul y y |
| 722 | t = add get cf K9 div y2 11.0 |
| 723 | t = add get cf K7 mul y2 t |
| 724 | t = add get cf K5 mul y2 t |
| 725 | t = add get cf K3 mul y2 t |
| 726 | t = add 1.0 mul y2 t |
| 727 | s = mul y t |
| 728 | ret bitcast uint add mul 2.0 s mul f32 e get cf KLN2 |
| 729 | end |
| 730 | fn fmodp x f32 y f32 to uint |
| 731 | note exact fmod for finite x >= 0 and normal y > 0 (integer long division, as musl fmodf) |
| 732 | bx = bitcast uint x |
| 733 | by = bitcast uint y |
| 734 | ex = and shr bx 23 255 |
| 735 | ey = and shr by 23 255 |
| 736 | if or or ge ex 255 ne shr bx 31 0 or lt ey 24 ge ey 255 |
| 737 | trap 11 |
| 738 | end |
| 739 | if lt x y |
| 740 | ret bx |
| 741 | end |
| 742 | mx = ulong or and bx 8388607 8388608 |
| 743 | my = ulong or and by 8388607 8388608 |
| 744 | r = mx |
| 745 | k = sub ex ey |
| 746 | for i uint k |
| 747 | if ge r my |
| 748 | r = sub r my |
| 749 | end |
| 750 | r = shl r 1 |
| 751 | end |
| 752 | if ge r my |
| 753 | r = sub r my |
| 754 | end |
| 755 | if eq r 0 |
| 756 | ret uint 0 |
| 757 | end |
| 758 | ret bitcast uint mul f32 r bitcast f32 shl sub ey 23 23 |
| 759 | end |
| 760 | fn sincos a f32 i ulong to void |
| 761 | r = bitcast f32 fmodp add a get cf KPI get cf KTAU |
| 762 | if lt r 0.0 |
| 763 | r = add r get cf KTAU |
| 764 | end |
| 765 | x = sub r get cf KPI |
| 766 | cs = f32 1.0 |
| 767 | if gt x get cf KPI2 |
| 768 | x = sub get cf KPI x |
| 769 | cs = f32 -1.0 |
| 770 | else |
| 771 | if lt x neg get cf KPI2 |
| 772 | x = sub neg get cf KPI x |
| 773 | cs = f32 -1.0 |
| 774 | end |
| 775 | end |
| 776 | x2 = mul x x |
| 777 | t = mul x2 get cf K362 |
| 778 | t = sub t get cf K5040 |
| 779 | t = mul x2 t |
| 780 | t = add get cf K120 t |
| 781 | t = mul x2 t |
| 782 | t = sub t get cf K6 |
| 783 | t = mul x2 t |
| 784 | t = add 1.0 t |
| 785 | put ac add ARS i mul x t |
| 786 | u = sub get cf K40320 div x2 3628800.0 |
| 787 | u = mul x2 u |
| 788 | u = sub u get cf K720 |
| 789 | u = mul x2 u |
| 790 | u = add get cf K24 u |
| 791 | u = mul x2 u |
| 792 | u = sub u 0.5 |
| 793 | u = mul x2 u |
| 794 | u = add 1.0 u |
| 795 | put ac add ARC i mul cs u |
| 796 | end |
| 797 | fn initc to void |
| 798 | put cf K3 div f32 1.0 3.0 |
| 799 | put cf K5 div f32 1.0 5.0 |
| 800 | put cf K7 div f32 1.0 7.0 |
| 801 | put cf K9 div f32 1.0 9.0 |
| 802 | put cf K6 div f32 1.0 6.0 |
| 803 | put cf K24 div f32 1.0 24.0 |
| 804 | put cf K120 div f32 1.0 120.0 |
| 805 | put cf K5040 div f32 1.0 5040.0 |
| 806 | put cf K362 div f32 1.0 362880.0 |
| 807 | put cf K720 div f32 1.0 720.0 |
| 808 | put cf K40320 div f32 1.0 40320.0 |
| 809 | put cf KPI bitcast f32 uint 1078530011 |
| 810 | put cf KTAU bitcast f32 uint 1086918619 |
| 811 | put cf KPI2 bitcast f32 uint 1070141403 |
| 812 | put cf KLN2 bitcast f32 uint 1060205080 |
| 813 | put cf KL2E bitcast f32 uint 1069066811 |
| 814 | put cf KSQ2 bitcast f32 uint 1068827891 |
| 815 | put cf KHI bitcast f32 uint 1060208640 |
| 816 | put cf KLO bitcast f32 uint 3109978243 |
| 817 | put cf KNINF bitcast f32 uint 4286578688 |
| 818 | put cf KEPS bitcast f32 uint 925353388 |
| 819 | put cf KNZ bitcast f32 uint 2147483648 |
| 820 | put cf KM87 f32 -87.0 |
| 821 | put cf K88 f32 88.0 |
| 822 | put cf KSCALE div f32 1.0 bitcast f32 msqrt f32 64.0 |
| 823 | base = bitcast f32 uint 1223959552 |
| 824 | put cf KTS bitcast f32 mexp mul f32 -0.03125 bitcast f32 mln base |
| 825 | end |
| 826 | note ---- the forward pass (src/decode.rs) ---- |
| 827 | fn rmsn xo ulong wo ulong yo ulong to void |
| 828 | ss = get cf KNZ |
| 829 | for i ulong NE |
| 830 | v = get ac add xo i |
| 831 | ss = add ss mul v v |
| 832 | end |
| 833 | ss = div ss 2048.0 |
| 834 | sc = div 1.0 bitcast f32 msqrt add ss get cf KEPS |
| 835 | for i ulong NE |
| 836 | v = mul get ac add xo i sc |
| 837 | put ac add yo i mul v get nw add wo i |
| 838 | end |
| 839 | end |
| 840 | fn ropec pos ulong to void |
| 841 | ts = get cf KTS |
| 842 | th = f32 pos |
| 843 | for i ulong 32 |
| 844 | sincos div th get rf i i |
| 845 | th = mul th ts |
| 846 | end |
| 847 | end |
| 848 | fn ropea o ulong nh ulong to void |
| 849 | for h ulong nh |
| 850 | for i ulong 32 |
| 851 | j = add add o mul h 64 mul i 2 |
| 852 | a = get ac j |
| 853 | b = get ac add j 1 |
| 854 | c = get ac add ARC i |
| 855 | s = get ac add ARS i |
| 856 | put ac j sub mul a c mul b s |
| 857 | put ac add j 1 add mul a s mul b c |
| 858 | end |
| 859 | end |
| 860 | end |
| 861 | fn smax n ulong to void |
| 862 | mx = get cf KNINF |
| 863 | for t ulong n |
| 864 | mx = fmax mx get ac add ASC t |
| 865 | end |
| 866 | sm = f32 0.0 |
| 867 | for t ulong n |
| 868 | v = bitcast f32 mexp sub get ac add ASC t mx |
| 869 | put ac add ASC t v |
| 870 | sm = add sm v |
| 871 | end |
| 872 | for t ulong n |
| 873 | put ac add ASC t div get ac add ASC t sm |
| 874 | end |
| 875 | end |
| 876 | fn ascore qo ulong kv ulong t ulong to uint |
| 877 | note q . k over one head (64 values), summed in order from -0.0 |
| 878 | kq = add kv mul t KVD |
| 879 | s = get cf KNZ |
| 880 | for i ulong 64 |
| 881 | v = mul get ac add qo i get kc add kq i |
| 882 | s = add s v |
| 883 | end |
| 884 | ret bitcast uint s |
| 885 | end |
| 886 | fn aacc ao ulong kv ulong t ulong to void |
| 887 | vo = add kv mul t KVD |
| 888 | w = get ac add ASC t |
| 889 | for i ulong 64 |
| 890 | v = mul w get vc add vo i |
| 891 | put ac add ao i add get ac add ao i v |
| 892 | end |
| 893 | end |
| 894 | fn ahead base ulong np ulong h ulong to void |
| 895 | kv = add base mul div h 4 64 |
| 896 | qo = add AQ mul h 64 |
| 897 | for t ulong np |
| 898 | s = bitcast f32 ascore qo kv t |
| 899 | put ac add ASC t mul s get cf KSCALE |
| 900 | end |
| 901 | smax np |
| 902 | ao = add AAT mul h 64 |
| 903 | for i ulong 64 |
| 904 | put ac add ao i 0.0 |
| 905 | end |
| 906 | for t ulong np |
| 907 | aacc ao kv t |
| 908 | end |
| 909 | end |
| 910 | fn attn l ulong pos ulong to void |
| 911 | base = mul l mul CTX KVD |
| 912 | for h ulong 32 |
| 913 | ahead base add pos 1 h |
| 914 | end |
| 915 | end |
| 916 | fn resid to void |
| 917 | for i ulong NE |
| 918 | put ac add AX i add get ac add AX i get ac add AT i |
| 919 | end |
| 920 | end |
| 921 | fn kvst l ulong pos ulong to void |
| 922 | at = add mul l mul CTX KVD mul pos KVD |
| 923 | for i ulong KVD |
| 924 | put kc add at i get ac add AK i |
| 925 | put vc add at i get ac add AV i |
| 926 | end |
| 927 | end |
| 928 | fn silu to void |
| 929 | for i ulong NF |
| 930 | g = get ac add AG i |
| 931 | e = add 1.0 bitcast f32 mexp neg g |
| 932 | put ac add AG i mul div g e get ac add AU i |
| 933 | end |
| 934 | end |
| 935 | fn fwd tok ulong pos ulong want bool to void |
| 936 | rowcp add get tof 144 mul tok 1680 1680 |
| 937 | put st SRB add get st SRB 1680 |
| 938 | dv6 8 AX |
| 939 | ropec pos |
| 940 | for l ulong 16 |
| 941 | rmsn AX mul l NE AH |
| 942 | li = mul l 9 |
| 943 | mv li AH AQ 2048 2048 |
| 944 | mv add li 1 AH AK 512 2048 |
| 945 | mv add li 2 AH AV 512 2048 |
| 946 | ropea AQ 32 |
| 947 | ropea AK 8 |
| 948 | kvst l pos |
| 949 | attn l pos |
| 950 | mv add li 3 AAT AT 2048 2048 |
| 951 | resid |
| 952 | if and eq l 8 eq pos get st SFLT |
| 953 | trap 12 |
| 954 | end |
| 955 | rmsn AX mul add 16 l NE AH |
| 956 | mv add li 6 AH AG 8192 2048 |
| 957 | mv add li 7 AH AU 8192 2048 |
| 958 | silu |
| 959 | mv add li 8 AG AT 2048 8192 |
| 960 | resid |
| 961 | end |
| 962 | if want |
| 963 | rmsn AX mul 32 NE AH |
| 964 | mv 144 AH ALG NV 2048 |
| 965 | end |
| 966 | end |
| 967 | fn amax to ulong |
| 968 | best = ulong 0 |
| 969 | bv = get ac ALG |
| 970 | for i ulong NV |
| 971 | v = get ac add ALG i |
| 972 | if gt v bv |
| 973 | best = i |
| 974 | bv = v |
| 975 | end |
| 976 | end |
| 977 | ret best |
| 978 | end |
| 979 | note ---- load: read, digest, admit ---- |
| 980 | fn loadf32 a ulong n ulong dst ulong to void |
| 981 | note n little-endian f32 values at file address a into nw at dst |
| 982 | for i ulong n |
| 983 | o = add a mul i 4 |
| 984 | v = or or uint fb o shl uint fb add o 1 8 or shl uint fb add o 2 16 shl uint fb add o 3 24 |
| 985 | put nw add dst i bitcast f32 v |
| 986 | end |
| 987 | end |
| 988 | fn admit to void |
| 989 | @@GEN_LOAD@@ |
| 990 | x = pread int 3 one 0 1 long FSIZE |
| 991 | if ne x 0 |
| 992 | refuse WSIZE |
| 993 | end |
| 994 | header |
| 995 | hreset |
| 996 | @@GEN_HASH@@ |
| 997 | ok = hfinal |
| 998 | if not ok |
| 999 | refuse WSHA |
| 1000 | end |
| 1001 | for i ulong 64 |
| 1002 | if ne get hd i get sha i |
| 1003 | refuse WSHA |
| 1004 | end |
| 1005 | end |
| 1006 | for l ulong 16 |
| 1007 | loadf32 get tof add mul l 9 4 NE mul l NE |
| 1008 | loadf32 get tof add mul l 9 5 NE mul add 16 l NE |
| 1009 | end |
| 1010 | loadf32 get tof 145 NE mul 32 NE |
| 1011 | for i ulong 32 |
| 1012 | o = add get tof 146 mul i 4 |
| 1013 | v = or or uint fb o shl uint fb add o 1 8 or shl uint fb add o 2 16 shl uint fb add o 3 24 |
| 1014 | put rf i bitcast f32 v |
| 1015 | end |
| 1016 | initc |
| 1017 | ow WLOAD |
| 1018 | osp |
| 1019 | ow WOK |
| 1020 | osp |
| 1021 | ow WBYTES |
| 1022 | osp |
| 1023 | ou FSIZE |
| 1024 | osp |
| 1025 | ow WTENS |
| 1026 | osp |
| 1027 | ou NTENS |
| 1028 | osp |
| 1029 | ow WSHA |
| 1030 | osp |
| 1031 | for i ulong 64 |
| 1032 | oc get hd i |
| 1033 | end |
| 1034 | osp |
| 1035 | ow WPASSES |
| 1036 | oc 32 |
| 1037 | oc 48 |
| 1038 | onl |
| 1039 | end |
| 1040 | note ---- requests ---- |
| 1041 | fn getln to long |
| 1042 | note next request line in ib at st SLS; length, -1 at end of input, -2 for a line over the buffer |
| 1043 | while true bound 999999 |
| 1044 | s = get st SSTART |
| 1045 | f = get st SFILL |
| 1046 | i = s |
| 1047 | while lt i f bound 999999 |
| 1048 | if eq get ib i 10 |
| 1049 | put st SSTART add i 1 |
| 1050 | if eq get st SSKIP 1 |
| 1051 | put st SSKIP 0 |
| 1052 | ret long -2 |
| 1053 | end |
| 1054 | put st SLS s |
| 1055 | ret long sub i s |
| 1056 | end |
| 1057 | i = add i 1 |
| 1058 | end |
| 1059 | if eq get st SEOF 1 |
| 1060 | if lt s f |
| 1061 | put st SSTART f |
| 1062 | if eq get st SSKIP 1 |
| 1063 | put st SSKIP 0 |
| 1064 | ret long -2 |
| 1065 | end |
| 1066 | put st SLS s |
| 1067 | ret long sub f s |
| 1068 | end |
| 1069 | if eq get st SSKIP 1 |
| 1070 | put st SSKIP 0 |
| 1071 | ret long -2 |
| 1072 | end |
| 1073 | ret long -1 |
| 1074 | end |
| 1075 | n = sub f s |
| 1076 | for j ulong n |
| 1077 | put ib j get ib add s j |
| 1078 | end |
| 1079 | if eq n IBCAP |
| 1080 | n = ulong 0 |
| 1081 | put st SSKIP 1 |
| 1082 | end |
| 1083 | put st SSTART 0 |
| 1084 | put st SFILL n |
| 1085 | r = read int 0 ib n sub IBCAP n |
| 1086 | if le r 0 |
| 1087 | put st SEOF 1 |
| 1088 | else |
| 1089 | put st SFILL add n ulong r |
| 1090 | end |
| 1091 | end |
| 1092 | ret long -1 |
| 1093 | end |
| 1094 | fn tword k ulong to bool |
| 1095 | note the request word k at the cursor, then a space or the line end; the cursor moves past the word |
| 1096 | p = get st SPC |
| 1097 | e = get st SPE |
| 1098 | n = ulong 0 |
| 1099 | for i ulong 16 |
| 1100 | c = get tw add mul k 16 i |
| 1101 | if eq c 32 |
| 1102 | break |
| 1103 | end |
| 1104 | n = add n 1 |
| 1105 | end |
| 1106 | if gt add p n e |
| 1107 | ret false |
| 1108 | end |
| 1109 | for i ulong n |
| 1110 | if ne get ib add p i get tw add mul k 16 i |
| 1111 | ret false |
| 1112 | end |
| 1113 | end |
| 1114 | q = add p n |
| 1115 | if lt q e |
| 1116 | if ne get ib q 32 |
| 1117 | ret false |
| 1118 | end |
| 1119 | end |
| 1120 | put st SPC q |
| 1121 | ret true |
| 1122 | end |
| 1123 | fn tsp to bool |
| 1124 | note exactly one space, then more text |
| 1125 | p = get st SPC |
| 1126 | e = get st SPE |
| 1127 | if ge add p 1 e |
| 1128 | ret false |
| 1129 | end |
| 1130 | if ne get ib p 32 |
| 1131 | ret false |
| 1132 | end |
| 1133 | if eq get ib add p 1 32 |
| 1134 | ret false |
| 1135 | end |
| 1136 | put st SPC add p 1 |
| 1137 | ret true |
| 1138 | end |
| 1139 | fn tnum to ulong |
| 1140 | note a decimal number of 1..18 digits; anything else sets st SERR |
| 1141 | p = get st SPC |
| 1142 | e = get st SPE |
| 1143 | v = ulong 0 |
| 1144 | n = ulong 0 |
| 1145 | while lt p e bound 100 |
| 1146 | c = get ib p |
| 1147 | if eq c 32 |
| 1148 | break |
| 1149 | end |
| 1150 | if or lt c 48 gt c 57 |
| 1151 | put st SERR 1 |
| 1152 | ret 0 |
| 1153 | end |
| 1154 | v = add mul v 10 ulong sub c 48 |
| 1155 | n = add n 1 |
| 1156 | if gt n 18 |
| 1157 | put st SERR 1 |
| 1158 | ret 0 |
| 1159 | end |
| 1160 | p = add p 1 |
| 1161 | end |
| 1162 | if eq n 0 |
| 1163 | put st SERR 1 |
| 1164 | end |
| 1165 | put st SPC p |
| 1166 | ret v |
| 1167 | end |
| 1168 | fn oid to void |
| 1169 | n = get st SIDN |
| 1170 | if eq n 0 |
| 1171 | ow WQMARK |
| 1172 | ret |
| 1173 | end |
| 1174 | s = get st SIDS |
| 1175 | for i ulong n |
| 1176 | oc get ib add s i |
| 1177 | end |
| 1178 | end |
| 1179 | fn jref w ulong to void |
| 1180 | put st SOB 0 |
| 1181 | ow WJOB |
| 1182 | osp |
| 1183 | oid |
| 1184 | osp |
| 1185 | ow WSTATUS |
| 1186 | osp |
| 1187 | ow WREFUSED |
| 1188 | osp |
| 1189 | ow WREASON |
| 1190 | osp |
| 1191 | ow w |
| 1192 | onl |
| 1193 | end |
| 1194 | fn tfield k ulong to ulong |
| 1195 | note " <word k> <number>"; a failure sets st SERR |
| 1196 | if not tsp |
| 1197 | put st SERR 1 |
| 1198 | ret 0 |
| 1199 | end |
| 1200 | if not tword k |
| 1201 | put st SERR 1 |
| 1202 | ret 0 |
| 1203 | end |
| 1204 | if not tsp |
| 1205 | put st SERR 1 |
| 1206 | ret 0 |
| 1207 | end |
| 1208 | ret tnum |
| 1209 | end |
| 1210 | fn pid to bool |
| 1211 | note the job id: 1..32 printable bytes up to the next space |
| 1212 | p = get st SPC |
| 1213 | e = get st SPE |
| 1214 | q = p |
| 1215 | while lt q e bound 100 |
| 1216 | c = get ib q |
| 1217 | if eq c 32 |
| 1218 | break |
| 1219 | end |
| 1220 | if or lt c 33 gt c 126 |
| 1221 | ret false |
| 1222 | end |
| 1223 | q = add q 1 |
| 1224 | end |
| 1225 | if gt sub q p 32 |
| 1226 | ret false |
| 1227 | end |
| 1228 | put st SIDS p |
| 1229 | put st SIDN sub q p |
| 1230 | put st SPC q |
| 1231 | ret true |
| 1232 | end |
| 1233 | fn pids to ulong |
| 1234 | note " <id>" repeated to the line end into ids; returns the count or a refusal word plus 1000 |
| 1235 | k = ulong 0 |
| 1236 | while lt get st SPC get st SPE bound 100000 |
| 1237 | if not tsp |
| 1238 | ret add WSYNTAX 1000 |
| 1239 | end |
| 1240 | v = tnum |
| 1241 | if eq get st SERR 1 |
| 1242 | ret add WSYNTAX 1000 |
| 1243 | end |
| 1244 | if ge k CTX |
| 1245 | ret add WTOOMANY 1000 |
| 1246 | end |
| 1247 | if ge v NV |
| 1248 | ret add WBADID 1000 |
| 1249 | end |
| 1250 | put ids k v |
| 1251 | k = add k 1 |
| 1252 | end |
| 1253 | ret k |
| 1254 | end |
| 1255 | fn parse ls ulong n ulong to ulong |
| 1256 | note fills SIDS SIDN SGEN SBUD SSTOP SFAULT SNID and ids; returns 99 when accepted, else a refusal word |
| 1257 | put st SPC ls |
| 1258 | put st SPE add ls n |
| 1259 | put st SERR 0 |
| 1260 | put st SIDN 0 |
| 1261 | if not tword WJOB |
| 1262 | ret WSYNTAX |
| 1263 | end |
| 1264 | if not tsp |
| 1265 | ret WSYNTAX |
| 1266 | end |
| 1267 | if not pid |
| 1268 | ret WSYNTAX |
| 1269 | end |
| 1270 | put st SGEN tfield WGEN |
| 1271 | put st SBUD tfield WBUDGET |
| 1272 | put st SSTOP tfield WSTOP |
| 1273 | put st SFAULT tfield WFAULT |
| 1274 | if eq get st SERR 1 |
| 1275 | ret WSYNTAX |
| 1276 | end |
| 1277 | if not tsp |
| 1278 | ret WSYNTAX |
| 1279 | end |
| 1280 | if not tword WIDS |
| 1281 | ret WSYNTAX |
| 1282 | end |
| 1283 | k = pids |
| 1284 | if ge k 1000 |
| 1285 | ret sub k 1000 |
| 1286 | end |
| 1287 | if eq k 0 |
| 1288 | ret WEMPTY |
| 1289 | end |
| 1290 | if or eq get st SGEN 0 gt get st SFAULT 1 |
| 1291 | ret WRANGE |
| 1292 | end |
| 1293 | if gt sub add k get st SGEN 1 CTX |
| 1294 | ret WCONTEXT |
| 1295 | end |
| 1296 | put st SNID k |
| 1297 | ret 99 |
| 1298 | end |
| 1299 | fn tpush t ulong to void |
| 1300 | nt = get st SNTOK |
| 1301 | put tk nt t |
| 1302 | put tb nt ulong bitcast uint get ac add ALG t |
| 1303 | put st SNTOK add nt 1 |
| 1304 | end |
| 1305 | fn jrun to ulong |
| 1306 | note prefill then greedy steps; returns the passes run; st SCAN 1 when the budget ran out |
| 1307 | n = get st SNID |
| 1308 | bud = get st SBUD |
| 1309 | put st SRB 0 |
| 1310 | put st SCAN 0 |
| 1311 | put st SNTOK 0 |
| 1312 | put st SFLT 4294967295 |
| 1313 | if eq get st SFAULT 1 |
| 1314 | put st SFLT div n 2 |
| 1315 | end |
| 1316 | pos = ulong 0 |
| 1317 | for i ulong n |
| 1318 | if ge pos bud |
| 1319 | put st SCAN 1 |
| 1320 | ret pos |
| 1321 | end |
| 1322 | fwd get ids i pos eq add i 1 n |
| 1323 | pos = add pos 1 |
| 1324 | end |
| 1325 | for s ulong get st SGEN |
| 1326 | t = amax |
| 1327 | tpush t |
| 1328 | if eq t get st SSTOP |
| 1329 | ret pos |
| 1330 | end |
| 1331 | if eq add s 1 get st SGEN |
| 1332 | ret pos |
| 1333 | end |
| 1334 | if ge pos bud |
| 1335 | put st SCAN 1 |
| 1336 | ret pos |
| 1337 | end |
| 1338 | fwd t pos true |
| 1339 | pos = add pos 1 |
| 1340 | end |
| 1341 | ret pos |
| 1342 | end |
| 1343 | fn kvclr pos ulong to void |
| 1344 | note discard this job's KV state (the weights stay resident) |
| 1345 | for l ulong 16 |
| 1346 | for p ulong pos |
| 1347 | at = add mul l mul CTX KVD mul p KVD |
| 1348 | for i ulong KVD |
| 1349 | put kc add at i 0.0 |
| 1350 | put vc add at i 0.0 |
| 1351 | end |
| 1352 | end |
| 1353 | end |
| 1354 | end |
| 1355 | fn jout pos ulong to void |
| 1356 | nt = get st SNTOK |
| 1357 | put st SOB 0 |
| 1358 | ow WJOB |
| 1359 | osp |
| 1360 | oid |
| 1361 | osp |
| 1362 | ow WSTATUS |
| 1363 | osp |
| 1364 | if eq get st SCAN 1 |
| 1365 | ow WCANCEL |
| 1366 | else |
| 1367 | ow WOK |
| 1368 | end |
| 1369 | osp |
| 1370 | ow WPASSES |
| 1371 | osp |
| 1372 | ou pos |
| 1373 | osp |
| 1374 | ow WTOKENS |
| 1375 | for i ulong nt |
| 1376 | osp |
| 1377 | ou get tk i |
| 1378 | end |
| 1379 | onl |
| 1380 | ow WBITS |
| 1381 | for i ulong nt |
| 1382 | osp |
| 1383 | ou get tb i |
| 1384 | end |
| 1385 | onl |
| 1386 | ow WACCT |
| 1387 | osp |
| 1388 | ow WJOB |
| 1389 | osp |
| 1390 | oid |
| 1391 | osp |
| 1392 | ow WROWB |
| 1393 | osp |
| 1394 | ou get st SRB |
| 1395 | osp |
| 1396 | ow WPASSES |
| 1397 | osp |
| 1398 | ou pos |
| 1399 | onl |
| 1400 | end |
| 1401 | fn runjob to void |
| 1402 | pos = jrun |
| 1403 | kvclr pos |
| 1404 | jout pos |
| 1405 | end |
| 1406 | fn main to int |
| 1407 | admit |
| 1408 | while true bound 999999 |
| 1409 | n = getln |
| 1410 | if eq n -1 |
| 1411 | ret 0 |
| 1412 | end |
| 1413 | if eq n -2 |
| 1414 | put st SIDN 0 |
| 1415 | jref WTOOLONG |
| 1416 | continue |
| 1417 | end |
| 1418 | if eq n 0 |
| 1419 | continue |
| 1420 | end |
| 1421 | w = parse get st SLS ulong n |
| 1422 | if ne w 99 |
| 1423 | jref w |
| 1424 | continue |
| 1425 | end |
| 1426 | runjob |
| 1427 | end |
| 1428 | ret 0 |
| 1429 | end |