Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/om/gpumath/gpumath.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / om / gpumath / gpumath.om` 1244 lines ยท 24773 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note gpumath.om: seeded Omega port of src/omega_gpu_hostmath.c, src/omega_gpu_fp8_pack.c and 3 note src/omega_gpu_fmt.c (aien-dev/omega-mv#726). Parity is on bit patterns against the C as the 4 note product builds it (gcc -std=gnu11 -O2, the Makefile CFLAGS, aarch64). Where the C does float 5 note arithmetic this file does the same IEEE operation in the same operand order (the operand order 6 note decides which NaN payload survives). gcc contracts some of the C's a*b+c into one fused 7 note FMADD/FMSUB (gnu11 implies -ffp-contract=fast); Omega has no fused operation, so fma64 below 8 note computes the fused result exactly in integer arithmetic. 9 note 10 note Exported functions (omega-bridge native call ABI v1: ulong/long scalars, fixed-length 11 note ulong/uchar arrays, ulong/long/void result). A double or float travels as its bit pattern. 12 note hmlog x omega_hostmath_log, f64 bits in and out 13 note hmexp y omega_hostmath_exp 14 note hmpow b e omega_hostmath_pow 15 note hmsincos a o omega_hostmath_sincos: o 0 sin bits, o 1 cos bits 16 note hmpow2f e omega_hostmath_pow2f: f32 bits 17 note e4m3ref b omega_fp8_e4m3_ref: f32 bits (b 0..255, else trap 5) 18 note e4m3kbits b omega_fp8_e4m3_kernel_bits 19 note e4m3isnan b omega_fp8_e4m3_is_nan: 1 or 0 20 note f8scannan w n base st omega_fp8_pack_scan_nan over a 4096-byte piece (see the function) 21 note f8packscales i n o nf omega_fp8_pack_scales over a 4096-element piece 22 note f8packxf32 i n o omega_fp8_pack_x_f32 over a 4096-element piece 23 note f8packxbf16 i n o omega_fp8_pack_x_bf16 over a 4096-element piece 24 note f8gemvblock w x sb acc one 128-column block of one row of omega_fp8_gemv_oracle 25 note gpufmt o cap f args pl omega_gpu_fmt with the varargs as 64-bit words (see the function) 26 note gpufmtkinds f kinds the va_arg type of every conversion gpufmt consumes 27 note Arrays carry little-endian element bytes exactly as the C arrays hold them in memory. 28 29 note ---- 128-bit helpers and the exact fused multiply-add ---- 30 31 note index of the highest set bit of x (x > 0) 32 fn msb64 x ulong to ulong 33 n = ulong 0 34 if ge x `4294967296` 35 x = shr x 32 36 n = add n 32 37 end 38 if ge x 65536 39 x = shr x 16 40 n = add n 16 41 end 42 if ge x 256 43 x = shr x 8 44 n = add n 8 45 end 46 if ge x 16 47 x = shr x 4 48 n = add n 4 49 end 50 if ge x 4 51 x = shr x 2 52 n = add n 2 53 end 54 if ge x 2 55 n = add n 1 56 end 57 ret n 58 end 59 60 fn isnan64 x ulong to bool 61 ret gt and x `9223372036854775807` `9218868437227405312` 62 end 63 64 fn issnan64 x ulong to bool 65 ret and isnan64 x eq and x `2251799813685248` 0 66 end 67 68 fn isinf64 x ulong to bool 69 ret eq and x `9223372036854775807` `9218868437227405312` 70 end 71 72 fn iszero64 x ulong to bool 73 ret eq and x `9223372036854775807` 0 74 end 75 76 note significand of a finite nonzero double, shifted so that bit 52 is its top bit 77 fn mantn x ulong to ulong 78 ex = and shr x 52 2047 79 m = and x `4503599627370495` 80 if gt ex 0 81 ret or m `4503599627370496` 82 end 83 ret shl m sub 52 msb64 m 84 end 85 86 note exponent matching mantn: value = mantn x * 2^expn x 87 fn expn x ulong to long 88 ex = and shr x 52 2047 89 if gt ex 0 90 ret sub long ex 1075 91 end 92 m = and x `4503599627370495` 93 ret sub -1074 long sub 52 msb64 m 94 end 95 96 note t 0, t 1 = high, low words of a * b (a, b < 2^53) 97 fn mul128 a ulong b ulong t edit ulong 8 to void 98 al = and a `4294967295` 99 ah = shr a 32 100 bl = and b `4294967295` 101 bh = shr b 32 102 ll = mul al bl 103 mid = add add mul al bh mul ah bl shr ll 32 104 put t 1 or shl mid 32 and ll `4294967295` 105 put t 0 add mul ah bh shr mid 32 106 end 107 108 note shift the 128-bit value t k (high), t k+1 (low) right by n, OR-ing every lost bit into bit 0 109 fn srj t edit ulong 8 k ulong n ulong to void 110 if eq n 0 111 ret 112 end 113 hi = get t k 114 lo = get t add k 1 115 if ge n 128 116 put t k 0 117 if eq or hi lo 0 118 put t add k 1 0 119 else 120 put t add k 1 1 121 end 122 ret 123 end 124 st = ulong 0 125 if ge n 64 126 m = sub n 64 127 nlo = hi 128 if gt m 0 129 nlo = shr hi m 130 if ne shl hi sub 64 m 0 131 st = 1 132 end 133 end 134 if ne lo 0 135 st = 1 136 end 137 put t k 0 138 put t add k 1 or nlo st 139 ret 140 end 141 if ne shl lo sub 64 n 0 142 st = 1 143 end 144 put t add k 1 or or shr lo n shl hi sub 64 n st 145 put t k shr hi n 146 end 147 148 note t 4, t 5 = (t 0, t 1) + (t 2, t 3) 149 fn add128 t edit ulong 8 to void 150 lo = add get t 1 get t 3 151 cy = ulong 0 152 if lt lo get t 1 153 cy = 1 154 end 155 put t 5 lo 156 put t 4 add add get t 0 get t 2 cy 157 end 158 159 note t 4, t 5 = (t a, t a+1) - (t b, t b+1), the first not below the second 160 fn sub128 t edit ulong 8 a ulong b ulong to void 161 l1 = get t add a 1 162 l2 = get t add b 1 163 bw = ulong 0 164 if lt l1 l2 165 bw = 1 166 end 167 put t 5 sub l1 l2 168 put t 4 sub sub get t a get t b bw 169 end 170 171 fn ge128 t edit ulong 8 to bool 172 if ne get t 0 get t 2 173 ret gt get t 0 get t 2 174 end 175 ret ge get t 1 get t 3 176 end 177 178 note bit k of the 128-bit value hi, lo 179 fn bit128 hi ulong lo ulong k ulong to ulong 180 if ge k 64 181 ret and shr hi sub k 64 1 182 end 183 ret and shr lo k 1 184 end 185 186 note any bit of hi, lo below bit k 187 fn low128 hi ulong lo ulong k ulong to bool 188 if ge k 64 189 ret or ne lo 0 ne and hi sub shl ulong 1 sub k 64 1 0 190 end 191 ret ne and lo sub shl ulong 1 k 1 0 192 end 193 194 note round S = (t 4, t 5) * 2^x (S > 0) to nearest even as a double with sign sg; subnormal 195 note results round once at 2^-1074, overflow gives infinity (ARM FPRound, FPCR at its Linux default) 196 fn fround t edit ulong 8 x long sg ulong to ulong 197 hi = get t 4 198 lo = get t 5 199 h = msb64 lo 200 if ne hi 0 201 h = add msb64 hi 64 202 end 203 s = sub long h 52 204 mn = sub -1074 x 205 if lt s mn 206 s = mn 207 end 208 sgn = shl sg 63 209 if ge s 128 210 ret sgn 211 end 212 q = ulong 0 213 if gt s 0 214 us = ulong s 215 if ge us 64 216 q = shr hi sub us 64 217 else 218 q = or shr lo us shl hi sub 64 us 219 end 220 if eq bit128 hi lo sub us 1 1 221 if or low128 hi lo sub us 1 eq and q 1 1 222 q = add q 1 223 end 224 end 225 else 226 q = shl lo ulong neg s 227 end 228 bits = add shl ulong add add x s 1074 52 q 229 if ge bits `9218868437227405312` 230 ret or sgn `9218868437227405312` 231 end 232 ret or sgn bits 233 end 234 235 note the NaN result of c + a * b when an operand is NaN (FPProcessNaNs3, addend first; a quiet NaN 236 note addend with infinity times zero gives the default NaN) 237 fn fmanan a ulong b ulong c ulong to ulong 238 if issnan64 c 239 ret or c `2251799813685248` 240 end 241 if issnan64 a 242 ret or a `2251799813685248` 243 end 244 if issnan64 b 245 ret or b `2251799813685248` 246 end 247 if isnan64 c 248 if or and isinf64 a iszero64 b and iszero64 a isinf64 b 249 ret `9221120237041090560` 250 end 251 ret c 252 end 253 if isnan64 a 254 ret a 255 end 256 ret b 257 end 258 259 note c + a * b with infinities or zeros among the operands (no NaN operand) 260 fn fmaspec a ulong b ulong c ulong to ulong 261 sp = xor shr a 63 shr b 63 262 sc = shr c 63 263 infp = or isinf64 a isinf64 b 264 infc = isinf64 c 265 if or and isinf64 a iszero64 b and iszero64 a isinf64 b 266 ret `9221120237041090560` 267 end 268 if and and infc infp ne sc sp 269 ret `9221120237041090560` 270 end 271 if or and infc eq sc 0 and infp eq sp 0 272 ret `9218868437227405312` 273 end 274 if or infc infp 275 ret `18442240474082181120` 276 end 277 if iszero64 c 278 if eq sc sp 279 ret c 280 end 281 ret 0 282 end 283 ret c 284 end 285 286 note the exact product a * b (a, b finite nonzero) and the addend c placed in t 0..1 and t 2..3, 287 note both with their top bit at bit 124 or 125, the smaller shifted right with a sticky bit to the 288 note larger's scale; returns that scale x (value = t * 2^x) 289 fn fmaalign a ulong b ulong c ulong t edit ulong 8 to long 290 mul128 mantn a mantn b t 291 ph = get t 0 292 pl = get t 1 293 put t 0 or shl ph 20 shr pl 44 294 put t 1 shl pl 20 295 xp = sub add expn a expn b 20 296 if iszero64 c 297 ret xp 298 end 299 put t 2 shl mantn c 9 300 xc = sub expn c 73 301 d = sub xc xp 302 if gt d 0 303 srj t 0 ulong d 304 ret xc 305 end 306 srj t 2 ulong neg d 307 ret xp 308 end 309 310 note c + a * b, finite, a and b nonzero: the exact sum rounded once 311 fn fmafin a ulong b ulong c ulong to ulong 312 t = new ulong 8 0 313 x = fmaalign a b c t 314 sp = xor shr a 63 shr b 63 315 sc = shr c 63 316 sg = sp 317 if or iszero64 c eq sp sc 318 add128 t 319 else 320 if ge128 t 321 sub128 t 0 2 322 else 323 sub128 t 2 0 324 sg = sc 325 end 326 end 327 if eq or get t 4 get t 5 0 328 ret 0 329 end 330 ret fround t x sg 331 end 332 333 note AArch64 FMADD d = c + a * b (one rounding, ARM NaN rules); FMSUB d = c - a * b is 334 note fma64 with the sign of a flipped 335 fn fma64 a ulong b ulong c ulong to ulong 336 if or or isnan64 a isnan64 b isnan64 c 337 ret fmanan a b c 338 end 339 if or or or isinf64 a isinf64 b isinf64 c or iszero64 a iszero64 b 340 ret fmaspec a b c 341 end 342 ret fmafin a b c 343 end 344 345 note ---- omega_gpu_hostmath.c ---- 346 347 note pow2i: 2^k as a double, -1074 <= k <= 1023, 0 below, +inf above 348 fn pow2i k long to f64 349 if gt k 1023 350 ret bitcast f64 ulong `9218868437227405312` 351 end 352 if lt k -1074 353 ret 0.0f64 354 end 355 if ge k -1022 356 ret bitcast f64 shl ulong add k 1023 52 357 end 358 ret mul bitcast f64 ulong `4503599627370496` bitcast f64 shl ulong add k 2045 52 359 end 360 361 note log(x): the s * t2 + 1/k step and the two final steps are fused in the C object 362 fn hmlog xb ulong to ulong 363 x = bitcast f64 xb 364 if or not gt x 0.0f64 gt x bitcast f64 ulong `9218868437227405311` 365 ret `9221120237041090560` 366 end 367 e = long 0 368 if lt x bitcast f64 ulong `4503599627370496` 369 x = mul x bitcast f64 ulong `4850376798678024192` 370 e = -54 371 end 372 u = bitcast ulong x 373 e = add e sub long and shr u 52 2047 1023 374 m = bitcast f64 or and u `4503599627370495` `4607182418800017408` 375 if gt m bitcast f64 ulong `4609047870845172685` 376 m = mul m 0.5f64 377 e = add e 1 378 end 379 t = div sub m 1.0f64 add m 1.0f64 380 t2 = bitcast ulong mul t t 381 s = ulong 0 382 k = long 25 383 while ge k 1 bound 13 384 s = fma64 s t2 bitcast ulong div 1.0f64 f64 k 385 k = sub k 2 386 end 387 ed = f64 e 388 lo = bitcast ulong mul ed bitcast f64 ulong `4461442080421002358` 389 lo = fma64 s bitcast ulong add t t lo 390 ret fma64 bitcast ulong ed `4604418534311723008` lo 391 end 392 393 note exp(y): r = (y - k * ln2hi) - k * ln2lo is two FMSUBs in the C object 394 fn hmexp yb ulong to ulong 395 y = bitcast f64 yb 396 if ne y y 397 ret yb 398 end 399 if gt y bitcast f64 ulong `4649454506724284170` 400 ret `9218868437227405312` 401 end 402 if lt y bitcast f64 ulong `13873138101193906586` 403 ret 0 404 end 405 fk = mul y bitcast f64 ulong `4609176140021203710` 406 h = -0.5f64 407 if ge fk 0.0f64 408 h = 0.5f64 409 end 410 k = long add fk h 411 nk = bitcast ulong neg f64 k 412 r = fma64 nk `4604418534311723008` yb 413 r = fma64 nk `4461442080421002358` r 414 rd = bitcast f64 r 415 p = 1.0f64 416 n = long 17 417 while ge n 1 bound 17 418 p = add div mul rd p f64 n 1.0f64 419 n = sub n 1 420 end 421 if lt k -1020 422 ret bitcast ulong mul mul p pow2i -1020 pow2i add k 1020 423 end 424 ret bitcast ulong mul p pow2i k 425 end 426 427 fn hmpow bb ulong eb ulong to ulong 428 b = bitcast f64 bb 429 e = bitcast f64 eb 430 if or eq b 1.0f64 eq e 0.0f64 431 ret `4607182418800017408` 432 end 433 ret hmexp bitcast ulong mul bitcast f64 hmlog bb e 434 end 435 436 note sin r = r * ps, cos r = pc (w = r * r), the two series of the C 437 fn sinser w f64 to f64 438 ps = 1.0f64 439 k = long 15 440 while ge k 3 bound 7 441 ps = sub 1.0f64 div mul w ps f64 mul k sub k 1 442 k = sub k 2 443 end 444 ret ps 445 end 446 447 fn cosser w f64 to f64 448 pc = 1.0f64 449 k = long 16 450 while ge k 2 bound 8 451 pc = sub 1.0f64 div mul w pc f64 mul k sub k 1 452 k = sub k 2 453 end 454 ret pc 455 end 456 457 note sincos(a): o 0 = sin bits, o 1 = cos bits; the four reduction steps are FMSUBs in the C object 458 fn hmsincos ab ulong o edit ulong 2 to void 459 a = bitcast f64 ab 460 fq = mul a bitcast f64 ulong `4603909380684499075` 461 h = -0.5f64 462 if ge fq 0.0f64 463 h = 0.5f64 464 end 465 n = long add fq h 466 nn = bitcast ulong neg f64 n 467 r = fma64 nn `4609753056924401664` ab 468 r = fma64 nn `4454258360616747008` r 469 r = fma64 nn `4297306550709518336` r 470 r = fma64 nn `4142048980368378305` r 471 rd = bitcast f64 r 472 w = mul rd rd 473 sr = mul rd sinser w 474 cr = cosser w 475 q = and n 3 476 if eq q 0 477 put o 0 bitcast ulong sr 478 put o 1 bitcast ulong cr 479 ret 480 end 481 if eq q 1 482 put o 0 bitcast ulong cr 483 put o 1 bitcast ulong neg sr 484 ret 485 end 486 if eq q 2 487 put o 0 bitcast ulong neg sr 488 put o 1 bitcast ulong neg cr 489 ret 490 end 491 put o 0 bitcast ulong neg cr 492 put o 1 bitcast ulong sr 493 end 494 495 note 2^e as f32 bits, exact for -149 <= e <= 127; 0 below, +inf above 496 fn hmpow2f e long to ulong 497 if gt e 127 498 ret `2139095040` 499 end 500 if ge e -126 501 ret shl ulong add e 127 23 502 end 503 if ge e -149 504 ret shl ulong 1 add e 149 505 end 506 ret 0 507 end 508 509 note ---- omega_gpu_fp8_pack.c ---- 510 511 note f32 helpers return the bit pattern (omc refuses an f32 result type, error 5) 512 fn p2f e long to uint 513 u = hmpow2f e 514 ret uint u 515 end 516 517 fn e4m3ref bb ulong to ulong 518 b = uchar bb 519 ex = and shr b 3 15 520 mf = f32 and b 7 521 sg = 1.0f32 522 if ne and b 128 0 523 sg = -1.0f32 524 end 525 if and eq ex 15 eq and b 7 7 526 ret `2143289344` 527 end 528 if eq ex 0 529 ret ulong bitcast uint mul mul sg mf bitcast f32 p2f -9 530 end 531 v = mul add 1.0f32 div mf 8.0f32 sg 532 ret ulong bitcast uint mul v bitcast f32 p2f sub long ex 7 533 end 534 535 fn e4m3kbits bb ulong to ulong 536 b = ulong uchar bb 537 ret or shl and b 127 20 shl and b 128 24 538 end 539 540 fn e4m3isnan bb ulong to ulong 541 b = uchar bb 542 if eq and b 127 127 543 ret 1 544 end 545 ret 0 546 end 547 548 note one piece of omega_fp8_pack_scan_nan(w, count, first): scans w 0 .. n-1 (n <= 4096), the 549 note piece starting at element base of the whole array. st 0 is the NaN count so far (0 before the 550 note first piece), st 1 the index of the first NaN, written only when the count was 0 (as the C 551 note writes *first). Returns st 0. 552 fn f8scannan w view uchar 4096 n ulong base ulong st edit ulong 2 to ulong 553 for i ulong n 554 if eq and get w i 127 127 555 if eq get st 0 0 556 put st 1 add base i 557 end 558 put st 0 add get st 0 1 559 end 560 end 561 ret get st 0 562 end 563 564 fn ldbf16 v view uchar 8192 i ulong to uint 565 lo = uint get v mul i 2 566 hi = uint get v add mul i 2 1 567 ret shl or lo shl hi 8 16 568 end 569 570 fn ldf32 v view uchar 16384 i ulong to uint 571 o = mul i 4 572 x = uint get v add o 3 573 x = or shl x 8 uint get v add o 2 574 x = or shl x 8 uint get v add o 1 575 x = or shl x 8 uint get v o 576 ret x 577 end 578 579 fn stf32 v edit uchar 16384 i ulong f f32 to void 580 o = mul i 4 581 x = bitcast uint f 582 put v o uchar and x 255 583 put v add o 1 uchar and shr x 8 255 584 put v add o 2 uchar and shr x 16 255 585 put v add o 3 uchar shr x 24 586 end 587 588 note one piece of omega_fp8_pack_scales: in holds n bf16 (n <= 4096), out gets n f32; nofold 589 note nonzero is the C's mutant_no_fold. 0, or -1 at the first NaN/inf scale or fold overflow with 590 note the earlier outputs written, as the C. 591 fn f8packscales in view uchar 8192 n ulong out edit uchar 16384 nofold ulong to long 592 fold = 1.0f32 593 if eq nofold 0 594 fold = bitcast f32 p2f 56 595 end 596 lim = bitcast f32 uint `2137108966` 597 for i ulong n 598 s = bitcast f32 ldbf16 in i 599 if not le fabs s lim 600 ret -1 601 end 602 f = mul fold s 603 if not le fabs f lim 604 ret -1 605 end 606 stf32 out i f 607 end 608 ret 0 609 end 610 611 note one piece of omega_fp8_pack_x_f32: out = in * 2^64, n <= 4096 f32 612 fn f8packxf32 in view uchar 16384 n ulong out edit uchar 16384 to void 613 fold = bitcast f32 p2f 64 614 for i ulong n 615 stf32 out i mul bitcast f32 ldf32 in i fold 616 end 617 end 618 619 note one piece of omega_fp8_pack_x_bf16: out = bf16 in * 2^64, n <= 4096 620 fn f8packxbf16 in view uchar 8192 n ulong out edit uchar 16384 to void 621 fold = bitcast f32 p2f 64 622 for i ulong n 623 stf32 out i mul bitcast f32 ldbf16 in i fold 624 end 625 end 626 627 fn ldx v view uchar 512 i ulong to uint 628 o = mul i 4 629 x = uint get v add o 3 630 x = or shl x 8 uint get v add o 2 631 x = or shl x 8 uint get v add o 1 632 x = or shl x 8 uint get v o 633 ret x 634 end 635 636 note one 128-column block of one row of omega_fp8_gemv_oracle: w the row's 128 e4m3 bytes, x the 637 note 128 f32 of x, sb the bf16 scale of the block, acc 0 / acc 1 the row's acc / mag (f64 bits, 638 note 0 before the row's first block). The caller runs the blocks of a row in order, as the C. 639 fn f8gemvblock w view uchar 128 x view uchar 512 sb ulong acc edit ulong 2 to void 640 s = f64 bitcast f32 shl uint ushort sb 16 641 a = bitcast f64 get acc 0 642 mg = bitcast f64 get acc 1 643 for c ulong 128 644 rf = f64 bitcast f32 uint e4m3ref ulong get w c 645 p = mul mul f64 bitcast f32 ldx x c rf s 646 a = add a p 647 mg = add mg fabs p 648 end 649 put acc 0 bitcast ulong a 650 put acc 1 bitcast ulong mg 651 end 652 653 note ---- omega_gpu_fmt.c ---- 654 655 note the sink: sn 0 = characters written, sn 1 = cap 656 fn fput out edit uchar 1024 sn edit ulong 2 c uchar to void 657 n = get sn 0 658 if lt add n 1 get sn 1 659 put out n c 660 put sn 0 add n 1 661 end 662 end 663 664 note a %s argument: an offset into the string pool pl (NUL-terminated there), or 2^64-1 for NULL 665 fn fputs out edit uchar 1024 sn edit ulong 2 pl view uchar 4096 off ulong to void 666 if eq off `18446744073709551615` 667 fput out sn 40 668 fput out sn 110 669 fput out sn 117 670 fput out sn 108 671 fput out sn 108 672 fput out sn 41 673 ret 674 end 675 i = off 676 while ne get pl i 0 bound 4096 677 fput out sn get pl i 678 i = add i 1 679 end 680 end 681 682 fn fputu out edit uchar 1024 sn edit ulong 2 v ulong base ulong to void 683 tmp = new uchar 24 0 684 i = ulong 0 685 while true bound 24 686 d = rem v base 687 if lt d 10 688 put tmp i uchar add d 48 689 else 690 put tmp i uchar add d 87 691 end 692 i = add i 1 693 v = div v base 694 if eq v 0 695 break 696 end 697 end 698 while gt i 0 bound 24 699 i = sub i 1 700 fput out sn get tmp i 701 end 702 end 703 704 fn fputd out edit uchar 1024 sn edit ulong 2 v long to void 705 if lt v 0 706 fput out sn 45 707 fputu out sn add ulong neg add v 1 1 10 708 ret 709 end 710 fputu out sn ulong v 10 711 end 712 713 note the low 32 bits of w as a signed int 714 fn sx32 w ulong to long 715 v = and w `4294967295` 716 if ge v `2147483648` 717 ret sub long v `4294967296` 718 end 719 ret long v 720 end 721 722 note the conversion at fmt p (fmt p is '%'): r 0 = index of the conversion byte, r 1 = the va_arg 723 note kind it consumes (0 none; 1 int, 2 long, 3 long long, 4 ptrdiff_t, 5 unsigned int, 724 note 6 unsigned long, 7 unsigned long long, 8 size_t, 9 const char *) 725 fn fscan fmt view uchar 1024 p ulong r edit ulong 4 to void 726 q = add p 1 727 z = false 728 l = ulong 0 729 if eq get fmt q 122 730 z = true 731 q = add q 1 732 else 733 while and eq get fmt q 108 lt l 2 bound 3 734 l = add l 1 735 q = add q 1 736 end 737 end 738 ch = get fmt q 739 k = ulong 0 740 if eq ch 115 741 k = 9 742 end 743 if eq ch 99 744 k = 1 745 end 746 if eq ch 100 747 k = 1 748 if eq l 1 749 k = 2 750 end 751 if eq l 2 752 k = 3 753 end 754 if z 755 k = 4 756 end 757 end 758 if or eq ch 117 eq ch 120 759 k = 5 760 if eq l 1 761 k = 6 762 end 763 if eq l 2 764 k = 7 765 end 766 if z 767 k = 8 768 end 769 end 770 put r 0 q 771 put r 1 k 772 end 773 774 note one consuming conversion: argument word w of kind k, conversion byte cc 775 fn fconv out edit uchar 1024 sn edit ulong 2 w ulong k ulong cc uchar pl view uchar 4096 to void 776 if eq k 9 777 fputs out sn pl w 778 ret 779 end 780 if eq cc 99 781 fput out sn uchar and w 255 782 ret 783 end 784 if eq k 1 785 fputd out sn sx32 w 786 ret 787 end 788 if lt k 5 789 fputd out sn bitcast long w 790 ret 791 end 792 base = ulong 10 793 if eq cc 120 794 base = 16 795 end 796 if eq k 5 797 fputu out sn and w `4294967295` base 798 ret 799 end 800 fputu out sn w base 801 end 802 803 note one '%' conversion (r 0, r 1 from fscan, r 2 the next argument, r 3 the '%' index) 804 fn fone out edit uchar 1024 sn edit ulong 2 fmt view uchar 1024 args view ulong 16 pl view uchar 4096 r edit ulong 4 to void 805 q = get r 0 806 k = get r 1 807 cc = get fmt q 808 if gt k 0 809 fconv out sn get args get r 2 k cc pl 810 put r 2 add get r 2 1 811 ret 812 end 813 if eq cc 37 814 fput out sn 37 815 ret 816 end 817 note unsupported conversion: copied literally, nothing consumed 818 j = get r 3 819 while le j q bound 5 820 if eq get fmt j 0 821 break 822 end 823 fput out sn get fmt j 824 j = add j 1 825 end 826 end 827 828 note omega_gpu_fmt(out, cap, fmt, ...): fmt NUL-terminated inside its 1024 bytes; the i-th consuming 829 note conversion takes args i (16 at most): an int-sized kind uses the low 32 bits (the AAPCS64 830 note va_arg of an int), %c the low 8; %s takes a pool offset (2^64-1 = NULL, printed "(null)"). 831 note Writes at most cap - 1 characters and the NUL (cap <= 1024, else out-of-range trap 3 once 832 note the output reaches 1024); cap 0 writes nothing. Returns the characters written. 833 fn gpufmt out edit uchar 1024 cap ulong fmt view uchar 1024 args view ulong 16 pl view uchar 4096 to ulong 834 if eq cap 0 835 ret 0 836 end 837 sn = new ulong 2 0 838 r = new ulong 4 0 839 put sn 1 cap 840 p = ulong 0 841 while ne get fmt p 0 bound 1024 842 if ne get fmt p 37 843 fput out sn get fmt p 844 p = add p 1 845 continue 846 end 847 fscan fmt p r 848 put r 3 p 849 fone out sn fmt args pl r 850 q = get r 0 851 p = add q 1 852 if eq get fmt q 0 853 p = q 854 end 855 end 856 put out get sn 0 0 857 ret get sn 0 858 end 859 860 note the va_arg kinds (fscan codes) of fmt's consuming conversions, in order; returns their count 861 fn gpufmtkinds fmt view uchar 1024 kinds edit ulong 64 to ulong 862 r = new ulong 4 0 863 p = ulong 0 864 nk = ulong 0 865 while ne get fmt p 0 bound 1024 866 if ne get fmt p 37 867 p = add p 1 868 continue 869 end 870 fscan fmt p r 871 q = get r 0 872 k = get r 1 873 if gt k 0 874 put kinds nk k 875 nk = add nk 1 876 end 877 p = add q 1 878 if eq get fmt q 0 879 p = q 880 end 881 end 882 ret nk 883 end 884 885 note ==== parity harness: program mode only; everything above this line is the library ==== 886 note Reads corpus records on stdin (om/gpumath/parity/corpus.py), writes one result line per record. 887 note Numbers are lowercase hex, byte strings one hex token ("-" when empty). 888 889 global ib uchar `1048576` 890 global ist ulong 2 891 global ob uchar `1048576` 892 global obn ulong 1 893 global tb uchar 262144 894 global gw uchar 262144 895 global gsc uchar 8192 896 global gx uchar 131072 897 898 fn inb to ulong 899 p = get ist 0 900 if ge p get ist 1 901 r = read 0 ib 0 `1048576` 902 if le r 0 903 ret 256 904 end 905 put ist 1 ulong r 906 p = 0 907 end 908 put ist 0 add p 1 909 ret ulong get ib p 910 end 911 912 fn obflush to void 913 n = get obn 0 914 if gt n 0 915 r = write 1 ob 0 n 916 end 917 put obn 0 0 918 end 919 920 fn ob1 c ulong to void 921 n = get obn 0 922 if ge n `1048576` 923 obflush 924 n = 0 925 end 926 put ob n uchar c 927 put obn 0 add n 1 928 end 929 930 fn hexdig v ulong to ulong 931 if lt v 10 932 ret add v 48 933 end 934 ret add v 87 935 end 936 937 fn ohex v ulong to void 938 ob1 32 939 for i ulong 16 940 ob1 hexdig and shr v mul sub 15 i 4 15 941 end 942 end 943 944 fn ohexb b ulong to void 945 ob1 hexdig shr b 4 946 ob1 hexdig and b 15 947 end 948 949 fn hexval c ulong to ulong 950 if and ge c 48 le c 57 951 ret sub c 48 952 end 953 if and ge c 97 le c 102 954 ret sub c 87 955 end 956 ret 16 957 end 958 959 fn rdcode to ulong 960 c = inb 961 while or eq c 10 eq c 32 bound 999999 962 c = inb 963 end 964 ret c 965 end 966 967 fn rdhex to ulong 968 c = inb 969 while eq c 32 bound 999999 970 c = inb 971 end 972 v = ulong 0 973 while lt hexval c 16 bound 17 974 v = or shl v 4 hexval c 975 c = inb 976 end 977 ret v 978 end 979 980 note one hex byte-string token into tb; returns the byte count ("-" is empty) 981 fn rdbytes to ulong 982 c = inb 983 while eq c 32 bound 999999 984 c = inb 985 end 986 if eq c 45 987 c = inb 988 ret 0 989 end 990 n = ulong 0 991 while lt hexval c 16 bound 999999 992 d = inb 993 put tb n uchar or shl hexval c 4 hexval d 994 n = add n 1 995 c = inb 996 end 997 ret n 998 end 999 1000 fn sgnl v ulong to long 1001 if ge v `9223372036854775808` 1002 ret neg long add not v 1 1003 end 1004 ret long v 1005 end 1006 1007 fn u32at v view uchar 16384 i ulong to ulong 1008 o = mul i 4 1009 x = ulong get v add o 3 1010 x = or shl x 8 ulong get v add o 2 1011 x = or shl x 8 ulong get v add o 1 1012 ret or shl x 8 ulong get v o 1013 end 1014 1015 fn doz to void 1016 a = rdhex 1017 b = rdhex 1018 c = rdhex 1019 ob1 90 1020 ohex fma64 a b c 1021 end 1022 1023 fn dosc to void 1024 o = new ulong 2 0 1025 a = rdhex 1026 hmsincos a o 1027 ob1 68 1028 ohex get o 0 1029 ohex get o 1 1030 end 1031 1032 fn dof to void 1033 b = rdhex 1034 ob1 70 1035 ohex e4m3ref b 1036 ohex e4m3kbits b 1037 ohex e4m3isnan b 1038 end 1039 1040 note G nofold bf16-bytes / H f32-bytes / I bf16-bytes: one piece of the pack functions 1041 fn dopack c ulong to void 1042 in = new uchar 8192 0 1043 inf = new uchar 16384 0 1044 out = new uchar 16384 170 1045 nf = ulong 0 1046 if eq c 71 1047 nf = rdhex 1048 end 1049 nb = rdbytes 1050 n = div nb 2 1051 if eq c 72 1052 n = div nb 4 1053 end 1054 for i ulong nb 1055 if eq c 72 1056 put inf i get tb i 1057 else 1058 put in i get tb i 1059 end 1060 end 1061 ob1 c 1062 if eq c 71 1063 ohex bitcast ulong f8packscales in n out nf 1064 end 1065 if eq c 72 1066 f8packxf32 inf n out 1067 end 1068 if eq c 73 1069 f8packxbf16 in n out 1070 end 1071 for i ulong n 1072 ohex u32at out i 1073 end 1074 end 1075 1076 note N bytes: omega_fp8_pack_scan_nan over the whole array, in 4096-byte pieces 1077 fn doscan to void 1078 w = new uchar 4096 0 1079 st = new ulong 2 0 1080 put st 1 `12297829382473034410` 1081 nb = rdbytes 1082 base = ulong 0 1083 while lt base nb bound 64 1084 m = sub nb base 1085 if gt m 4096 1086 m = 4096 1087 end 1088 for i ulong m 1089 put w i get tb add base i 1090 end 1091 r = f8scannan w m base st 1092 base = add base m 1093 end 1094 ob1 78 1095 ohex get st 0 1096 ohex get st 1 1097 end 1098 1099 fn ldblk wb edit uchar 128 xb edit uchar 512 wo ulong xo ulong to void 1100 for c ulong 128 1101 put wb c get gw add wo c 1102 end 1103 for c ulong 512 1104 put xb c get gx add xo c 1105 end 1106 end 1107 1108 fn gsb i ulong to ulong 1109 si = mul i 2 1110 ret or ulong get gsc si shl ulong get gsc add si 1 8 1111 end 1112 1113 note one row r of the gemv held in gw, gsc, gx: the adapter's block loop (acc and mag start at 0) 1114 fn gemvrow wb edit uchar 128 xb edit uchar 512 acc edit ulong 2 r ulong k ulong to void 1115 kb = div k 128 1116 so = mul div r 128 kb 1117 put acc 0 0 1118 put acc 1 0 1119 for b ulong kb 1120 ldblk wb xb add mul r k mul b 128 mul b 512 1121 f8gemvblock wb xb gsb add so b acc 1122 end 1123 end 1124 1125 note Q n k w-bytes scale-bytes x-bytes: omega_fp8_gemv_oracle row by row, block by block 1126 fn dogemv to void 1127 wb = new uchar 128 0 1128 xb = new uchar 512 0 1129 acc = new ulong 2 0 1130 n = rdhex 1131 k = rdhex 1132 nb = rdbytes 1133 for i ulong nb 1134 put gw i get tb i 1135 end 1136 nb = rdbytes 1137 for i ulong nb 1138 put gsc i get tb i 1139 end 1140 nb = rdbytes 1141 for i ulong nb 1142 put gx i get tb i 1143 end 1144 ob1 81 1145 for r ulong n 1146 gemvrow wb xb acc r k 1147 ohex get acc 0 1148 ohex get acc 1 1149 end 1150 end 1151 1152 note M cap fmt-bytes nargs args... pool-bytes: omega_gpu_fmt and its va_arg kinds 1153 fn dofmt to void 1154 out = new uchar 1024 170 1155 fm = new uchar 1024 0 1156 args = new ulong 16 0 1157 pl = new uchar 4096 0 1158 kd = new ulong 64 0 1159 cap = rdhex 1160 nb = rdbytes 1161 for i ulong nb 1162 put fm i get tb i 1163 end 1164 na = rdhex 1165 for i ulong na 1166 put args i rdhex 1167 end 1168 nb = rdbytes 1169 for i ulong nb 1170 put pl i get tb i 1171 end 1172 n = gpufmt out cap fm args pl 1173 nk = gpufmtkinds fm kd 1174 ob1 77 1175 ohex n 1176 ob1 32 1177 if eq nk 0 1178 ob1 45 1179 end 1180 for i ulong nk 1181 ob1 hexdig get kd i 1182 end 1183 ob1 32 1184 m = add n 2 1185 if gt m cap 1186 m = cap 1187 end 1188 if eq m 0 1189 ob1 45 1190 end 1191 for i ulong m 1192 ohexb ulong get out i 1193 end 1194 end 1195 1196 fn main to int 1197 while true bound 999999 1198 c = rdcode 1199 if eq c 256 1200 break 1201 end 1202 if eq c 90 1203 doz 1204 end 1205 if eq c 65 1206 ob1 65 1207 ohex hmlog rdhex 1208 end 1209 if eq c 66 1210 ob1 66 1211 ohex hmexp rdhex 1212 end 1213 if eq c 67 1214 ob1 67 1215 b = rdhex 1216 ohex hmpow b rdhex 1217 end 1218 if eq c 68 1219 dosc 1220 end 1221 if eq c 69 1222 ob1 69 1223 ohex hmpow2f sgnl rdhex 1224 end 1225 if eq c 70 1226 dof 1227 end 1228 if and ge c 71 le c 73 1229 dopack c 1230 end 1231 if eq c 78 1232 doscan 1233 end 1234 if eq c 81 1235 dogemv 1236 end 1237 if eq c 77 1238 dofmt 1239 end 1240 ob1 10 1241 end 1242 obflush 1243 ret 0 1244 end