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 / seed / kflow / kemu.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/kflow/kemu.om: kflow's host emulation stage. A SASS interpreter for the instruction |
| 3 | note subset the M1 kernels use, written in seeded Omega (program mode, Linux AArch64). |
| 4 | note |
| 5 | note Run: kemu ARCH BIN HARNESS SEED |
| 6 | note ARCH 80 or 121 (sm_80 / sm_121) |
| 7 | note BIN raw SASS text, 16 bytes per instruction, at most 65536 bytes |
| 8 | note HARNESS vadd (c[i] = a[i] + b[i] as u32, i < n = 1000, 8 CTAs of 128 threads; the |
| 9 | note vecadd ABI: a 0x380, b 0x388, c 0x390, n 0x398) or radd (x[i] += a[i] as f32, |
| 10 | note 2048 values, 1 CTA of 256 threads; the Qwen3 decode RES ADD ABI: x 0x380, |
| 11 | note a 0x388, five unused parameters through 0x3ac) or tmm1 (D = A x B, 16x16x16, A and |
| 12 | note B f16 row-major, D f32 row-major, 1 CTA of 32 threads; A 0x380, B 0x388, D 0x390) or |
| 13 | note tmmt (the tmm1 reference, but the arena holds B transposed: a negative control) or |
| 14 | note tsmx (softmax of each 32-value f32 row, 16 CTAs of 32 threads, row = CTA; X 0x380, |
| 15 | note Y 0x388) |
| 16 | note SEED decimal splitmix64 seed for the inputs |
| 17 | note |
| 18 | note The decoder reads fixed field positions of the Volta+ 128-bit encoding (opcode bits 0..11, guard |
| 19 | note 12..15, destination 16..23, first source 24..31, second source word 1, third source and modifiers |
| 20 | note word 2), not the asm2 tables, so a wrong table base that still decodes is a different program |
| 21 | note here. Each modifier the semantics below do not implement is refused (UNSUPPORTED), never |
| 22 | note ignored, except memory ordering and cache hints, which do not change single-thread results. |
| 23 | note Each CTA runs warp after warp. The 32 lanes of a warp step in lockstep with their own registers |
| 24 | note and predicates (uniform registers are per warp); lanes leave at EXIT; lanes that reach different |
| 25 | note pcs stop the run as DIVERGED (no reconvergence model). HMMA and SHFL act on the whole warp and |
| 26 | note are refused unless all 32 lanes are running with one guard value. Control words (stall, |
| 27 | note scoreboards) are not modelled: a schedule that is wrong on silicon can still MATCH here. |
| 28 | note The arena is 256 KiB filled with 0xa5. The expected arena is the input arena with the harness |
| 29 | note reference applied (vadd/radd exactly: u32 add; f32 add on the AArch64 FPU, round to nearest |
| 30 | note even, no flush, finite normal inputs). Every arena byte is compared, so a stray store fails as |
| 31 | note well as a wrong value; tmm1/tmmt/tsmx compare their output region as f32 against an f64 |
| 32 | note reference within a stated bound (fcmp) and every other byte exactly. |
| 33 | note Exit: 0 MATCH, 1 MISMATCH, 2 UNSUPPORTED (incomplete model, DIVERGED), 3 usage, unreadable |
| 34 | note input, a fault (address outside the arena) or a runaway thread. stdout: one line of key=value words. |
| 35 | om2 |
| 36 | text msg "kemu: usage: kemu ARCH BIN HARNESS SEED\nkemu: arch=\n harness=\n threads=\n insts=\n result=MATCH\n result=MISMATCH arena_offset=\n expected=\n got=\n result=UNSUPPORTED op=\n pc=\n result=FAULT address=\n result=RUNAWAY pc=\nkemu: bin unreadable, empty or not a multiple of 16 bytes\nkemu: unknown harness or arch\n w1=\n w2=\n result=DIVERGED pc=\n tol_used_ppm=\n" |
| 37 | text hn "vaddraddtmm1tmmttsmx" |
| 38 | global code uchar 65536 |
| 39 | global gm uchar 262144 |
| 40 | global ex uchar 262144 |
| 41 | global cb uchar 65536 |
| 42 | global rr uint 8192 |
| 43 | global ur uint 256 |
| 44 | global pr uchar 256 |
| 45 | global ls uchar 32 |
| 46 | global sh uint 32 |
| 47 | global ma uint 256 |
| 48 | global mb uint 128 |
| 49 | global mc uint 128 |
| 50 | global fr ulong 2 |
| 51 | global rf f64 2048 |
| 52 | global tl f64 2048 |
| 53 | global sv ulong 16 |
| 54 | global ab uchar 256 |
| 55 | global dig uchar 32 |
| 56 | global bad uint 4 |
| 57 | note sv: 0 arch, 1 ntid.x, 2 ctaid.x, 3 tid.x, 4 URZ index, 5 thread state (0 run, 1 exit, |
| 58 | note 2 unsupported, 3 fault, 4 runaway, 5 diverged), 6 instructions, 7 rng state, 8 code bytes, |
| 59 | note 9 fault address, 10 pc of the stopping instruction, 11 CTAs, 12 lane, 13 lane register base, |
| 60 | note 14 lane predicate base, 15 warp. bad: w0 w1 w2 of an unsupported instruction. ls: lane state. |
| 61 | note fr: arena offset and count of the f32 region compared within tolerance (rf value, tl bound). |
| 62 | const GB ulong 0x100000000000 |
| 63 | note ---- output ---- |
| 64 | fn say id ulong to void |
| 65 | k = ulong 1 |
| 66 | s = ulong 0 |
| 67 | for i ulong len msg |
| 68 | if eq get msg i 10 |
| 69 | if eq k id |
| 70 | r = write 1 msg s sub i s |
| 71 | ret |
| 72 | end |
| 73 | k = add k 1 |
| 74 | s = add i 1 |
| 75 | end |
| 76 | end |
| 77 | exit 99 |
| 78 | end |
| 79 | fn newline to void |
| 80 | put dig 0 10 |
| 81 | r = write 1 dig 0 1 |
| 82 | end |
| 83 | fn num v ulong to void |
| 84 | x = v |
| 85 | n = ulong 0 |
| 86 | while ge x 10 bound 64 |
| 87 | put dig n uchar add 48 rem x 10 |
| 88 | x = div x 10 |
| 89 | n = add n 1 |
| 90 | end |
| 91 | put dig n uchar add 48 x |
| 92 | n = add n 1 |
| 93 | for i ulong n |
| 94 | r = write 1 dig sub sub n i 1 1 |
| 95 | end |
| 96 | end |
| 97 | fn hexn v ulong to void |
| 98 | put dig 0 48 |
| 99 | put dig 1 120 |
| 100 | r = write 1 dig 0 2 |
| 101 | s = ulong 60 |
| 102 | z = true |
| 103 | for i ulong 16 |
| 104 | d = and shr v s 15 |
| 105 | if or ne d 0 or not z eq s 0 |
| 106 | z = false |
| 107 | c = uchar add 48 d |
| 108 | if ge d 10 |
| 109 | c = uchar add 87 d |
| 110 | end |
| 111 | put dig 0 c |
| 112 | r = write 1 dig 0 1 |
| 113 | end |
| 114 | if ne s 0 |
| 115 | s = sub s 4 |
| 116 | end |
| 117 | end |
| 118 | end |
| 119 | note ---- random inputs (splitmix64) ---- |
| 120 | fn rnd to ulong |
| 121 | s = add get sv 7 11400714819323198485 |
| 122 | put sv 7 s |
| 123 | z = mul xor s shr s 30 13787848793156543929 |
| 124 | z = mul xor z shr z 27 10723151780598845931 |
| 125 | ret xor z shr z 31 |
| 126 | end |
| 127 | note a finite normal f32: exponent field 100..163, any sign and fraction |
| 128 | fn rndf to uint |
| 129 | v = rnd |
| 130 | e = add and shr v 23 63 100 |
| 131 | ret uint or or and v 0x807fffff shl e 23 0 |
| 132 | end |
| 133 | note ---- registers (rr and pr hold 32 lanes; sv 13 and sv 14 select the current lane) ---- |
| 134 | fn rg r uint to uint |
| 135 | if eq r 255 |
| 136 | ret 0 |
| 137 | end |
| 138 | ret get rr add ulong r get sv 13 |
| 139 | end |
| 140 | fn wr r uint v uint to void |
| 141 | if ne r 255 |
| 142 | put rr add ulong r get sv 13 v |
| 143 | end |
| 144 | end |
| 145 | fn ug r uint to uint |
| 146 | if eq ulong r get sv 4 |
| 147 | ret 0 |
| 148 | end |
| 149 | ret get ur r |
| 150 | end |
| 151 | fn wu r uint v uint to void |
| 152 | if ne ulong r get sv 4 |
| 153 | put ur r v |
| 154 | end |
| 155 | end |
| 156 | fn pg g uint to bool |
| 157 | i = and g 7 |
| 158 | v = true |
| 159 | if ne i 7 |
| 160 | v = ne get pr add ulong i get sv 14 0 |
| 161 | end |
| 162 | if ne and g 8 0 |
| 163 | ret not v |
| 164 | end |
| 165 | ret v |
| 166 | end |
| 167 | note ---- memory ---- |
| 168 | fn cw pc ulong k ulong to uint |
| 169 | o = add pc mul k 4 |
| 170 | v = uint get code o |
| 171 | v = or v shl uint get code add o 1 8 |
| 172 | v = or v shl uint get code add o 2 16 |
| 173 | ret or v shl uint get code add o 3 24 |
| 174 | end |
| 175 | fn cbw o ulong to uint |
| 176 | v = uint get cb o |
| 177 | v = or v shl uint get cb add o 1 8 |
| 178 | v = or v shl uint get cb add o 2 16 |
| 179 | ret or v shl uint get cb add o 3 24 |
| 180 | end |
| 181 | fn cbput o ulong v uint to void |
| 182 | put cb o uchar and v 255 |
| 183 | put cb add o 1 uchar and shr v 8 255 |
| 184 | put cb add o 2 uchar and shr v 16 255 |
| 185 | put cb add o 3 uchar shr v 24 |
| 186 | end |
| 187 | fn gmw o ulong v uint to void |
| 188 | put gm o uchar and v 255 |
| 189 | put gm add o 1 uchar and shr v 8 255 |
| 190 | put gm add o 2 uchar and shr v 16 255 |
| 191 | put gm add o 3 uchar shr v 24 |
| 192 | end |
| 193 | fn exw o ulong v uint to void |
| 194 | put ex o uchar and v 255 |
| 195 | put ex add o 1 uchar and shr v 8 255 |
| 196 | put ex add o 2 uchar and shr v 16 255 |
| 197 | put ex add o 3 uchar shr v 24 |
| 198 | end |
| 199 | fn gmr o ulong to uint |
| 200 | v = uint get gm o |
| 201 | v = or v shl uint get gm add o 1 8 |
| 202 | v = or v shl uint get gm add o 2 16 |
| 203 | ret or v shl uint get gm add o 3 24 |
| 204 | end |
| 205 | fn exr o ulong to uint |
| 206 | v = uint get ex o |
| 207 | v = or v shl uint get ex add o 1 8 |
| 208 | v = or v shl uint get ex add o 2 16 |
| 209 | ret or v shl uint get ex add o 3 24 |
| 210 | end |
| 211 | note arena offset of n bytes at address a; sets the fault state when outside |
| 212 | fn madr a ulong n ulong to ulong |
| 213 | if or lt a GB gt add sub a GB n len gm |
| 214 | put sv 9 a |
| 215 | put sv 5 3 |
| 216 | ret 0 |
| 217 | end |
| 218 | ret sub a GB |
| 219 | end |
| 220 | note a 64-bit register pair (RZ pair reads 0) |
| 221 | fn rp r uint to ulong |
| 222 | if eq r 255 |
| 223 | ret 0 |
| 224 | end |
| 225 | ret or ulong rg r shl ulong rg add r 1 32 |
| 226 | end |
| 227 | fn wp r uint v ulong to void |
| 228 | wr r uint and v 0xffffffff |
| 229 | if ne r 255 |
| 230 | wr add r 1 uint shr v 32 |
| 231 | end |
| 232 | end |
| 233 | fn unsup w0 uint w1 uint w2 uint to void |
| 234 | put bad 0 w0 |
| 235 | put bad 1 w1 |
| 236 | put bad 2 w2 |
| 237 | put sv 5 2 |
| 238 | end |
| 239 | note ---- second source by form (bits 9..11): 1 register, 2/4 number, 6/7 uniform register ---- |
| 240 | fn bsrc f uint w1 uint to uint |
| 241 | if eq f 1 |
| 242 | ret rg and w1 255 |
| 243 | end |
| 244 | if or eq f 2 eq f 4 |
| 245 | ret w1 |
| 246 | end |
| 247 | u = and w1 255 |
| 248 | if eq get sv 0 80 |
| 249 | u = and w1 63 |
| 250 | end |
| 251 | ret ug u |
| 252 | end |
| 253 | fn formok f uint to bool |
| 254 | ret or or eq f 1 or eq f 2 eq f 4 or eq f 6 eq f 7 |
| 255 | end |
| 256 | fn lut3 a uint b uint c uint l uint to uint |
| 257 | r = uint 0 |
| 258 | for k uint 8 |
| 259 | if ne and shr l k 1 0 |
| 260 | t = not uint 0 |
| 261 | if ne and k 4 0 |
| 262 | t = and t a |
| 263 | else |
| 264 | t = and t not a |
| 265 | end |
| 266 | if ne and k 2 0 |
| 267 | t = and t b |
| 268 | else |
| 269 | t = and t not b |
| 270 | end |
| 271 | if ne and k 1 0 |
| 272 | t = and t c |
| 273 | else |
| 274 | t = and t not c |
| 275 | end |
| 276 | r = or r t |
| 277 | end |
| 278 | end |
| 279 | ret r |
| 280 | end |
| 281 | fn icmp x0 uint y0 uint cm uint sg uint to bool |
| 282 | x = x0 |
| 283 | y = y0 |
| 284 | if ne sg 0 |
| 285 | x = xor x 0x80000000 |
| 286 | y = xor y 0x80000000 |
| 287 | end |
| 288 | if eq cm 1 |
| 289 | ret lt x y |
| 290 | end |
| 291 | if eq cm 2 |
| 292 | ret eq x y |
| 293 | end |
| 294 | if eq cm 3 |
| 295 | ret le x y |
| 296 | end |
| 297 | if eq cm 4 |
| 298 | ret gt x y |
| 299 | end |
| 300 | if eq cm 5 |
| 301 | ret ne x y |
| 302 | end |
| 303 | if eq cm 6 |
| 304 | ret ge x y |
| 305 | end |
| 306 | ret eq cm 7 |
| 307 | end |
| 308 | fn isalu op uint to bool |
| 309 | if or or eq op 0x24 eq op 0x25 or eq op 0x0c eq op 0x12 |
| 310 | ret true |
| 311 | end |
| 312 | ret or or eq op 0x19 eq op 0x10 or eq op 0x21 eq op 0x02 |
| 313 | end |
| 314 | note ---- integer and float ALU; false when the base opcode is not one of these ---- |
| 315 | fn alu w0 uint w1 uint w2 uint to bool |
| 316 | op = and w0 0x1ff |
| 317 | f = shr and w0 0xfff 9 |
| 318 | d = and shr w0 16 255 |
| 319 | a = rg and shr w0 24 255 |
| 320 | if not isalu op |
| 321 | ret false |
| 322 | end |
| 323 | if not formok f |
| 324 | unsup w0 w1 w2 |
| 325 | ret true |
| 326 | end |
| 327 | b = bsrc f w1 |
| 328 | c = rg and w2 255 |
| 329 | if eq op 0x02 |
| 330 | wr d b |
| 331 | ret true |
| 332 | end |
| 333 | if eq op 0x24 |
| 334 | wr d add mul a b c |
| 335 | ret true |
| 336 | end |
| 337 | if eq op 0x25 |
| 338 | imadw w0 w2 a b |
| 339 | ret true |
| 340 | end |
| 341 | if eq op 0x10 |
| 342 | if or not modok f w1 w2 0x80000000 0x900 ne and shr w2 17 63 63 |
| 343 | unsup w0 w1 w2 |
| 344 | ret true |
| 345 | end |
| 346 | wr d add add a b c |
| 347 | ret true |
| 348 | end |
| 349 | if eq op 0x12 |
| 350 | wr d lut3 a b c and shr w2 8 255 |
| 351 | ret true |
| 352 | end |
| 353 | if eq op 0x19 |
| 354 | shf w0 w1 w2 b |
| 355 | ret true |
| 356 | end |
| 357 | if eq op 0x0c |
| 358 | isetp w0 w1 w2 a b |
| 359 | ret true |
| 360 | end |
| 361 | if not modok f w1 w2 0x40000000 0x1e300 |
| 362 | unsup w0 w1 w2 |
| 363 | ret true |
| 364 | end |
| 365 | if and eq f 1 ne and w1 0x80000000 0 |
| 366 | b = xor b 0x80000000 |
| 367 | end |
| 368 | wr d bitcast uint add bitcast f32 a bitcast f32 b |
| 369 | ret true |
| 370 | end |
| 371 | note no word-2 modifier bit in m2, and (register form) no word-1 negate/abs bit in m1 |
| 372 | fn modok f uint w1 uint w2 uint m1 uint m2 uint to bool |
| 373 | if ne and w2 m2 0 |
| 374 | ret false |
| 375 | end |
| 376 | if eq f 1 |
| 377 | ret eq and w1 m1 0 |
| 378 | end |
| 379 | ret true |
| 380 | end |
| 381 | fn imadw w0 uint w2 uint a uint b uint to void |
| 382 | d = and shr w0 16 255 |
| 383 | x = ulong a |
| 384 | y = ulong b |
| 385 | if ne and w2 0x200 0 |
| 386 | if ne and a 0x80000000 0 |
| 387 | x = or x 0xffffffff00000000 |
| 388 | end |
| 389 | if ne and b 0x80000000 0 |
| 390 | y = or y 0xffffffff00000000 |
| 391 | end |
| 392 | end |
| 393 | wp d add mul x y rp and w2 255 |
| 394 | end |
| 395 | note SHF: only the right funnel shift giving the high word of a U32 pair (type 3, bit 12, bit 16) |
| 396 | fn shf w0 uint w1 uint w2 uint b uint to void |
| 397 | if ne and w2 0x11600 0x11600 |
| 398 | unsup w0 w1 w2 |
| 399 | ret |
| 400 | end |
| 401 | s = and b 63 |
| 402 | if ne and w2 0x800 0 |
| 403 | s = and b 31 |
| 404 | end |
| 405 | if gt s 32 |
| 406 | s = uint 32 |
| 407 | end |
| 408 | v = uint 0 |
| 409 | if lt s 32 |
| 410 | v = shr rg and w2 255 s |
| 411 | end |
| 412 | wr and shr w0 16 255 v |
| 413 | end |
| 414 | note ISETP: no .EX (bit 8), set operation AND (bits 10..11 = 0) |
| 415 | fn isetp w0 uint w1 uint w2 uint a uint b uint to void |
| 416 | if ne and w2 0xd00 0 |
| 417 | unsup w0 w1 w2 |
| 418 | ret |
| 419 | end |
| 420 | r = icmp a b and shr w2 12 7 and shr w2 9 1 |
| 421 | r = and r pg and shr w2 23 15 |
| 422 | p = and shr w2 17 7 |
| 423 | if ne p 7 |
| 424 | q = add ulong p get sv 14 |
| 425 | put pr q uchar 0 |
| 426 | if r |
| 427 | put pr q uchar 1 |
| 428 | end |
| 429 | end |
| 430 | end |
| 431 | note S2R: TID.X 0x21, CTAID.X 0x25; Y and Z (0x22 0x23 0x26 0x27) are 0 in a 1-D launch |
| 432 | fn s2r w0 uint w1 uint w2 uint to void |
| 433 | sr = and shr w2 8 255 |
| 434 | v = uint get sv 3 |
| 435 | if eq sr 0x25 |
| 436 | v = uint get sv 2 |
| 437 | end |
| 438 | if ne sr 0x21 |
| 439 | if ne sr 0x25 |
| 440 | if lt sr 0x22 |
| 441 | unsup w0 w1 w2 |
| 442 | ret |
| 443 | end |
| 444 | if and gt sr 0x23 lt sr 0x26 |
| 445 | unsup w0 w1 w2 |
| 446 | ret |
| 447 | end |
| 448 | if gt sr 0x27 |
| 449 | unsup w0 w1 w2 |
| 450 | ret |
| 451 | end |
| 452 | v = uint 0 |
| 453 | end |
| 454 | end |
| 455 | wr and shr w0 16 255 v |
| 456 | end |
| 457 | note ---- constant bank, global memory, control ---- |
| 458 | fn memop pc ulong w0 uint w1 uint w2 uint to bool |
| 459 | op = and w0 0xfff |
| 460 | d = and shr w0 16 255 |
| 461 | sz = and shr w2 9 7 |
| 462 | if eq op 0x918 |
| 463 | ret true |
| 464 | end |
| 465 | if eq op 0x919 |
| 466 | s2r w0 w1 w2 |
| 467 | ret true |
| 468 | end |
| 469 | if eq op 0xb82 |
| 470 | if ne shr w0 24 255 |
| 471 | unsup w0 w1 w2 |
| 472 | ret true |
| 473 | end |
| 474 | end |
| 475 | if eq op 0xb82 |
| 476 | if ne and w1 0x7c00000 0 |
| 477 | unsup w0 w1 w2 |
| 478 | ret true |
| 479 | end |
| 480 | if not ldc 0 d and shr w1 6 0xffff sz |
| 481 | unsup w0 w1 w2 |
| 482 | end |
| 483 | ret true |
| 484 | end |
| 485 | if and eq op 0x7ac eq get sv 0 121 |
| 486 | if not ldc 1 d mul and shr w1 8 0xffff 8 sz |
| 487 | unsup w0 w1 w2 |
| 488 | end |
| 489 | ret true |
| 490 | end |
| 491 | if and eq op 0xab9 eq get sv 0 80 |
| 492 | if not ldc 1 and d 63 and shr w1 6 0xffff sz |
| 493 | unsup w0 w1 w2 |
| 494 | end |
| 495 | ret true |
| 496 | end |
| 497 | if or eq op 0x981 eq op 0x986 |
| 498 | gmem w0 w1 w2 |
| 499 | ret true |
| 500 | end |
| 501 | if eq op 0x94d |
| 502 | put sv 5 1 |
| 503 | ret true |
| 504 | end |
| 505 | if eq op 0x947 |
| 506 | put sv 10 pc |
| 507 | put sv 5 4 |
| 508 | ret true |
| 509 | end |
| 510 | ret false |
| 511 | end |
| 512 | fn ldc un uint d uint o uint sz uint to bool |
| 513 | if or and ne sz 4 ne sz 5 gt o 0xfff8 |
| 514 | ret false |
| 515 | end |
| 516 | v = cbw ulong o |
| 517 | if eq un 1 |
| 518 | wu d v |
| 519 | if eq sz 5 |
| 520 | wu add d 1 cbw add ulong o 4 |
| 521 | end |
| 522 | else |
| 523 | wr d v |
| 524 | if eq sz 5 |
| 525 | wr add d 1 cbw add ulong o 4 |
| 526 | end |
| 527 | end |
| 528 | ret true |
| 529 | end |
| 530 | note LDG/STG with a 64-bit address register (word 2 bit 8). sm_80 adds the uniform register in its |
| 531 | note uniform-address field; sm_121's uniform field is the memory descriptor and adds nothing. |
| 532 | note Size (word 2 bits 9..11): 0 U8, 1 S8, 2 U16, 3 S16 (loads only), 4 32, 5 64, 6 128 bits. |
| 533 | fn gmem w0 uint w1 uint w2 uint to void |
| 534 | sz = and shr w2 9 7 |
| 535 | st = eq and w0 0xfff 0x986 |
| 536 | if or or eq and w2 0x100 0 gt sz 6 and st lt sz 4 |
| 537 | unsup w0 w1 w2 |
| 538 | ret |
| 539 | end |
| 540 | off = ulong shr w1 8 |
| 541 | if ge off 0x800000 |
| 542 | off = or off 0xffffffffff000000 |
| 543 | end |
| 544 | a = add rp and shr w0 24 255 off |
| 545 | if eq get sv 0 80 |
| 546 | u = and w1 63 |
| 547 | if st |
| 548 | u = and w2 63 |
| 549 | end |
| 550 | a = add a ulong ug u |
| 551 | end |
| 552 | nb = shl ulong 1 shr sz 1 |
| 553 | if ge sz 4 |
| 554 | nb = shl ulong 4 sub sz 4 |
| 555 | end |
| 556 | o = madr a nb |
| 557 | if ne get sv 5 0 |
| 558 | ret |
| 559 | end |
| 560 | d = and shr w0 16 255 |
| 561 | if st |
| 562 | d = and w1 255 |
| 563 | end |
| 564 | if lt sz 4 |
| 565 | gsmall o d sz |
| 566 | ret |
| 567 | end |
| 568 | xfer st o d shr nb 2 |
| 569 | end |
| 570 | fn gsmall o ulong d uint sz uint to void |
| 571 | v = uint get gm o |
| 572 | if lt sz 2 |
| 573 | if and eq sz 1 ne and v 0x80 0 |
| 574 | v = or v 0xffffff00 |
| 575 | end |
| 576 | else |
| 577 | v = or v shl uint get gm add o 1 8 |
| 578 | if and eq sz 3 ne and v 0x8000 0 |
| 579 | v = or v 0xffff0000 |
| 580 | end |
| 581 | end |
| 582 | wr d v |
| 583 | end |
| 584 | fn xfer st bool o ulong d uint n ulong to void |
| 585 | for i ulong n |
| 586 | p = add o mul i 4 |
| 587 | r = add d uint i |
| 588 | if st |
| 589 | gmw p rg r |
| 590 | else |
| 591 | wr r gmr p |
| 592 | end |
| 593 | end |
| 594 | end |
| 595 | note ---- FMUL, FMNMX, MUFU.EX2/RCP (register forms); false when the opcode is not one of these ---- |
| 596 | fn fpu w0 uint w1 uint w2 uint to bool |
| 597 | op = and w0 0xfff |
| 598 | if and and ne op 0x220 ne op 0x209 ne op 0x308 |
| 599 | ret false |
| 600 | end |
| 601 | d = and shr w0 16 255 |
| 602 | a = bitcast f32 rg and shr w0 24 255 |
| 603 | b = bitcast f32 rg and w1 255 |
| 604 | if ne and w1 0xffffff00 0 |
| 605 | unsup w0 w1 w2 |
| 606 | ret true |
| 607 | end |
| 608 | if eq op 0x308 |
| 609 | mufu w0 w1 w2 b |
| 610 | ret true |
| 611 | end |
| 612 | if eq op 0x220 |
| 613 | if ne w2 0x00400000 |
| 614 | unsup w0 w1 w2 |
| 615 | ret true |
| 616 | end |
| 617 | wr d bitcast uint mul a b |
| 618 | ret true |
| 619 | end |
| 620 | if ne and w2 0xf87fffff 0 |
| 621 | unsup w0 w1 w2 |
| 622 | ret true |
| 623 | end |
| 624 | r = fmax a b |
| 625 | if pg and shr w2 23 15 |
| 626 | r = fmin a b |
| 627 | end |
| 628 | wr d bitcast uint r |
| 629 | ret true |
| 630 | end |
| 631 | note MUFU: EX2 (word 2 0x800) and RCP (0x1000) only; source register in word 1 |
| 632 | fn mufu w0 uint w1 uint w2 uint x f32 to void |
| 633 | d = and shr w0 16 255 |
| 634 | if eq w2 0x800 |
| 635 | wr d bitcast uint ex2m x |
| 636 | ret |
| 637 | end |
| 638 | if eq w2 0x1000 |
| 639 | wr d bitcast uint div 1.0f32 x |
| 640 | ret |
| 641 | end |
| 642 | unsup w0 w1 w2 |
| 643 | end |
| 644 | note model of MUFU.EX2: 2^x from an f64 series, rounded once to f32; below 2^-126 flushes to 0, |
| 645 | note from 2^128 up gives +inf (the hardware is within 2 ulp of this; the harness bound allows it) |
| 646 | fn ex2m x f32 to f32 |
| 647 | y = f64 x |
| 648 | if lt y -126.0f64 |
| 649 | ret 0.0f32 |
| 650 | end |
| 651 | if ge y 128.0f64 |
| 652 | ret bitcast f32 uint 0x7f800000 |
| 653 | end |
| 654 | n = long y |
| 655 | if gt f64 n y |
| 656 | n = sub n 1 |
| 657 | end |
| 658 | f = mul sub y f64 n 0.6931471805599453094f64 |
| 659 | s = 1.0f64 |
| 660 | t = 1.0f64 |
| 661 | for i ulong 24 |
| 662 | t = div mul t f f64 add i 1 |
| 663 | s = add s t |
| 664 | end |
| 665 | p = bitcast f64 shl ulong add n 1023 52 |
| 666 | ret f32 mul s p |
| 667 | end |
| 668 | note ---- warp-wide HMMA and SHFL ---- |
| 669 | note SHFL.BFLY / SHFL.DOWN with immediate lane and clamp 0x1f over a full warp, predicate out PT |
| 670 | fn shfl w0 uint w1 uint w2 uint to void |
| 671 | md = and shr w1 26 3 |
| 672 | k = ulong and shr w1 21 31 |
| 673 | x = or ne and w1 0xf01fe0ff 0 ne and w1 0x1f00 0x1f00 |
| 674 | if or x or lt md 2 ne w2 0x000e0000 |
| 675 | unsup w0 w1 w2 |
| 676 | ret |
| 677 | end |
| 678 | a = and shr w0 24 255 |
| 679 | for l ulong 32 |
| 680 | lane l |
| 681 | put sh l rg a |
| 682 | end |
| 683 | d = and shr w0 16 255 |
| 684 | for l ulong 32 |
| 685 | s = xor l k |
| 686 | if eq md 2 |
| 687 | s = add l k |
| 688 | if gt s 31 |
| 689 | s = l |
| 690 | end |
| 691 | end |
| 692 | lane l |
| 693 | wr d get sh s |
| 694 | end |
| 695 | end |
| 696 | note fragment index of accumulator register i of lane l: row g (+8 for i 2,3), column 2t + (i and 1) |
| 697 | fn cidx l ulong i ulong to ulong |
| 698 | r = add shr l 2 mul shr i 1 8 |
| 699 | n = add mul and l 3 2 and i 1 |
| 700 | ret add mul r 8 n |
| 701 | end |
| 702 | note A registers of lane l (g = l/4, t = l%4): i holds row g (+8 if i odd), columns 2t, 2t+1 (+8 if |
| 703 | note i >= 2), low half first; C likewise by cidx (RZ reads 0) |
| 704 | fn hga l ulong a uint c uint to void |
| 705 | g = shr l 2 |
| 706 | t = and l 3 |
| 707 | for i ulong 4 |
| 708 | v = rg add a uint i |
| 709 | r = add g mul and i 1 8 |
| 710 | n = add mul t 2 mul shr i 1 8 |
| 711 | o = add mul r 16 n |
| 712 | put ma o and v 0xffff |
| 713 | put ma add o 1 shr v 16 |
| 714 | w = uint 0 |
| 715 | if ne c 255 |
| 716 | w = rg add c uint i |
| 717 | end |
| 718 | put mc cidx l i w |
| 719 | end |
| 720 | end |
| 721 | note B registers of lane l: i holds rows k = 2t, 2t+1 (+8 if i = 1) of column g, low half first |
| 722 | fn hgb l ulong b uint to void |
| 723 | g = shr l 2 |
| 724 | t = and l 3 |
| 725 | for i ulong 2 |
| 726 | v = rg add b uint i |
| 727 | k = add mul t 2 mul i 8 |
| 728 | put mb add mul k 8 g and v 0xffff |
| 729 | k = add k 1 |
| 730 | put mb add mul k 8 g shr v 16 |
| 731 | end |
| 732 | end |
| 733 | note model: f16/bf16 to f32 by moving bits, each product exact in f32, accumulated in f32 in k order |
| 734 | fn h2f h uint bf bool to f32 |
| 735 | if bf |
| 736 | ret bitcast f32 shl h 16 |
| 737 | end |
| 738 | s = shl and h 0x8000 16 |
| 739 | e = and shr h 10 31 |
| 740 | f = and h 1023 |
| 741 | if eq e 31 |
| 742 | ret bitcast f32 or or s 0x7f800000 shl f 13 |
| 743 | end |
| 744 | if eq e 0 |
| 745 | x = mul f32 f 5.9604644775390625e-8f32 |
| 746 | if ne s 0 |
| 747 | ret neg x |
| 748 | end |
| 749 | ret x |
| 750 | end |
| 751 | e = shl add e 112 23 |
| 752 | ret bitcast f32 or or s e shl f 13 |
| 753 | end |
| 754 | fn hcomp bf bool to void |
| 755 | for r ulong 16 |
| 756 | for n ulong 8 |
| 757 | acc = bitcast f32 get mc add mul r 8 n |
| 758 | for k ulong 16 |
| 759 | x = h2f get ma add mul r 16 k bf |
| 760 | y = h2f get mb add mul k 8 n bf |
| 761 | acc = add acc mul x y |
| 762 | end |
| 763 | put mc add mul r 8 n bitcast uint acc |
| 764 | end |
| 765 | end |
| 766 | end |
| 767 | note HMMA.16816.F32 (word 2 0x1800 F16 or 0x41800 BF16 inputs): D = A x B + C, m16n8k16 |
| 768 | fn hmma w0 uint w1 uint w2 uint to void |
| 769 | if not hmmok w0 w1 w2 |
| 770 | unsup w0 w1 w2 |
| 771 | ret |
| 772 | end |
| 773 | hmmx w0 w1 w2 |
| 774 | end |
| 775 | note register pairs/quads aligned, word 1 only the B register, F32 accumulate of F16 or BF16 |
| 776 | fn hmmok w0 uint w1 uint w2 uint to bool |
| 777 | m = and w2 0xffffff00 |
| 778 | if or ne and w1 0xffffff00 0 and ne m 0x1800 ne m 0x41800 |
| 779 | ret false |
| 780 | end |
| 781 | if or ne and shr w0 24 3 0 ne and w1 1 0 |
| 782 | ret false |
| 783 | end |
| 784 | if ne and shr w0 16 3 0 |
| 785 | ret false |
| 786 | end |
| 787 | c = and w2 255 |
| 788 | ret or eq c 255 eq and c 3 0 |
| 789 | end |
| 790 | fn hmmx w0 uint w1 uint w2 uint to void |
| 791 | m = and w2 0xffffff00 |
| 792 | c = and w2 255 |
| 793 | a = and shr w0 24 255 |
| 794 | b = and w1 255 |
| 795 | d = and shr w0 16 255 |
| 796 | for l ulong 32 |
| 797 | lane l |
| 798 | hga l a c |
| 799 | hgb l b |
| 800 | end |
| 801 | hcomp eq m 0x41800 |
| 802 | for l ulong 32 |
| 803 | lane l |
| 804 | for i ulong 4 |
| 805 | wr add d uint i get mc cidx l i |
| 806 | end |
| 807 | end |
| 808 | end |
| 809 | note one instruction at pc for the current lane; gives the next pc |
| 810 | fn step pc ulong to ulong |
| 811 | w0 = cw pc 0 |
| 812 | w1 = cw pc 1 |
| 813 | w2 = cw pc 2 |
| 814 | put sv 6 add get sv 6 1 |
| 815 | if not pg and shr w0 12 15 |
| 816 | ret add pc 16 |
| 817 | end |
| 818 | if alu w0 w1 w2 |
| 819 | ret add pc 16 |
| 820 | end |
| 821 | if fpu w0 w1 w2 |
| 822 | ret add pc 16 |
| 823 | end |
| 824 | if memop pc w0 w1 w2 |
| 825 | ret add pc 16 |
| 826 | end |
| 827 | unsup w0 w1 w2 |
| 828 | ret pc |
| 829 | end |
| 830 | note ---- one warp: 32 lanes in lockstep ---- |
| 831 | const ALL ulong 0xffffffffffffffff |
| 832 | fn lane l ulong to void |
| 833 | put sv 12 l |
| 834 | put sv 13 mul l 256 |
| 835 | put sv 14 mul l 8 |
| 836 | put sv 3 add mul get sv 15 32 l |
| 837 | end |
| 838 | fn wclear to void |
| 839 | for i ulong len rr |
| 840 | put rr i 0 |
| 841 | end |
| 842 | for i ulong 256 |
| 843 | put ur i 0 |
| 844 | put pr i 0 |
| 845 | end |
| 846 | for l ulong 32 |
| 847 | put pr add mul l 8 7 1 |
| 848 | put ls l 0 |
| 849 | end |
| 850 | end |
| 851 | fn nrun to ulong |
| 852 | n = ulong 0 |
| 853 | for l ulong 32 |
| 854 | if eq get ls l 0 |
| 855 | n = add n 1 |
| 856 | end |
| 857 | end |
| 858 | ret n |
| 859 | end |
| 860 | note HMMA and SHFL: false when the instruction at pc is neither; refused unless the warp is full |
| 861 | note and its guard is uniform |
| 862 | fn coll pc ulong to bool |
| 863 | w0 = cw pc 0 |
| 864 | op = and w0 0xfff |
| 865 | if and ne op 0x23c ne op 0xf89 |
| 866 | ret false |
| 867 | end |
| 868 | w1 = cw pc 1 |
| 869 | w2 = cw pc 2 |
| 870 | put sv 6 add get sv 6 nrun |
| 871 | if ne nrun 32 |
| 872 | unsup w0 w1 w2 |
| 873 | ret true |
| 874 | end |
| 875 | g = and shr w0 12 15 |
| 876 | lane 0 |
| 877 | v = pg g |
| 878 | for l ulong 32 |
| 879 | lane l |
| 880 | x = pg g |
| 881 | if or and x not v and v not x |
| 882 | unsup w0 w1 w2 |
| 883 | ret true |
| 884 | end |
| 885 | end |
| 886 | if not v |
| 887 | ret true |
| 888 | end |
| 889 | if eq op 0x23c |
| 890 | hmma w0 w1 w2 |
| 891 | else |
| 892 | shfl w0 w1 w2 |
| 893 | end |
| 894 | ret true |
| 895 | end |
| 896 | note every running lane executes pc; gives their common next pc, ALL when none is left running, |
| 897 | note sub ALL 1 when the warp stops (state in sv 5) |
| 898 | fn wstep pc ulong to ulong |
| 899 | q = ALL |
| 900 | for l ulong 32 |
| 901 | if eq get ls l 0 |
| 902 | lane l |
| 903 | put sv 5 0 |
| 904 | x = step pc |
| 905 | s = get sv 5 |
| 906 | if gt s 1 |
| 907 | ret sub ALL 1 |
| 908 | end |
| 909 | put ls l uchar s |
| 910 | if eq s 0 |
| 911 | if eq q ALL |
| 912 | q = x |
| 913 | end |
| 914 | if ne q x |
| 915 | put sv 5 5 |
| 916 | ret sub ALL 1 |
| 917 | end |
| 918 | end |
| 919 | end |
| 920 | end |
| 921 | put sv 5 0 |
| 922 | ret q |
| 923 | end |
| 924 | fn warp to void |
| 925 | wclear |
| 926 | pc = ulong 0 |
| 927 | for k ulong 200000 |
| 928 | if eq nrun 0 |
| 929 | put sv 5 1 |
| 930 | ret |
| 931 | end |
| 932 | put sv 10 pc |
| 933 | put sv 5 0 |
| 934 | if ge pc get sv 8 |
| 935 | put sv 5 4 |
| 936 | ret |
| 937 | end |
| 938 | q = add pc 16 |
| 939 | if not coll pc |
| 940 | q = wstep pc |
| 941 | end |
| 942 | if ne get sv 5 0 |
| 943 | ret |
| 944 | end |
| 945 | pc = q |
| 946 | end |
| 947 | put sv 5 4 |
| 948 | end |
| 949 | note ---- harnesses ---- |
| 950 | fn arena to void |
| 951 | for i ulong len gm |
| 952 | put gm i 165 |
| 953 | end |
| 954 | for i ulong len cb |
| 955 | put cb i 0 |
| 956 | end |
| 957 | end |
| 958 | fn cbp o ulong v ulong to void |
| 959 | cbput o uint and v 0xffffffff |
| 960 | cbput add o 4 uint shr v 32 |
| 961 | end |
| 962 | fn hvadd to void |
| 963 | cbput 0x360 128 |
| 964 | cbput 0x364 1 |
| 965 | cbput 0x368 1 |
| 966 | cbput 0x37c 0xfffc |
| 967 | cbp 0x380 add GB 0x10000 |
| 968 | cbp 0x388 add GB 0x20000 |
| 969 | cbp 0x390 add GB 0x30000 |
| 970 | cbput 0x398 1000 |
| 971 | for i ulong 1000 |
| 972 | gmw add 0x10000 mul i 4 uint and rnd 0xffffffff |
| 973 | gmw add 0x20000 mul i 4 uint and rnd 0xffffffff |
| 974 | end |
| 975 | for i ulong len gm |
| 976 | put ex i get gm i |
| 977 | end |
| 978 | for i ulong 1000 |
| 979 | exw add 0x30000 mul i 4 add gmr add 0x10000 mul i 4 gmr add 0x20000 mul i 4 |
| 980 | end |
| 981 | put sv 1 128 |
| 982 | put sv 11 8 |
| 983 | end |
| 984 | fn hradd to void |
| 985 | cbput 0x360 256 |
| 986 | cbput 0x364 1 |
| 987 | cbput 0x368 1 |
| 988 | cbp 0x380 add GB 0x10000 |
| 989 | cbp 0x388 add GB 0x20000 |
| 990 | for i ulong 2048 |
| 991 | gmw add 0x10000 mul i 4 rndf |
| 992 | gmw add 0x20000 mul i 4 rndf |
| 993 | end |
| 994 | for i ulong len gm |
| 995 | put ex i get gm i |
| 996 | end |
| 997 | for i ulong 2048 |
| 998 | x = bitcast f32 gmr add 0x10000 mul i 4 |
| 999 | y = bitcast f32 gmr add 0x20000 mul i 4 |
| 1000 | exw add 0x10000 mul i 4 bitcast uint add x y |
| 1001 | end |
| 1002 | put sv 1 256 |
| 1003 | put sv 11 1 |
| 1004 | end |
| 1005 | note ---- tile harnesses: an f64 reference by a different route, and a stated bound ---- |
| 1006 | note a normal f16: exponent field 10..20 (2^-5..2^5), any sign and fraction |
| 1007 | fn rnh to uint |
| 1008 | v = rnd |
| 1009 | e = add rem and shr v 16 255 11 10 |
| 1010 | ret uint or and v 0x83ff shl e 10 |
| 1011 | end |
| 1012 | fn gmh o ulong v uint to void |
| 1013 | put gm o uchar and v 255 |
| 1014 | put gm add o 1 uchar shr v 8 |
| 1015 | end |
| 1016 | fn gmhr o ulong to uint |
| 1017 | ret or uint get gm o shl uint get gm add o 1 8 |
| 1018 | end |
| 1019 | note an f16 value as f64 by arithmetic on its fields (not the HMMA model's bit moves) |
| 1020 | fn h64 h uint to f64 |
| 1021 | m = f64 and h 1023 |
| 1022 | e = and shr h 10 31 |
| 1023 | if ne e 0 |
| 1024 | m = add m 1024.0f64 |
| 1025 | else |
| 1026 | e = uint 1 |
| 1027 | end |
| 1028 | for i uint e |
| 1029 | m = mul m 2.0f64 |
| 1030 | end |
| 1031 | m = mul m 2.98023223876953125e-8f64 |
| 1032 | if ne and h 0x8000 0 |
| 1033 | ret neg m |
| 1034 | end |
| 1035 | ret m |
| 1036 | end |
| 1037 | note D[r][n] = sum over k of A[r][k] B[k][n] in f64; bound gamma_15 (u = 2^-23) times sum |A B|, |
| 1038 | note rounded up to 1966085 * 2^-40 (Wolfram, see seed/kflow/README.md) |
| 1039 | fn mmcell r ulong n ulong to void |
| 1040 | s = 0.0f64 |
| 1041 | z = 0.0f64 |
| 1042 | for k ulong 16 |
| 1043 | a = h64 gmhr add 0x10000 mul add mul r 16 k 2 |
| 1044 | b = h64 gmhr add 0x20000 mul add mul k 16 n 2 |
| 1045 | s = add s mul a b |
| 1046 | z = add z fabs mul a b |
| 1047 | end |
| 1048 | i = add mul r 16 n |
| 1049 | put rf i s |
| 1050 | q = div 1966085.0f64 1099511627776.0f64 |
| 1051 | put tl i mul z q |
| 1052 | exw add 0x30000 mul i 4 bitcast uint f32 s |
| 1053 | end |
| 1054 | note B transposed in place (gm and the expected arena): the tmmt negative control |
| 1055 | fn btr to void |
| 1056 | for i ulong 256 |
| 1057 | put ma i gmhr add 0x20000 mul i 2 |
| 1058 | end |
| 1059 | for k ulong 16 |
| 1060 | for n ulong 16 |
| 1061 | o = add 0x20000 mul add mul k 16 n 2 |
| 1062 | gmh o get ma add mul n 16 k |
| 1063 | put ex o get gm o |
| 1064 | put ex add o 1 get gm add o 1 |
| 1065 | end |
| 1066 | end |
| 1067 | end |
| 1068 | fn hmm tr bool to void |
| 1069 | cbput 0x360 32 |
| 1070 | cbput 0x364 1 |
| 1071 | cbput 0x368 1 |
| 1072 | cbput 0x37c 0xfffc |
| 1073 | cbp 0x380 add GB 0x10000 |
| 1074 | cbp 0x388 add GB 0x20000 |
| 1075 | cbp 0x390 add GB 0x30000 |
| 1076 | for i ulong 256 |
| 1077 | gmh add 0x10000 mul i 2 rnh |
| 1078 | gmh add 0x20000 mul i 2 rnh |
| 1079 | end |
| 1080 | for i ulong len gm |
| 1081 | put ex i get gm i |
| 1082 | end |
| 1083 | for r ulong 16 |
| 1084 | for n ulong 16 |
| 1085 | mmcell r n |
| 1086 | end |
| 1087 | end |
| 1088 | if tr |
| 1089 | btr |
| 1090 | end |
| 1091 | put fr 0 0x30000 |
| 1092 | put fr 1 256 |
| 1093 | put sv 1 32 |
| 1094 | put sv 11 1 |
| 1095 | end |
| 1096 | note e^z for z in [-8, 0]: series for e^(z/16), squared four times (no ln 2, no log2 e) |
| 1097 | fn expr z f64 to f64 |
| 1098 | y = mul z 0.0625f64 |
| 1099 | s = 1.0f64 |
| 1100 | t = 1.0f64 |
| 1101 | for i ulong 24 |
| 1102 | t = div mul t y f64 add i 1 |
| 1103 | s = add s t |
| 1104 | end |
| 1105 | for i ulong 4 |
| 1106 | s = mul s s |
| 1107 | end |
| 1108 | ret s |
| 1109 | end |
| 1110 | fn xin i ulong to f64 |
| 1111 | ret f64 bitcast f32 gmr add 0x10000 mul i 4 |
| 1112 | end |
| 1113 | note softmax of row r in f64; bound 2528617 * 2^-40 relative (Wolfram, see seed/kflow/README.md) |
| 1114 | fn smrow r ulong to void |
| 1115 | b = mul r 32 |
| 1116 | m = xin b |
| 1117 | for j ulong 32 |
| 1118 | x = xin add b j |
| 1119 | m = fmax m x |
| 1120 | end |
| 1121 | s = 0.0f64 |
| 1122 | for j ulong 32 |
| 1123 | x = xin add b j |
| 1124 | s = add s expr sub x m |
| 1125 | end |
| 1126 | for j ulong 32 |
| 1127 | i = add b j |
| 1128 | x = xin i |
| 1129 | y = div expr sub x m s |
| 1130 | put rf i y |
| 1131 | q = div 2528617.0f64 1099511627776.0f64 |
| 1132 | put tl i mul y q |
| 1133 | exw add 0x20000 mul i 4 bitcast uint f32 y |
| 1134 | end |
| 1135 | end |
| 1136 | note X: multiples of 2^-17 in [-4, 4), so x - max is exact in f32 |
| 1137 | fn hsmx to void |
| 1138 | cbput 0x360 32 |
| 1139 | cbput 0x364 1 |
| 1140 | cbput 0x368 1 |
| 1141 | cbput 0x37c 0xfffc |
| 1142 | cbp 0x380 add GB 0x10000 |
| 1143 | cbp 0x388 add GB 0x20000 |
| 1144 | for i ulong 512 |
| 1145 | x = mul f32 sub long and rnd 0xfffff 524288 7.62939453125e-6f32 |
| 1146 | gmw add 0x10000 mul i 4 bitcast uint x |
| 1147 | end |
| 1148 | for i ulong len gm |
| 1149 | put ex i get gm i |
| 1150 | end |
| 1151 | for r ulong 16 |
| 1152 | smrow r |
| 1153 | end |
| 1154 | put fr 0 0x20000 |
| 1155 | put fr 1 512 |
| 1156 | put sv 1 32 |
| 1157 | put sv 11 16 |
| 1158 | end |
| 1159 | note ---- arguments ---- |
| 1160 | fn argdec i ulong to ulong |
| 1161 | r = argread i ab 0 20 |
| 1162 | if lt r 1 |
| 1163 | ret 0xffffffffffffffff |
| 1164 | end |
| 1165 | v = ulong 0 |
| 1166 | for k ulong ulong r |
| 1167 | c = get ab k |
| 1168 | if or lt c 48 gt c 57 |
| 1169 | ret 0xffffffffffffffff |
| 1170 | end |
| 1171 | v = add mul v 10 ulong sub c 48 |
| 1172 | end |
| 1173 | ret v |
| 1174 | end |
| 1175 | fn harness to ulong |
| 1176 | r = argread 3 ab 0 8 |
| 1177 | if ne r 4 |
| 1178 | ret 0 |
| 1179 | end |
| 1180 | for h ulong 5 |
| 1181 | m = true |
| 1182 | for k ulong 4 |
| 1183 | if ne get ab k get hn add mul h 4 k |
| 1184 | m = false |
| 1185 | end |
| 1186 | end |
| 1187 | if m |
| 1188 | ret add h 1 |
| 1189 | end |
| 1190 | end |
| 1191 | ret 0 |
| 1192 | end |
| 1193 | fn loadbin to bool |
| 1194 | r = argread 2 ab 0 255 |
| 1195 | if lt r 1 |
| 1196 | ret false |
| 1197 | end |
| 1198 | fd = fopen ab 0 ulong r 524288 0 |
| 1199 | if lt fd 0 |
| 1200 | ret false |
| 1201 | end |
| 1202 | n = ulong 0 |
| 1203 | for k ulong 4097 |
| 1204 | x = read int fd code n sub len code n |
| 1205 | if le x 0 |
| 1206 | break |
| 1207 | end |
| 1208 | n = add n ulong x |
| 1209 | if eq n len code |
| 1210 | break |
| 1211 | end |
| 1212 | end |
| 1213 | y = read int fd ab 0 1 |
| 1214 | c = fclose int fd |
| 1215 | put sv 8 n |
| 1216 | ret and and gt n 0 eq rem n 16 0 eq y 0 |
| 1217 | end |
| 1218 | fn report to int |
| 1219 | st = get sv 5 |
| 1220 | if eq st 2 |
| 1221 | say 10 |
| 1222 | hexn ulong and get bad 0 0xfff |
| 1223 | say 11 |
| 1224 | hexn get sv 10 |
| 1225 | say 16 |
| 1226 | hexn ulong get bad 1 |
| 1227 | say 17 |
| 1228 | hexn ulong get bad 2 |
| 1229 | newline |
| 1230 | ret 2 |
| 1231 | end |
| 1232 | if eq st 3 |
| 1233 | say 12 |
| 1234 | hexn get sv 9 |
| 1235 | say 11 |
| 1236 | hexn get sv 10 |
| 1237 | newline |
| 1238 | ret 3 |
| 1239 | end |
| 1240 | if eq st 5 |
| 1241 | say 18 |
| 1242 | hexn get sv 10 |
| 1243 | newline |
| 1244 | ret 2 |
| 1245 | end |
| 1246 | if ne st 1 |
| 1247 | say 13 |
| 1248 | hexn get sv 10 |
| 1249 | newline |
| 1250 | ret 3 |
| 1251 | end |
| 1252 | lo = get fr 0 |
| 1253 | hi = add lo mul get fr 1 4 |
| 1254 | for i ulong len gm |
| 1255 | if and ne get gm i get ex i or lt i lo ge i hi |
| 1256 | say 7 |
| 1257 | hexn i |
| 1258 | say 8 |
| 1259 | hexn ulong get ex i |
| 1260 | say 9 |
| 1261 | hexn ulong get gm i |
| 1262 | newline |
| 1263 | ret 1 |
| 1264 | end |
| 1265 | end |
| 1266 | if ne fcmp 0 |
| 1267 | ret 1 |
| 1268 | end |
| 1269 | say 6 |
| 1270 | newline |
| 1271 | ret 0 |
| 1272 | end |
| 1273 | note the f32 region: |got - reference| within the bound; prints the largest share of the bound used |
| 1274 | fn fcmp to int |
| 1275 | lo = get fr 0 |
| 1276 | w = 0.0f64 |
| 1277 | for i ulong get fr 1 |
| 1278 | o = add lo mul i 4 |
| 1279 | g = f64 bitcast f32 gmr o |
| 1280 | e = fabs sub g get rf i |
| 1281 | t = get tl i |
| 1282 | if not le e t |
| 1283 | say 7 |
| 1284 | hexn o |
| 1285 | say 8 |
| 1286 | hexn ulong exr o |
| 1287 | say 9 |
| 1288 | hexn ulong gmr o |
| 1289 | newline |
| 1290 | ret 1 |
| 1291 | end |
| 1292 | q = div e t |
| 1293 | w = fmax w q |
| 1294 | end |
| 1295 | if ne get fr 1 0 |
| 1296 | say 19 |
| 1297 | num ulong mul w 1000000.0f64 |
| 1298 | end |
| 1299 | ret 0 |
| 1300 | end |
| 1301 | note every CTA, warp by warp; false at the first warp that does not end by EXIT in all lanes |
| 1302 | fn runall to bool |
| 1303 | for c ulong get sv 11 |
| 1304 | put sv 2 c |
| 1305 | for wi ulong div get sv 1 32 |
| 1306 | put sv 15 wi |
| 1307 | warp |
| 1308 | if ne get sv 5 1 |
| 1309 | ret false |
| 1310 | end |
| 1311 | end |
| 1312 | end |
| 1313 | ret true |
| 1314 | end |
| 1315 | fn main to int |
| 1316 | if ne argcnt 5 |
| 1317 | say 1 |
| 1318 | newline |
| 1319 | ret 3 |
| 1320 | end |
| 1321 | ar = argdec 1 |
| 1322 | sd = argdec 4 |
| 1323 | h = harness |
| 1324 | if or or eq h 0 eq sd 0xffffffffffffffff and ne ar 80 ne ar 121 |
| 1325 | say 15 |
| 1326 | newline |
| 1327 | ret 3 |
| 1328 | end |
| 1329 | if not loadbin |
| 1330 | say 14 |
| 1331 | newline |
| 1332 | ret 3 |
| 1333 | end |
| 1334 | put sv 0 ar |
| 1335 | put sv 4 255 |
| 1336 | if eq ar 80 |
| 1337 | put sv 4 63 |
| 1338 | end |
| 1339 | put sv 7 sd |
| 1340 | arena |
| 1341 | if eq h 1 |
| 1342 | hvadd |
| 1343 | end |
| 1344 | if eq h 2 |
| 1345 | hradd |
| 1346 | end |
| 1347 | if or eq h 3 eq h 4 |
| 1348 | hmm eq h 4 |
| 1349 | end |
| 1350 | if eq h 5 |
| 1351 | hsmx |
| 1352 | end |
| 1353 | say 2 |
| 1354 | num ar |
| 1355 | say 3 |
| 1356 | r = write 1 hn mul sub h 1 4 4 |
| 1357 | if not runall |
| 1358 | say 4 |
| 1359 | t = add mul get sv 2 get sv 1 get sv 3 |
| 1360 | num t |
| 1361 | say 5 |
| 1362 | num get sv 6 |
| 1363 | ret report |
| 1364 | end |
| 1365 | say 4 |
| 1366 | num mul get sv 11 get sv 1 |
| 1367 | say 5 |
| 1368 | num get sv 6 |
| 1369 | ret report |
| 1370 | end |