aien-train
AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.
Brought by drakestapleton
aien-train / native / si19 / world.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note aien-train#19 offline standalone experiment: a tiny from-initialization experiential |
| 3 | note world model. Predictor (p,v,u) -> (p2,v2) is one affine map X[2,3]W[3,2]+b[2] trained |
| 4 | note only through the pinned ecosystem6 afstep (L720 add32/mul32 forward, explicit gradient, |
| 5 | note SGD). Not M23/M24 qualification; M20-M22 stay NOT QUALIFIED; no production path. |
| 6 | note All resumable trainer state lives in WS (ulong), TL (experience log) and FS (fixture). |
| 7 | note WS: 0 event, 1 updates, 2 sampler rng, 3 ring A pushed, 4 ring B pushed, 5..7 last loss |
| 8 | note per arm. Params (W[0..5] row-major k*2+col, b[6..7]): 8 M, 16 N, 24 S, 32 MA, 40 INIT. |
| 9 | note 48..111 eight M snapshots after 32,64,..,256 experiences. 112..119 last batch ids. |
| 10 | note Lineage use counts per experience id: 128 M, 640 N, 1152 S, 1664 MA (512 ids each). |
| 11 | note Bounded replay rings of experience ids: 2176 A (64), 2240 B (64). |
| 12 | note Arms: M main (A, then B with A replay), N (copy of M after A, then B only), |
| 13 | note S shuffled-experience control (inputs and targets drawn independently). |
| 14 | note TL: 8 longs per experience id: id, p, v, u, p2, v2, done, episode*16+step. |
| 15 | global WS ulong 2304 |
| 16 | global TL long 4096 |
| 17 | global HO long 1024 |
| 18 | global FQ long 36960 |
| 19 | global PR ulong 2 |
| 20 | global EH ulong 1 |
| 21 | global CB uchar 103504 |
| 22 | global DG uchar 24 |
| 23 | global XB uchar 1 |
| 24 | global QV long 4 |
| 25 | |
| 26 | fn wnum n ulong to void |
| 27 | p = ulong 24 |
| 28 | if eq n 0 |
| 29 | p = 23 |
| 30 | put DG 23 48 |
| 31 | end |
| 32 | while gt n 0 bound 24 |
| 33 | p = sub p 1 |
| 34 | put DG p uchar add 48 rem n 10 |
| 35 | n = div n 10 |
| 36 | end |
| 37 | w = write 1 DG p sub 24 p |
| 38 | if ne w long sub 24 p |
| 39 | exit 90 |
| 40 | end |
| 41 | end |
| 42 | |
| 43 | fn wdec x ulong to void |
| 44 | note Positive finite binary32 bits as decimal, truncated to six places. |
| 45 | e = and shr x 23 255 |
| 46 | m = and x 8388607 |
| 47 | if eq e 0 |
| 48 | e = 1 |
| 49 | else |
| 50 | m = or m 8388608 |
| 51 | end |
| 52 | if ge e 160 |
| 53 | exit 91 |
| 54 | end |
| 55 | s = mul m 1000000 |
| 56 | if ge e 150 |
| 57 | s = shl s sub e 150 |
| 58 | else |
| 59 | k = sub 150 e |
| 60 | if ge k 64 |
| 61 | s = 0 |
| 62 | else |
| 63 | s = shr s k |
| 64 | end |
| 65 | end |
| 66 | wnum div s 1000000 |
| 67 | f = rem s 1000000 |
| 68 | put DG 0 46 |
| 69 | d = ulong 100000 |
| 70 | for i ulong 6 |
| 71 | put DG add i 1 uchar add 48 rem div f d 10 |
| 72 | d = div d 10 |
| 73 | end |
| 74 | w = write 1 DG 0 7 |
| 75 | if ne w 7 |
| 76 | exit 90 |
| 77 | end |
| 78 | end |
| 79 | |
| 80 | fn wsenc x long to ulong |
| 81 | if lt x -1099511627776 |
| 82 | exit 83 |
| 83 | end |
| 84 | ret ulong add x 1099511627776 |
| 85 | end |
| 86 | |
| 87 | fn wsdec u ulong to long |
| 88 | ret sub long u 1099511627776 |
| 89 | end |
| 90 | |
| 91 | fn wsabs x long to long |
| 92 | if lt x 0 |
| 93 | ret neg x |
| 94 | end |
| 95 | ret x |
| 96 | end |
| 97 | |
| 98 | fn wmbits n long scale ulong to ulong |
| 99 | note Exact binary32 bits of n/2^scale for |n| below 2^24. |
| 100 | if eq n 0 |
| 101 | ret 0 |
| 102 | end |
| 103 | sign = ulong 0 |
| 104 | if lt n 0 |
| 105 | sign = 2147483648 |
| 106 | n = neg n |
| 107 | end |
| 108 | m = ulong n |
| 109 | pw = ulong 0 |
| 110 | q = m |
| 111 | while gt q 1 bound 64 |
| 112 | q = shr q 1 |
| 113 | pw = add pw 1 |
| 114 | end |
| 115 | if gt pw 23 |
| 116 | exit 81 |
| 117 | end |
| 118 | ret or sign or shl add sub pw scale 127 23 sub shl m sub 23 pw 8388608 |
| 119 | end |
| 120 | |
| 121 | fn wmdesc to void |
| 122 | note Trusted M20 descriptors and allocator snapshots for the eight afstep slots. |
| 123 | for i ulong 192 |
| 124 | put AD i 0 |
| 125 | end |
| 126 | for i ulong 64 |
| 127 | put AS i 0 |
| 128 | end |
| 129 | for t ulong 8 |
| 130 | rank = ulong 2 |
| 131 | rows = ulong 2 |
| 132 | cols = ulong 3 |
| 133 | count = ulong 6 |
| 134 | if or eq t 1 eq t 6 |
| 135 | rows = 3 |
| 136 | cols = 2 |
| 137 | end |
| 138 | if or eq t 3 eq t 4 |
| 139 | cols = 2 |
| 140 | count = 4 |
| 141 | end |
| 142 | if or eq t 2 eq t 7 |
| 143 | rank = 1 |
| 144 | cols = 1 |
| 145 | count = 2 |
| 146 | end |
| 147 | p = mul t 24 |
| 148 | put AD p 1 |
| 149 | put AD add p 1 1 |
| 150 | put AD add p 2 rank |
| 151 | put AD add p 3 count |
| 152 | put AD add p 5 add t 1 |
| 153 | put AD add p 6 17 |
| 154 | put AD add p 8 rows |
| 155 | put AD add p 16 cols |
| 156 | if eq rank 2 |
| 157 | put AD add p 9 cols |
| 158 | put AD add p 17 1 |
| 159 | end |
| 160 | p = mul t 8 |
| 161 | put AS p 1 |
| 162 | put AS add p 2 add t 1 |
| 163 | put AS add p 3 17 |
| 164 | put AS add p 4 1 |
| 165 | put AS add p 5 count |
| 166 | end |
| 167 | end |
| 168 | |
| 169 | fn wscopy dst ulong src ulong n ulong to void |
| 170 | for i ulong n |
| 171 | put WS add dst i get WS add src i |
| 172 | end |
| 173 | end |
| 174 | |
| 175 | fn wsrand to ulong |
| 176 | n = and add mul get WS 2 1664525 1013904223 4294967295 |
| 177 | put WS 2 n |
| 178 | ret shr n 8 |
| 179 | end |
| 180 | |
| 181 | fn wmrow row ulong xid ulong tid ulong to void |
| 182 | note Row inputs from experience xid, target from tid (equal except in the shuffled arm). |
| 183 | x = mul xid 8 |
| 184 | t = mul tid 8 |
| 185 | c = mul row 3 |
| 186 | put AP c wmbits get TL add x 1 0 |
| 187 | put AP add c 1 wmbits get TL add x 2 0 |
| 188 | put AP add c 2 wmbits get TL add x 3 0 |
| 189 | c = add 48 mul row 2 |
| 190 | put AP c wmbits get TL add t 4 0 |
| 191 | put AP add c 1 wmbits get TL add t 5 0 |
| 192 | end |
| 193 | |
| 194 | fn wmapply arm ulong to void |
| 195 | note One real afstep: forward, explicit gradient and SGD at lr 2^-9 on the loaded batch. |
| 196 | pb = add 8 mul arm 8 |
| 197 | for i ulong 6 |
| 198 | put AP add 16 i get WS add pb i |
| 199 | end |
| 200 | put AP 32 get WS add pb 6 |
| 201 | put AP 33 get WS add pb 7 |
| 202 | st = afstep 989855744 |
| 203 | if ne st 0 |
| 204 | exit 82 |
| 205 | end |
| 206 | for i ulong 6 |
| 207 | put WS add pb i get AP add 16 i |
| 208 | end |
| 209 | put WS add pb 6 get AP 32 |
| 210 | put WS add pb 7 get AP 33 |
| 211 | put WS add 5 arm get AL 0 |
| 212 | put WS 1 add get WS 1 1 |
| 213 | end |
| 214 | |
| 215 | fn wmuse arm ulong id ulong to void |
| 216 | k = add add 128 mul arm 512 id |
| 217 | put WS k add get WS k 1 |
| 218 | end |
| 219 | |
| 220 | fn wspick ring ulong to ulong |
| 221 | note Uniform pick among the retained ids of bounded ring 0 (A) or 1 (B). |
| 222 | n = get WS add 3 ring |
| 223 | if gt n 64 |
| 224 | n = 64 |
| 225 | end |
| 226 | ret get WS add add 2176 mul ring 64 rem wsrand n |
| 227 | end |
| 228 | |
| 229 | fn wstrain arm ulong ra ulong rb ulong to void |
| 230 | a = wspick ra |
| 231 | c = wspick rb |
| 232 | wmrow 0 a a |
| 233 | wmrow 1 c c |
| 234 | wmapply arm |
| 235 | wmuse arm a |
| 236 | wmuse arm c |
| 237 | put WS add 112 mul arm 2 a |
| 238 | put WS add 113 mul arm 2 c |
| 239 | end |
| 240 | |
| 241 | fn wsshuf to void |
| 242 | note Shuffled-experience control: same ring, same schedule, outcome detached from state. |
| 243 | a = wspick 0 |
| 244 | c = wspick 0 |
| 245 | ta = wspick 0 |
| 246 | tc = wspick 0 |
| 247 | wmrow 0 a ta |
| 248 | wmrow 1 c tc |
| 249 | wmapply 2 |
| 250 | wmuse 2 a |
| 251 | wmuse 2 ta |
| 252 | wmuse 2 c |
| 253 | wmuse 2 tc |
| 254 | put WS 116 a |
| 255 | put WS 117 ta |
| 256 | put WS 118 c |
| 257 | put WS 119 tc |
| 258 | end |
| 259 | |
| 260 | fn wscollect s ulong to void |
| 261 | note Admit one fixture transition as experience id e and push it into ring s. |
| 262 | e = get WS 0 |
| 263 | u = fxact s |
| 264 | fxstep s u |
| 265 | r = mul e 8 |
| 266 | put TL r long e |
| 267 | for i ulong 7 |
| 268 | put TL add r add i 1 get FT i |
| 269 | end |
| 270 | n = get WS add 3 s |
| 271 | put WS add add 2176 mul s 64 rem n 64 e |
| 272 | put WS add 3 s add n 1 |
| 273 | end |
| 274 | |
| 275 | fn wsevent to void |
| 276 | e = get WS 0 |
| 277 | if lt e 256 |
| 278 | wscollect 0 |
| 279 | for k ulong 4 |
| 280 | wstrain 0 0 0 |
| 281 | wsshuf |
| 282 | end |
| 283 | else |
| 284 | wscollect 1 |
| 285 | for k ulong 4 |
| 286 | wstrain 0 0 1 |
| 287 | wstrain 1 1 1 |
| 288 | end |
| 289 | end |
| 290 | e = add e 1 |
| 291 | put WS 0 e |
| 292 | if and le e 256 eq rem e 32 0 |
| 293 | wscopy add 48 mul sub div e 32 1 8 8 8 |
| 294 | end |
| 295 | if eq e 256 |
| 296 | wscopy 32 8 8 |
| 297 | wscopy 16 8 8 |
| 298 | wscopy 1664 128 512 |
| 299 | wscopy 640 128 512 |
| 300 | end |
| 301 | end |
| 302 | |
| 303 | fn wsinit to void |
| 304 | for i ulong 2304 |
| 305 | put WS i 0 |
| 306 | end |
| 307 | for i ulong 4096 |
| 308 | put TL i 0 |
| 309 | end |
| 310 | for i ulong 64 |
| 311 | put FS i 0 |
| 312 | end |
| 313 | wmdesc |
| 314 | put WS 2 19019 |
| 315 | note Seeded random initialization in {-4..3}/16; no pretrained or corpus prior. |
| 316 | for i ulong 8 |
| 317 | put WS add 40 i wmbits sub long rem wsrand 8 4 4 |
| 318 | end |
| 319 | wscopy 8 40 8 |
| 320 | wscopy 24 40 8 |
| 321 | fxinit 0 1 1901 0 |
| 322 | fxinit 1 -1 1902 0 |
| 323 | end |
| 324 | |
| 325 | fn wsrun limit ulong to void |
| 326 | while lt get WS 0 limit bound 600 |
| 327 | wsevent |
| 328 | end |
| 329 | end |
| 330 | |
| 331 | fn wsaffect arm ulong id ulong to bool |
| 332 | note Lineage query for #20: did experience id contribute to this arm's parameters? |
| 333 | ret gt get WS add add 128 mul arm 512 id 0 |
| 334 | end |
| 335 | |
| 336 | fn wsids arm ulong to ulong |
| 337 | n = ulong 0 |
| 338 | for i ulong 512 |
| 339 | if gt get WS add add 128 mul arm 512 i 0 |
| 340 | n = add n 1 |
| 341 | end |
| 342 | end |
| 343 | ret n |
| 344 | end |
| 345 | |
| 346 | fn wsuses arm ulong to ulong |
| 347 | n = ulong 0 |
| 348 | for i ulong 512 |
| 349 | n = add n get WS add add 128 mul arm 512 i |
| 350 | end |
| 351 | ret n |
| 352 | end |
| 353 | |
| 354 | fn wmpred pb ulong p long v long u long to void |
| 355 | note Forward only, in afstep's exact operation order: b[col] + x0*w + x1*w + x2*w. |
| 356 | x0 = wmbits p 0 |
| 357 | x1 = wmbits v 0 |
| 358 | x2 = wmbits u 0 |
| 359 | for col ulong 2 |
| 360 | a = get WS add add pb 6 col |
| 361 | a = add32 a mul32 x0 get WS add pb col |
| 362 | a = add32 a mul32 x1 get WS add add pb 2 col |
| 363 | a = add32 a mul32 x2 get WS add add pb 4 col |
| 364 | put PR col a |
| 365 | end |
| 366 | end |
| 367 | |
| 368 | fn wskey p long v long u long to ulong |
| 369 | ret ulong add add mul add p 12 21 mul add v 3 3 add u 1 |
| 370 | end |
| 371 | |
| 372 | fn wsout dp long dv long to ulong |
| 373 | ret ulong add mul add dp 3 5 add dv 2 |
| 374 | end |
| 375 | |
| 376 | fn wsfreq1 fb ulong r ulong to void |
| 377 | p = get TL add r 1 |
| 378 | v = get TL add r 2 |
| 379 | u = get TL add r 3 |
| 380 | o = wsout sub get TL add r 4 p sub get TL add r 5 v |
| 381 | k = mul wskey p v u 35 |
| 382 | k = add fb add k o |
| 383 | put FQ k add get FQ k 1 |
| 384 | k = mul ulong add u 1 35 |
| 385 | k = add add fb 18375 add k o |
| 386 | put FQ k add get FQ k 1 |
| 387 | end |
| 388 | |
| 389 | fn wsfreq env ulong to void |
| 390 | note Frequency tables from all 256 admitted experiences of env: per (p,v,u) key and per action. |
| 391 | fb = mul env 18480 |
| 392 | for i ulong 18480 |
| 393 | put FQ add fb i 0 |
| 394 | end |
| 395 | for j ulong 256 |
| 396 | wsfreq1 fb mul add mul env 256 j 8 |
| 397 | end |
| 398 | end |
| 399 | |
| 400 | fn wsseen env ulong p long v long u long to bool |
| 401 | k = add mul env 18480 mul wskey p v u 35 |
| 402 | for o ulong 35 |
| 403 | if gt get FQ add k o 0 |
| 404 | ret true |
| 405 | end |
| 406 | end |
| 407 | ret false |
| 408 | end |
| 409 | |
| 410 | fn wsargmax base ulong to ulong |
| 411 | note Most frequent outcome, lowest index on ties; 35 when no observation. |
| 412 | best = ulong 35 |
| 413 | bc = long 0 |
| 414 | for o ulong 35 |
| 415 | c = get FQ add base o |
| 416 | if gt c bc |
| 417 | bc = c |
| 418 | best = o |
| 419 | end |
| 420 | end |
| 421 | ret best |
| 422 | end |
| 423 | |
| 424 | fn wsmode env ulong p long v long u long to ulong |
| 425 | fb = mul env 18480 |
| 426 | o = wsargmax add fb mul wskey p v u 35 |
| 427 | if lt o 35 |
| 428 | put EH 0 add get EH 0 1 |
| 429 | ret o |
| 430 | end |
| 431 | o = wsargmax add add fb 18375 mul ulong add u 1 35 |
| 432 | if lt o 35 |
| 433 | ret o |
| 434 | end |
| 435 | ret wsout 0 0 |
| 436 | end |
| 437 | |
| 438 | fn wsnear env ulong p long v long u long to ulong |
| 439 | note Nearest admitted experience by L1 distance over (p,v,u), lowest id on ties. |
| 440 | best = ulong 0 |
| 441 | bd = long 1000 |
| 442 | for j ulong 256 |
| 443 | id = add mul env 256 j |
| 444 | r = mul id 8 |
| 445 | d = wsabs sub p get TL add r 1 |
| 446 | d = add d wsabs sub v get TL add r 2 |
| 447 | d = add d wsabs sub u get TL add r 3 |
| 448 | if lt d bd |
| 449 | bd = d |
| 450 | best = id |
| 451 | end |
| 452 | end |
| 453 | ret best |
| 454 | end |
| 455 | |
| 456 | fn wsheld env ulong to void |
| 457 | note 128 held-out transitions from a separate seed and wide reset region whose (p,v,u) |
| 458 | note key never occurs in env's training experience. Ids live outside the training range. |
| 459 | s = add 2 env |
| 460 | gain = sub 1 mul 2 long env |
| 461 | fxinit s gain add 7001 long env 1 |
| 462 | n = ulong 0 |
| 463 | while lt n 128 bound 200000 |
| 464 | u = fxact s |
| 465 | fxstep s u |
| 466 | if not wsseen env get FT 0 get FT 1 get FT 2 |
| 467 | r = mul n 8 |
| 468 | put HO r long add 1048576 n |
| 469 | for i ulong 7 |
| 470 | put HO add r add i 1 get FT i |
| 471 | end |
| 472 | n = add n 1 |
| 473 | end |
| 474 | end |
| 475 | end |
| 476 | |
| 477 | fn wsput gp long gv long to void |
| 478 | put PR 0 wmbits gp 0 |
| 479 | put PR 1 wmbits gv 0 |
| 480 | end |
| 481 | |
| 482 | fn wsrule env ulong kind ulong to void |
| 483 | note 1 persistence, 2 constant velocity (hand rules), 3 true mean dynamics (reference only). |
| 484 | p = get QV 0 |
| 485 | v = get QV 1 |
| 486 | gp = p |
| 487 | gv = v |
| 488 | if ge kind 2 |
| 489 | gp = add p v |
| 490 | end |
| 491 | if eq kind 3 |
| 492 | g = sub 1 mul 2 long env |
| 493 | gv = add v mul g get QV 2 |
| 494 | end |
| 495 | wsput gp gv |
| 496 | end |
| 497 | |
| 498 | fn wsfreqg env ulong to void |
| 499 | o = wsmode env get QV 0 get QV 1 get QV 2 |
| 500 | dp = sub long div o 5 3 |
| 501 | dv = sub long rem o 5 2 |
| 502 | wsput add get QV 0 dp add get QV 1 dv |
| 503 | end |
| 504 | |
| 505 | fn wsnng env ulong delta bool to void |
| 506 | id = wsnear env get QV 0 get QV 1 get QV 2 |
| 507 | id = mul id 8 |
| 508 | gp = get TL add id 4 |
| 509 | gv = get TL add id 5 |
| 510 | if delta |
| 511 | gp = add get QV 0 sub gp get TL add id 1 |
| 512 | gv = add get QV 1 sub gv get TL add id 2 |
| 513 | end |
| 514 | wsput gp gv |
| 515 | end |
| 516 | |
| 517 | fn wsguess env ulong kind ulong pb ulong n ulong to void |
| 518 | note kind 0 model at pb, 1 persistence rule, 2 constant-velocity rule, 3 true mean |
| 519 | note dynamics (reference only), 4 frequency mode, 5 nearest neighbour, 6 neighbour delta. |
| 520 | r = mul n 8 |
| 521 | put QV 0 get HO add r 1 |
| 522 | put QV 1 get HO add r 2 |
| 523 | put QV 2 get HO add r 3 |
| 524 | if eq kind 0 |
| 525 | wmpred pb get QV 0 get QV 1 get QV 2 |
| 526 | ret |
| 527 | end |
| 528 | if eq kind 4 |
| 529 | wsfreqg env |
| 530 | ret |
| 531 | end |
| 532 | if ge kind 5 |
| 533 | wsnng env eq kind 6 |
| 534 | ret |
| 535 | end |
| 536 | wsrule env kind |
| 537 | end |
| 538 | |
| 539 | fn wserr pred ulong t long to ulong |
| 540 | d = add32 pred xor wmbits t 0 2147483648 |
| 541 | ret mul32 d d |
| 542 | end |
| 543 | |
| 544 | fn wsmse env ulong kind ulong pb ulong to ulong |
| 545 | note Mean over the 128 held-out items of squared error summed over (p2,v2), binary32. |
| 546 | sum = ulong 0 |
| 547 | for n ulong 128 |
| 548 | wsguess env kind pb n |
| 549 | r = mul n 8 |
| 550 | sum = add32 sum wserr get PR 0 get HO add r 4 |
| 551 | sum = add32 sum wserr get PR 1 get HO add r 5 |
| 552 | end |
| 553 | ret mul32 sum 1006632960 |
| 554 | end |
| 555 | |
| 556 | fn ckput pos ulong v ulong to void |
| 557 | for i ulong 8 |
| 558 | put CB add pos i uchar and shr v mul i 8 255 |
| 559 | end |
| 560 | end |
| 561 | |
| 562 | fn ckget pos ulong to ulong |
| 563 | v = ulong 0 |
| 564 | for i ulong 8 |
| 565 | v = or v shl ulong get CB add pos i mul i 8 |
| 566 | end |
| 567 | ret v |
| 568 | end |
| 569 | |
| 570 | fn ckfnv base ulong n ulong to ulong |
| 571 | note FNV-1a 64 integrity check over the image; detects damage, does not authenticate. |
| 572 | h = ulong 14695981039346656037 |
| 573 | for i ulong n |
| 574 | h = mul xor h ulong get CB add base i 1099511628211 |
| 575 | end |
| 576 | ret h |
| 577 | end |
| 578 | |
| 579 | fn cksave base ulong to void |
| 580 | note 51752-byte image: magic, version1, cells6464, event, WS, TL, FS, FNV-1a 64. |
| 581 | ckput base 5423263413301234003 |
| 582 | ckput add base 8 1 |
| 583 | ckput add base 16 6464 |
| 584 | ckput add base 24 get WS 0 |
| 585 | for i ulong 2304 |
| 586 | ckput add add base 32 mul i 8 get WS i |
| 587 | end |
| 588 | for i ulong 4096 |
| 589 | ckput add add base 18464 mul i 8 wsenc get TL i |
| 590 | end |
| 591 | for i ulong 64 |
| 592 | ckput add add base 51232 mul i 8 wsenc get FS i |
| 593 | end |
| 594 | ckput add base 51744 ckfnv base 51744 |
| 595 | end |
| 596 | |
| 597 | fn ckupdates e ulong to ulong |
| 598 | if le e 256 |
| 599 | ret mul e 8 |
| 600 | end |
| 601 | ret add 2048 mul sub e 256 8 |
| 602 | end |
| 603 | |
| 604 | fn ckcheck base ulong to int |
| 605 | note Semantic admission before any state is replaced. |
| 606 | e = ckget add base 32 |
| 607 | if or gt e 512 ne e ckget add base 24 |
| 608 | ret -6 |
| 609 | end |
| 610 | na = e |
| 611 | nb = ulong 0 |
| 612 | if gt e 256 |
| 613 | na = 256 |
| 614 | nb = sub e 256 |
| 615 | end |
| 616 | if or ne ckget add base 56 na ne ckget add base 64 nb |
| 617 | ret -7 |
| 618 | end |
| 619 | if ne ckget add base 40 ckupdates e |
| 620 | ret -7 |
| 621 | end |
| 622 | for i ulong 104 |
| 623 | if not affinite ckget add add base 96 mul i 8 |
| 624 | ret -8 |
| 625 | end |
| 626 | end |
| 627 | for i ulong 128 |
| 628 | if ge ckget add add base 17440 mul i 8 512 |
| 629 | ret -10 |
| 630 | end |
| 631 | end |
| 632 | for i ulong 4160 |
| 633 | if ge ckget add add base 18464 mul i 8 2199023255552 |
| 634 | ret -9 |
| 635 | end |
| 636 | end |
| 637 | ret 0 |
| 638 | end |
| 639 | |
| 640 | fn ckload base ulong n ulong to int |
| 641 | note Refuses without changing WS, TL or FS: length, magic, version, size, checksum, semantics. |
| 642 | if ne n 51752 |
| 643 | ret -1 |
| 644 | end |
| 645 | if ne ckget base 5423263413301234003 |
| 646 | ret -2 |
| 647 | end |
| 648 | if ne ckget add base 8 1 |
| 649 | ret -3 |
| 650 | end |
| 651 | if ne ckget add base 16 6464 |
| 652 | ret -4 |
| 653 | end |
| 654 | if ne ckfnv base 51744 ckget add base 51744 |
| 655 | ret -5 |
| 656 | end |
| 657 | st = ckcheck base |
| 658 | if ne st 0 |
| 659 | ret st |
| 660 | end |
| 661 | for i ulong 2304 |
| 662 | put WS i ckget add add base 32 mul i 8 |
| 663 | end |
| 664 | for i ulong 4096 |
| 665 | put TL i wsdec ckget add add base 18464 mul i 8 |
| 666 | end |
| 667 | for i ulong 64 |
| 668 | put FS i wsdec ckget add add base 51232 mul i 8 |
| 669 | end |
| 670 | wmdesc |
| 671 | ret 0 |
| 672 | end |
| 673 | |
| 674 | fn ckread base ulong cap ulong to ulong |
| 675 | note Read stdin into CB from base until end of input or cap bytes; returns the count. |
| 676 | n = ulong 0 |
| 677 | while lt n cap bound 999999 |
| 678 | r = read 0 CB add base n sub cap n |
| 679 | if lt r 0 |
| 680 | exit 74 |
| 681 | end |
| 682 | if eq r 0 |
| 683 | ret n |
| 684 | end |
| 685 | n = add n ulong r |
| 686 | end |
| 687 | r = read 0 XB 0 1 |
| 688 | if ne r 0 |
| 689 | ret add n 1 |
| 690 | end |
| 691 | ret n |
| 692 | end |
| 693 | |
| 694 | fn ckwrite fd int base ulong n ulong to void |
| 695 | note Write CB[base..base+n) to fd fully (fd 3 is opened by host glue). |
| 696 | o = ulong 0 |
| 697 | while lt o n bound 999999 |
| 698 | w = write fd CB add base o sub n o |
| 699 | if le w 0 |
| 700 | exit 74 |
| 701 | end |
| 702 | o = add o ulong w |
| 703 | end |
| 704 | end |