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/gpu/nvidia/om/numeric/numeric.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 / gpu / nvidia / om / numeric / numeric.om` 816 lines ยท 14192 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 2 note Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 note 4 note OMEGA-NUMERIC reference tier in Omega: the pure scalar definitions of 5 note src/omega_numeric.c (E1 scalar contract, Omega div/sqrt/exp/log, compare 6 note classes, launch shape). Every FP32/FP16/BF16 value travels as raw bits in 7 note a ulong. The C file keeps the pointer loops, the asm realization tier and 8 note the registry tables; CUDAPP_MIGRATION_PENDING_C_NUMERIC selects the old C 9 note bodies as the differential oracle. One exported dispatcher, numeric_ref, 10 note because a hosted unit exports only its last function. 11 const M32 ulong `4294967295` 12 const SGN ulong `2147483648` 13 const ABS ulong `2147483647` 14 const INF ulong `2139095040` 15 const QNAN ulong `2143289344` 16 const QBIT ulong `4194304` 17 note CANON is the one NaN every float-producing op returns: PTX leaves f32 NaN results 18 note unspecified and the GPU produces 0x7fffffff, so no host NaN propagation leaks out. 19 const CANON ulong `2147483647` 20 const TOP ulong `9223372036854775808` 21 fn isnan32 u ulong to bool 22 ret gt and u ABS INF 23 end 24 fn canon u ulong to ulong 25 if isnan32 u 26 ret CANON 27 end 28 ret u 29 end 30 fn issnan u ulong to bool 31 ret and isnan32 u eq and u QBIT 0 32 end 33 fn iszero32 u ulong to bool 34 ret eq and u ABS 0 35 end 36 fn msb64 v ulong to ulong 37 note v is nonzero, so the leading one bit is found within 64 passes 38 p = ulong 63 39 while eq and shr v p 1 0 bound 64 40 p = sub p 1 41 end 42 ret p 43 end 44 note Round sig * 2^e2 (s <= 0: exact shift up) to the lsb chosen by rndfmt. 45 fn rndsh sig ulong s long p long to ulong 46 if le s 0 47 ret shl sig ulong neg s 48 end 49 if gt s add p 1 50 ret 0 51 end 52 u = ulong s 53 mant = ulong 0 54 if lt u 64 55 mant = shr sig u 56 end 57 g = and shr sig sub u 1 1 58 r = and sig sub shl ulong 1 sub u 1 1 59 if and eq g 1 or ne r 0 eq and mant 1 1 60 mant = add mant 1 61 end 62 ret mant 63 end 64 note ref_round_small: sig * 2^e2 (exact, sig > 0) to a format with man 65 note explicit bits, bias bias and all-ones exponent emaxf; RNE, subnormals 66 note kept, overflow to infinity. Returns the bits without the sign. A sticky 67 note amount below sig is passed as sig*2+1 at e2-1. 68 fn rndfmt sig ulong e2 long man long bias long emaxf ulong to ulong 69 p = long msb64 sig 70 be = add add p e2 bias 71 inf = shl emaxf ulong man 72 if ge be long emaxf 73 ret inf 74 end 75 s = sub sub sub 1 bias man e2 76 if ge be 1 77 s = sub p man 78 end 79 mant = rndsh sig s p 80 bits = mant 81 if ge be 1 82 bits = add shl ulong sub be 1 ulong man mant 83 end 84 if ge bits inf 85 ret inf 86 end 87 ret bits 88 end 89 fn rnd32 sig ulong e2 long to ulong 90 ret rndfmt sig e2 23 127 255 91 end 92 note ref_split: finite FP32 magnitude as m * 2^e, m below 2^24. 93 fn mant32 x ulong to ulong 94 f = and x `8388607` 95 if ne and shr x 23 255 0 96 ret or f `8388608` 97 end 98 ret f 99 end 100 fn expo32 x ulong to long 101 be = and shr x 23 255 102 if ne be 0 103 ret sub long be 150 104 end 105 ret long -149 106 end 107 note omega_unpack: finite nonzero magnitude as m * 2^e, m in [2^23, 2^24). 108 fn unpm x ulong to ulong 109 m = mant32 x 110 while eq and m `8388608` 0 bound 24 111 m = shl m 1 112 end 113 ret m 114 end 115 fn unpe x ulong to long 116 m = mant32 x 117 e = expo32 x 118 while eq and m `8388608` 0 bound 24 119 m = shl m 1 120 e = sub e 1 121 end 122 ret e 123 end 124 note Order key of a non-NaN FP32: -0 and +0 share a key. 125 fn okey u ulong to ulong 126 if ne and u SGN 0 127 ret sub SGN and u ABS 128 end 129 ret add SGN u 130 end 131 fn fmin32 a ulong b ulong to ulong 132 if isnan32 a 133 ret canon b 134 end 135 if isnan32 b 136 ret a 137 end 138 if and iszero32 a iszero32 b 139 ret and or a b SGN 140 end 141 if lt okey a okey b 142 ret a 143 end 144 ret b 145 end 146 fn fmax32 a ulong b ulong to ulong 147 if isnan32 a 148 ret canon b 149 end 150 if isnan32 b 151 ret a 152 end 153 if and iszero32 a iszero32 b 154 ret and and a b SGN 155 end 156 if gt okey a okey b 157 ret a 158 end 159 ret b 160 end 161 fn fsetpge a ulong b ulong to ulong 162 if or isnan32 a isnan32 b 163 ret 0 164 end 165 if ge okey a okey b 166 ret 1 167 end 168 ret 0 169 end 170 note ref_order: PL 1, PE 2, PG 4, PU 8. 171 fn order a ulong b ulong to ulong 172 if or isnan32 a isnan32 b 173 ret 8 174 end 175 ka = okey a 176 kb = okey b 177 if lt ka kb 178 ret 1 179 end 180 if gt ka kb 181 ret 4 182 end 183 ret 2 184 end 185 fn fsetpred mask ulong a ulong b ulong to ulong 186 if ne and mask order a b 0 187 ret 1 188 end 189 ret 0 190 end 191 fn fadd32 a ulong b ulong to ulong 192 ret canon ulong bitcast uint add bitcast f32 uint a bitcast f32 uint b 193 end 194 fn fsub32 a ulong b ulong to ulong 195 ret canon ulong bitcast uint sub bitcast f32 uint a bitcast f32 uint b 196 end 197 fn fmul32 a ulong b ulong to ulong 198 ret canon ulong bitcast uint mul bitcast f32 uint a bitcast f32 uint b 199 end 200 fn i2f32 a ulong to ulong 201 if eq a 0 202 ret 0 203 end 204 if ne and a SGN 0 205 ret or SGN rnd32 sub ulong `4294967296` a 0 206 end 207 ret rnd32 a 0 208 end 209 fn u2f32 a ulong to ulong 210 if eq a 0 211 ret 0 212 end 213 ret rnd32 a 0 214 end 215 note Integer part and fraction class of |x| (finite, nonzero): ip*4 + frac, 216 note frac 0 none, 1 below half, 2 half, 3 above; TOP when |x| >= 2^40. 217 fn ipfrac u ulong to ulong 218 m = mant32 u 219 e = expo32 u 220 if ge e 0 221 if gt e 16 222 ret TOP 223 end 224 ret shl shl m ulong e 2 225 end 226 if ge neg e 40 227 ret 1 228 end 229 sh = ulong neg e 230 ip = shr m sh 231 rm = and m sub shl ulong 1 sh 1 232 h = shl ulong 1 sub sh 1 233 fr = ulong 3 234 if eq rm 0 235 fr = 0 236 else 237 if lt rm h 238 fr = 1 239 else 240 if eq rm h 241 fr = 2 242 end 243 end 244 end 245 ret or shl ip 2 fr 246 end 247 fn satv ng bool uns bool to ulong 248 if uns 249 if ng 250 ret 0 251 end 252 ret M32 253 end 254 if ng 255 ret SGN 256 end 257 ret ABS 258 end 259 fn clampi ng bool mag ulong uns bool to ulong 260 if uns 261 if ng 262 ret 0 263 end 264 if gt mag M32 265 ret M32 266 end 267 ret mag 268 end 269 if ng 270 if gt mag SGN 271 ret SGN 272 end 273 ret and sub 0 mag M32 274 end 275 if gt mag ABS 276 ret ABS 277 end 278 ret mag 279 end 280 note ref_to_int: mode 0 RTZ, 1 floor, 2 ceil, 3 RNE; saturating to int32 281 note (uns false) or uint32 (uns true); NaN -> 0. Result: the 32 result bits. 282 fn toint u ulong mode ulong uns bool to ulong 283 if isnan32 u 284 ret 0 285 end 286 ng = ne and u SGN 0 287 x = and u ABS 288 if eq x INF 289 ret satv ng uns 290 end 291 if eq x 0 292 ret 0 293 end 294 pf = ipfrac x 295 if eq pf TOP 296 ret satv ng uns 297 end 298 ret clampi ng intrnd pf mode ng uns 299 end 300 note Integer magnitude of ipfrac pf after rounding mode mode (ng: negative). 301 fn intrnd pf ulong mode ulong ng bool to ulong 302 ip = shr pf 2 303 fr = and pf 3 304 up = false 305 if eq mode 1 306 up = and ng ne fr 0 307 end 308 if eq mode 2 309 up = and not ng ne fr 0 310 end 311 if eq mode 3 312 up = or eq fr 3 and eq fr 2 eq and ip 1 1 313 end 314 if up 315 ret add ip 1 316 end 317 ret ip 318 end 319 note ref_narrow: FP32 to a 16-bit format (man bits, bias, emaxf). 320 fn narrow u ulong man ulong bias long emaxf ulong to ulong 321 sign = and shr u 16 32768 322 be = and shr u 23 255 323 f = and u `8388607` 324 inf = shl emaxf man 325 if eq be 255 326 if eq f 0 327 ret or sign inf 328 end 329 ret or or or sign inf shl ulong 1 sub man 1 shr f sub 23 man 330 end 331 if and eq be 0 eq f 0 332 ret sign 333 end 334 ret or sign rndfmt mant32 u expo32 u long man bias emaxf 335 end 336 fn f16to32 h ulong to ulong 337 sign = shl and h 32768 16 338 be = and shr h 10 31 339 f = and h 1023 340 if eq be 31 341 if eq f 0 342 ret or sign INF 343 end 344 ret or or sign INF or QBIT shl f 13 345 end 346 if eq be 0 347 if eq f 0 348 ret sign 349 end 350 note f >= 1 reaches bit 10 within 10 shifts; e is the biased exponent 351 e = ulong 113 352 while eq and f 1024 0 bound 10 353 f = shl f 1 354 e = sub e 1 355 end 356 ret or or sign shl e 23 shl and f 1023 13 357 end 358 ret or or sign shl add be 112 23 shl f 13 359 end 360 fn bf16to32 h ulong to ulong 361 ret shl and h 65535 16 362 end 363 note omega_ref_ffma_int tail: X is the term with the leading bit at or 364 note above Y's. X is moved to bit 62; lost bits of Y are ORed into bit 0. 365 fn fmaord X ulong ex long sx ulong Y ulong ey long sy ulong to ulong 366 up = sub 62 long msb64 X 367 xs = shl X ulong up 368 xe = sub ex up 369 d = sub xe ey 370 ys = Y 371 if lt d 0 372 ys = shl Y ulong neg d 373 else 374 if ge d 64 375 ys = 1 376 else 377 if gt d 0 378 ys = shr Y ulong d 379 if ne and Y sub shl ulong 1 ulong d 1 0 380 ys = or ys 1 381 end 382 end 383 end 384 end 385 m = add xs ys 386 s = sx 387 if ne sx sy 388 if ge xs ys 389 m = sub xs ys 390 else 391 m = sub ys xs 392 s = sy 393 end 394 end 395 if eq m 0 396 ret 0 397 end 398 ret or s rnd32 m xe 399 end 400 fn fmaadd X ulong ex long sx ulong Y ulong ey long sy ulong to ulong 401 if gt add long msb64 Y ey add long msb64 X ex 402 ret fmaord Y ey sy X ex sx 403 end 404 ret fmaord X ex sx Y ey sy 405 end 406 fn fmafin xa ulong xb ulong xc ulong ps ulong cs ulong to ulong 407 mp = mul mant32 xa mant32 xb 408 ep = add expo32 xa expo32 xb 409 if eq xc 0 410 ret or ps rnd32 mp ep 411 end 412 ret fmaadd mp ep ps mant32 xc expo32 xc cs 413 end 414 fn ffmaint a ulong b ulong c ulong to ulong 415 ps = and xor a b SGN 416 cs = and c SGN 417 xa = and a ABS 418 xb = and b ABS 419 xc = and c ABS 420 if or or gt xa INF gt xb INF gt xc INF 421 ret QNAN 422 end 423 pinf = or eq xa INF eq xb INF 424 pz = or eq xa 0 eq xb 0 425 if and pinf pz 426 ret QNAN 427 end 428 if pinf 429 if and eq xc INF ne cs ps 430 ret QNAN 431 end 432 ret or ps INF 433 end 434 if eq xc INF 435 ret c 436 end 437 if pz 438 if ne xc 0 439 ret c 440 end 441 ret and ps cs 442 end 443 ret fmafin xa xb xc ps cs 444 end 445 note AArch64 FMADD (FPCR DN=0): FPProcessNaNs3(addend c, a, b), signaling 446 note NaNs first, then a quiet addend with inf*0 gives the default NaN. 447 fn ffmahw a ulong b ulong c ulong to ulong 448 if issnan c 449 ret or c QBIT 450 end 451 if issnan a 452 ret or a QBIT 453 end 454 if issnan b 455 ret or b QBIT 456 end 457 if isnan32 c 458 xa = and a ABS 459 xb = and b ABS 460 if or and eq xa INF eq xb 0 and eq xa 0 eq xb INF 461 ret QNAN 462 end 463 ret c 464 end 465 if isnan32 a 466 ret a 467 end 468 if isnan32 b 469 ret b 470 end 471 ret ffmaint a b c 472 end 473 fn divfin ax ulong ay ulong sign ulong to ulong 474 my = unpm ay 475 rm = unpm ax 476 q = ulong 0 477 for i ulong 27 478 q = shl q 1 479 if ge rm my 480 rm = sub rm my 481 q = or q 1 482 end 483 rm = shl rm 1 484 end 485 st = ulong 0 486 if ne rm 0 487 st = 1 488 end 489 ret or sign rnd32 or shl q 1 st sub sub sub unpe ax unpe ay 26 1 490 end 491 fn mdiv x ulong y ulong to ulong 492 if or isnan32 x isnan32 y 493 ret QNAN 494 end 495 sign = and xor x y SGN 496 ax = and x ABS 497 ay = and y ABS 498 if eq ay 0 499 if eq ax 0 500 ret QNAN 501 end 502 ret or sign INF 503 end 504 if eq ax 0 505 ret sign 506 end 507 if eq ax INF 508 if eq ay INF 509 ret QNAN 510 end 511 ret or sign INF 512 end 513 if eq ay INF 514 ret sign 515 end 516 ret divfin ax ay sign 517 end 518 fn sqfin mm ulong e long to ulong 519 op = shl mm 28 520 s = ulong 0 521 one = shl ulong 1 62 522 while gt one op bound 32 523 one = shr one 2 524 end 525 while ne one 0 bound 32 526 t = add s one 527 if ge op t 528 op = sub op t 529 s = add shr s 1 one 530 else 531 s = shr s 1 532 end 533 one = shr one 2 534 end 535 st = ulong 0 536 if ne op 0 537 st = 1 538 end 539 ret rnd32 or shl s 1 st sub sub div e 2 14 1 540 end 541 fn msqrt x ulong to ulong 542 if isnan32 x 543 ret QNAN 544 end 545 if iszero32 x 546 ret x 547 end 548 if ne and x SGN 0 549 ret QNAN 550 end 551 if eq x INF 552 ret INF 553 end 554 mm = unpm x 555 e = unpe x 556 if ne rem e 2 0 557 mm = shl mm 1 558 e = sub e 1 559 end 560 ret sqfin mm e 561 end 562 note AArch64 FSQRT (DN=0): a NaN operand is returned quiet. 563 fn ieeesqrt x ulong to ulong 564 if isnan32 x 565 ret or x QBIT 566 end 567 ret msqrt x 568 end 569 fn ieeediv x ulong y ulong to ulong 570 ret ulong bitcast uint div bitcast f32 uint x bitcast f32 uint y 571 end 572 fn f32of u ulong to f32 573 ret bitcast f32 uint u 574 end 575 fn bitsof v f32 to ulong 576 ret ulong bitcast uint v 577 end 578 fn pow2 k long to f32 579 ret bitcast f32 uint shl add k 127 23 580 end 581 fn expscal poly f32 k long to ulong 582 if gt k 127 583 ret bitsof mul mul poly f32of `2130706432` pow2 sub k 127 584 end 585 if lt k long -126 586 ret bitsof mul mul poly pow2 add k 100 pow2 long -100 587 end 588 ret bitsof mul poly pow2 k 589 end 590 fn exppoly x f32 k long to ulong 591 fk = f32 k 592 r = sub sub x mul fk f32of `1060205056` mul fk f32of `901758606` 593 p = mul r add f32of `1026206379` mul r f32of `1007192201` 594 p = mul r add f32of `1042983595` p 595 p = mul r add f32of `1056964608` p 596 poly = add f32of `1065353216` mul r add f32of `1065353216` p 597 ret expscal poly k 598 end 599 fn mexp u ulong to ulong 600 if isnan32 u 601 ret QNAN 602 end 603 x = f32of u 604 if lt x f32of `3268411392` 605 ret 0 606 end 607 if gt x f32of `1118925336` 608 ret INF 609 end 610 prod = mul x f32of `1069066811` 611 k = int 0 612 if ge prod f32of 0 613 k = int add prod f32of `1056964608` 614 else 615 k = int sub prod f32of `1056964608` 616 end 617 ret exppoly x long k 618 end 619 fn logpoly m f32 e long to ulong 620 z = f32of mdiv bitsof sub m f32of `1065353216` bitsof add m f32of `1065353216` 621 z2 = mul z z 622 poly = add f32of `1065353216` mul z2 add f32of `1051372203` mul z2 add f32of `1045220557` mul z2 f32of `1041385765` 623 lnm = mul mul f32of `1073741824` z poly 624 ret bitsof add mul f32 e f32of `1060205080` lnm 625 end 626 fn mlog u ulong to ulong 627 if isnan32 u 628 ret QNAN 629 end 630 if lt f32of u f32of 0 631 ret QNAN 632 end 633 if iszero32 u 634 ret `4286578688` 635 end 636 if eq u INF 637 ret INF 638 end 639 x = u 640 ea = long 0 641 if eq and shr u 23 255 0 642 x = bitsof mul f32of u f32of `1258291200` 643 ea = long -23 644 end 645 e = add sub long and shr x 23 255 127 ea 646 m = f32of or and x `8388607` `1065353216` 647 if gt m f32of `1068827891` 648 m = mul m f32of `1056964608` 649 e = add e 1 650 end 651 ret logpoly m e 652 end 653 fn bitseq a ulong b ulong to ulong 654 if and isnan32 a isnan32 b 655 ret 1 656 end 657 if eq a b 658 ret 1 659 end 660 ret 0 661 end 662 note omega_numeric_compare_equal: mode 0 BIT_EXACT, 1 INT_EXACT, 663 note 2 SEED_BOUND, 3 F16_BITS, 4 BF16_BITS. 664 fn b2u v bool to ulong 665 if v 666 ret 1 667 end 668 ret 0 669 end 670 fn cmpeq mode ulong ex ulong got ulong to ulong 671 if eq mode 0 672 ret bitseq ex got 673 end 674 if eq mode 1 675 ret b2u eq ex got 676 end 677 if or eq mode 3 eq mode 4 678 if or ne shr ex 16 0 ne shr got 16 0 679 ret 0 680 end 681 inf = ulong 31744 682 if eq mode 4 683 inf = 32640 684 end 685 en = gt and ex 32767 inf 686 gn = gt and got 32767 inf 687 if or en gn 688 ret b2u and en gn 689 end 690 ret b2u eq ex got 691 end 692 ret 0 693 end 694 fn levels n ulong to ulong 695 if eq n 0 696 ret 0 697 end 698 l = ulong 1 699 cap = ulong 32 700 while lt cap n bound 13 701 if gt cap `576460752303423487` 702 cap = sub ulong 0 1 703 else 704 cap = mul cap 32 705 end 706 l = add l 1 707 end 708 ret l 709 end 710 fn shape cnt ulong to ulong 711 g = and div add cnt 63 64 M32 712 if eq g 0 713 ret 1 714 end 715 ret g 716 end 717 note Float constants are written as the bits of the C `f` literals, because 718 note the pinned decimal conversion is not always correctly rounded (omega-mv#1024). 719 note Dispatcher. Codes are the OMEGA_NUMERIC_OM_* values of src/omega_numeric.c. 720 fn numeric_ref op ulong a ulong b ulong c ulong to ulong 721 if eq op 0 722 ret fadd32 a b 723 end 724 if eq op 1 725 ret fsub32 a b 726 end 727 if eq op 2 728 ret fmul32 a b 729 end 730 if eq op 3 731 ret canon ffmahw a b c 732 end 733 if eq op 4 734 ret canon ffmaint a b c 735 end 736 if eq op 5 737 ret fmin32 a b 738 end 739 if eq op 6 740 ret fmax32 a b 741 end 742 if eq op 7 743 ret fsetpge a b 744 end 745 if eq op 8 746 ret fsetpred c a b 747 end 748 if eq op 9 749 ret i2f32 a 750 end 751 if eq op 10 752 ret u2f32 a 753 end 754 if eq op 11 755 ret toint a 0 false 756 end 757 if eq op 12 758 ret toint a 1 false 759 end 760 if eq op 13 761 ret toint a 2 false 762 end 763 if eq op 14 764 ret toint a 3 false 765 end 766 if eq op 15 767 ret toint a 0 true 768 end 769 if eq op 16 770 ret narrow a 10 15 31 771 end 772 if eq op 17 773 ret narrow a 7 127 255 774 end 775 if eq op 18 776 ret f16to32 a 777 end 778 if eq op 19 779 ret bf16to32 a 780 end 781 if eq op 20 782 ret canon mdiv a b 783 end 784 if eq op 21 785 ret canon msqrt a 786 end 787 if eq op 22 788 ret canon mexp a 789 end 790 if eq op 23 791 ret canon mlog a 792 end 793 if eq op 24 794 ret canon ieeediv a b 795 end 796 if eq op 25 797 ret canon ieeesqrt a 798 end 799 if eq op 26 800 ret and sub `2129859008` a M32 801 end 802 if eq op 27 803 ret bitseq a b 804 end 805 if eq op 28 806 ret cmpeq a b c 807 end 808 if eq op 29 809 ret levels a 810 end 811 if eq op 30 812 ret shape a 813 end 814 note unknown codes never come from the C wrappers 815 ret 0 816 end