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/seed/ng/tests/ir/float-oracle.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 / seed / ng / tests / ir / float-oracle.om` 724 lines ยท 12315 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note Build: cat seed/ng/src/ir.om seed/ng/tests/ir/float-oracle.om | seed/out/omc 3 note Native seeded-omc computations produce the expected bits in a checked IR unit. 4 note Neither ngrun nor any rn_ function is linked here. The only shared unit is the IR writer. 5 note RTZ integer oracle uses native nearest conversion then native convert-back comparison, 6 note independent of ngrun's leading-bit integer encoder. 1024 splitmix64 values per float width. 7 global fo_s uint 4 8 fn fo_const t uint x ulong to uint 9 ret ir_res ir_op get fo_s 0 1 t x 0 10 end 11 fn fo_un op uint t uint im ulong a uint to uint 12 o = ir_op get fo_s 0 op t im 1 13 ir_set o 0 a 14 ret ir_res o 15 end 16 fn fo_expect t uint v uint bits ulong to void 17 it = uint 5 18 if or eq t 13 eq t 4 19 it = 4 20 end 21 a = v 22 if or eq t 13 eq t 14 23 a = fo_un 37 it 0 v 24 end 25 b = fo_const it bits 26 o = ir_op get fo_s 0 30 1 0 2 27 ir_set o 0 a 28 ir_set o 1 b 29 c = ir_res o 30 q = ir_op get fo_s 0 82 0 14 2 31 ir_set q 0 get fo_s 1 32 ir_set q 1 c 33 put fo_s 1 ir_otok q 34 put fo_s 2 add get fo_s 2 1 35 end 36 fn fo_bcast t uint bits ulong to void 37 it = uint 5 38 if eq t 13 39 it = 4 40 end 41 a = fo_const it bits 42 b = fo_un 37 t 0 a 43 c = fo_un 37 it 0 b 44 expect = bitcast ulong bitcast f64 bits 45 if eq t 13 46 expect = ulong bitcast uint bitcast f32 uint bits 47 end 48 fo_expect it c expect 49 end 50 fn fo_special t uint j ulong to ulong 51 if eq t 13 52 if eq j 0 53 ret 0x00000000 54 end 55 if eq j 1 56 ret 0x80000000 57 end 58 if eq j 2 59 ret 0x7f800000 60 end 61 if eq j 3 62 ret 0xff800000 63 end 64 if eq j 4 65 ret 0x7fc00000 66 end 67 if eq j 5 68 ret 0xffc00000 69 end 70 if eq j 6 71 ret 0x7f800001 72 end 73 if eq j 7 74 ret 0xff800001 75 end 76 if eq j 8 77 ret 0x7fffffff 78 end 79 if eq j 9 80 ret 0xffffffff 81 end 82 if eq j 10 83 ret 0x00000001 84 end 85 if eq j 11 86 ret 0x80000001 87 end 88 if eq j 12 89 ret 0x007fffff 90 end 91 if eq j 13 92 ret 0x807fffff 93 end 94 if eq j 14 95 ret 0x00800000 96 end 97 if eq j 15 98 ret 0x80800000 99 end 100 if eq j 16 101 ret 0x7f7fffff 102 end 103 ret 0xff7fffff 104 end 105 if eq j 0 106 ret 0x0000000000000000 107 end 108 if eq j 1 109 ret 0x8000000000000000 110 end 111 if eq j 2 112 ret 0x7ff0000000000000 113 end 114 if eq j 3 115 ret 0xfff0000000000000 116 end 117 if eq j 4 118 ret 0x7ff8000000000000 119 end 120 if eq j 5 121 ret 0xfff8000000000000 122 end 123 if eq j 6 124 ret 0x7ff0000000000001 125 end 126 if eq j 7 127 ret 0xfff0000000000001 128 end 129 if eq j 8 130 ret 0x7fffffffffffffff 131 end 132 if eq j 9 133 ret 0xffffffffffffffff 134 end 135 if eq j 10 136 ret 0x0000000000000001 137 end 138 if eq j 11 139 ret 0x8000000000000001 140 end 141 if eq j 12 142 ret 0x000fffffffffffff 143 end 144 if eq j 13 145 ret 0x800fffffffffffff 146 end 147 if eq j 14 148 ret 0x0010000000000000 149 end 150 if eq j 15 151 ret 0x8010000000000000 152 end 153 if eq j 16 154 ret 0x7fefffffffffffff 155 end 156 ret 0xffefffffffffffff 157 end 158 fn fo_mix state ulong to ulong 159 z = add state 0x9e3779b97f4a7c15 160 z = mul xor z shr z 30 0xbf58476d1ce4e5b9 161 z = mul xor z shr z 27 0x94d049bb133111eb 162 ret xor z shr z 31 163 end 164 fn fo_f2i t uint x f64 to ulong 165 if eq t 2 166 ret ulong uchar x 167 end 168 if eq t 3 169 ret ulong ushort x 170 end 171 if eq t 4 172 ret ulong uint x 173 end 174 if eq t 5 175 ret ulong x 176 end 177 if eq t 6 178 ret bitcast ulong long byte x 179 end 180 if eq t 7 181 ret bitcast ulong long short x 182 end 183 if eq t 8 184 ret bitcast ulong long int x 185 end 186 ret bitcast ulong long x 187 end 188 fn fo_intcase t uint s uint bits ulong to void 189 x = bitcast f64 bits 190 if eq s 13 191 x = f64 bitcast f32 uint bits 192 end 193 a = fo_const s bits 194 v = fo_un 36 t 0 a 195 expected = fo_f2i t x 196 note Compare signed integers as u64 raw bits, with canonical sign extension. 197 u = v 198 if eq t 9 199 u = fo_un 37 5 0 v 200 end 201 if or eq t 2 eq t 6 202 u = fo_un 36 5 0 v 203 end 204 if or eq t 3 eq t 7 205 u = fo_un 36 5 0 v 206 end 207 if or eq t 4 eq t 8 208 u = fo_un 36 5 0 v 209 end 210 if eq t 5 211 u = v 212 end 213 fo_expect 5 u expected 214 end 215 fn fo_round t uint mag ulong z bool to ulong 216 limit = mul f64 shl ulong 1 63 2.0f64 217 if eq t 14 218 v = f64 mag 219 b = bitcast ulong v 220 if z 221 if or ge v limit gt ulong v mag 222 b = sub b 1 223 end 224 end 225 ret b 226 end 227 v = f32 mag 228 b = ulong bitcast uint v 229 if z 230 if or ge f64 v limit gt ulong v mag 231 b = sub b 1 232 end 233 end 234 ret b 235 end 236 fn fo_i2f t uint s uint bits ulong im ulong to void 237 mag = bits 238 sg = ulong 0 239 if ge s 6 240 if lt bitcast long bits 0 241 mag = sub ulong 0 bits 242 sg = 0x8000000000000000 243 if eq t 13 244 sg = 0x80000000 245 end 246 end 247 end 248 expected = or sg fo_round t mag eq and im 3 1 249 a = fo_const s bits 250 v = fo_un 36 t im a 251 fo_expect t v expected 252 end 253 note Independent IEEE binary64 to binary32 toward-zero encoder (review F3). 254 note Uses only sign/exponent/significand integers, not a nearest float or a predecessor correction. 255 fn fo_narrowz bits ulong to ulong 256 sg = and shr bits 32 0x80000000 257 ex = and shr bits 52 2047 258 m = and bits 0x000fffffffffffff 259 if eq ex 2047 260 if eq m 0 261 ret or sg 0x7f800000 262 end 263 ret or sg 0x7fc00000 264 end 265 if eq ex 0 266 ret sg 267 end 268 e = sub long ex 1023 269 if gt e 127 270 ret or sg 0x7f7fffff 271 end 272 if lt e -149 273 ret sg 274 end 275 m = or m 0x0010000000000000 276 if ge e -126 277 frac = and shr m 29 0x7fffff 278 expo = shl ulong add e 127 23 279 ret or sg or expo frac 280 end 281 shift = ulong sub neg e 97 282 ret or sg shr m shift 283 end 284 fn fo_narrow bits ulong im ulong to void 285 x = bitcast f64 bits 286 y = f32 x 287 if eq and im 3 1 288 y = bitcast f32 uint fo_narrowz bits 289 end 290 if ne and im 4 0 291 if and eq fabs f64 y div 1.0f64 0.0f64 lt fabs x div 1.0f64 0.0f64 292 m = bitcast f32 uint 0x7f7fffff 293 y = m 294 if lt x 0.0f64 295 y = neg m 296 end 297 end 298 end 299 note NaN conversion payload is not specified. Compare the resulting classification only. 300 a = fo_const 14 bits 301 v = fo_un 36 13 im a 302 if ne x x 303 fo_nancheck v 304 else 305 fo_expect 13 v ulong bitcast uint y 306 end 307 end 308 fn fo_width t uint to ulong 309 if or eq t 2 eq t 6 310 ret 8 311 end 312 if or eq t 3 eq t 7 313 ret 16 314 end 315 if or eq t 4 eq t 8 316 ret 32 317 end 318 ret 64 319 end 320 fn fo_boundary t uint j ulong to f64 321 w = fo_width t 322 hi = mul f64 shl ulong 1 sub w 1 2.0f64 323 lo = f64 0.0 324 if ge t 6 325 hi = f64 shl ulong 1 sub w 1 326 lo = neg hi 327 end 328 if eq j 0 329 ret sub lo 1.0 330 end 331 if eq j 1 332 ret sub lo 0.5 333 end 334 if eq j 2 335 ret lo 336 end 337 if eq j 3 338 ret add lo 0.5 339 end 340 if eq j 4 341 ret add lo 1.0 342 end 343 if eq j 5 344 ret sub hi 1.0 345 end 346 if eq j 6 347 ret sub hi 0.5 348 end 349 if eq j 7 350 ret hi 351 end 352 if eq j 8 353 ret add hi 0.5 354 end 355 if eq j 9 356 ret add hi 1.0 357 end 358 note Adjacent representable values below and above the high boundary, including 64-bit widths. 359 u = bitcast ulong hi 360 if eq j 10 361 ret bitcast f64 sub u 1 362 end 363 if eq j 11 364 ret bitcast f64 add u 1 365 end 366 u = bitcast ulong lo 367 if eq j 12 368 if eq lo 0.0f64 369 ret bitcast f64 ulong 0x8000000000000001 370 end 371 ret bitcast f64 sub u 1 372 end 373 if eq j 13 374 ret bitcast f64 add u 1 375 end 376 if eq j 14 377 ret -0.5f64 378 end 379 ret 0.5f64 380 end 381 note IR min/max follow OMEGESE fmin/fmax (FMINNM/FMAXNM): native seeded omc is the reference, unguarded. 382 fn fo_mmbits t uint op uint a ulong b ulong to ulong 383 if eq t 13 384 x = bitcast f32 uint a 385 y = bitcast f32 uint b 386 if eq op 24 387 ret ulong bitcast uint fmin x y 388 end 389 ret ulong bitcast uint fmax x y 390 end 391 x = bitcast f64 a 392 y = bitcast f64 b 393 if eq op 24 394 ret bitcast ulong fmin x y 395 end 396 ret bitcast ulong fmax x y 397 end 398 note the contract set {qNaN, sNaN, +0, -0, +inf, -inf, 1.0, smallest denormal}, every ordered pair 399 fn fo_mmval t uint j ulong to ulong 400 if eq t 13 401 if eq j 0 402 ret 0x7fc00000 403 end 404 if eq j 1 405 ret 0x7f800001 406 end 407 if eq j 2 408 ret 0 409 end 410 if eq j 3 411 ret 0x80000000 412 end 413 if eq j 4 414 ret 0x7f800000 415 end 416 if eq j 5 417 ret 0xff800000 418 end 419 if eq j 6 420 ret 0x3f800000 421 end 422 ret 1 423 end 424 if eq j 0 425 ret 0x7ff8000000000000 426 end 427 if eq j 1 428 ret 0x7ff0000000000001 429 end 430 if eq j 2 431 ret 0 432 end 433 if eq j 3 434 ret 0x8000000000000000 435 end 436 if eq j 4 437 ret 0x7ff0000000000000 438 end 439 if eq j 5 440 ret 0xfff0000000000000 441 end 442 if eq j 6 443 ret 0x3ff0000000000000 444 end 445 ret 1 446 end 447 fn fo_mmset t uint to void 448 for i ulong 8 449 for j ulong 8 450 a = fo_mmval t i 451 b = fo_mmval t j 452 fo_mmcase t 24 a b 453 fo_mmcase t 25 a b 454 end 455 end 456 end 457 fn fo_nancheck v uint to void 458 o = ir_op get fo_s 0 31 1 0 2 459 ir_set o 0 v 460 ir_set o 1 v 461 q = ir_op get fo_s 0 82 0 14 2 462 ir_set q 0 get fo_s 1 463 ir_set q 1 ir_res o 464 put fo_s 1 ir_otok q 465 put fo_s 2 add get fo_s 2 1 466 end 467 fn fo_quietcheck t uint v uint to void 468 it = uint 5 469 quiet = ulong 0x0008000000000000 470 if eq t 13 471 it = 4 472 quiet = 0x00400000 473 end 474 b = fo_un 37 it 0 v 475 mask = fo_const it quiet 476 o = ir_op get fo_s 0 16 it 0 2 477 ir_set o 0 b 478 ir_set o 1 mask 479 fo_expect it ir_res o quiet 480 end 481 fn fo_mmexpect t uint v uint expected ulong to void 482 x = bitcast f64 expected 483 if eq t 13 484 x = f64 bitcast f32 uint expected 485 end 486 if ne x x 487 fo_nancheck v 488 fo_quietcheck t v 489 else 490 fo_expect t v expected 491 end 492 end 493 fn fo_mmcase t uint op uint a ulong b ulong to void 494 expected = fo_mmbits t op a b 495 va = fo_const t a 496 vb = fo_const t b 497 o = ir_op get fo_s 0 op t 0 2 498 ir_set o 0 va 499 ir_set o 1 vb 500 fo_mmexpect t ir_res o expected 501 end 502 fn fo_mmmatrix t uint to void 503 for i ulong 18 504 for j ulong 18 505 a = fo_special t i 506 b = fo_special t j 507 fo_mmcase t 24 a b 508 fo_mmcase t 25 a b 509 end 510 end 511 end 512 fn fo_fmexp t uint a ulong b ulong c ulong fused bool to ulong 513 if eq t 13 514 x = bitcast f32 uint a 515 y = bitcast f32 uint b 516 z = bitcast f32 uint c 517 if fused 518 ret ulong bitcast uint fma x y z 519 end 520 p = mul x y 521 ret ulong bitcast uint add p z 522 end 523 x = bitcast f64 a 524 y = bitcast f64 b 525 z = bitcast f64 c 526 if fused 527 ret bitcast ulong fma x y z 528 end 529 p = mul x y 530 ret bitcast ulong add p z 531 end 532 fn fo_fmacase t uint a ulong b ulong c ulong to void 533 expected = fo_fmexp t a b c true 534 unfused = fo_fmexp t a b c false 535 if ne expected unfused 536 x = bitcast f64 expected 537 y = bitcast f64 unfused 538 if eq t 13 539 x = f64 bitcast f32 uint expected 540 y = f64 bitcast f32 uint unfused 541 end 542 if and eq x x eq y y 543 put fo_s 3 add get fo_s 3 1 544 end 545 end 546 va = fo_const t a 547 vb = fo_const t b 548 vc = fo_const t c 549 o = ir_op get fo_s 0 46 t 0 3 550 ir_set o 0 va 551 ir_set o 1 vb 552 ir_set o 2 vc 553 fo_expect t ir_res o expected 554 end 555 global fo_num uchar 24 556 text fo_msg0 "FLOATORACLE checks=" 557 text fo_msg1 " fused_diff=" 558 text fo_msg2 "\n" 559 fn fo_print n ulong to void 560 x = n 561 i = ulong 24 562 while true bound 24 563 i = sub i 1 564 put fo_num i uchar add 48 rem x 10 565 x = div x 10 566 if eq x 0 567 k = write 2 fo_num i sub 24 i 568 ret 569 end 570 end 571 end 572 fn main to int 573 ir_init 574 f = ir_fnew 2 0 8 575 put fo_s 0 ir_blk f 576 put fo_s 1 ir_ftok f 577 for k ulong 2 578 s = uint 13 579 if eq k 1 580 s = 14 581 end 582 state = ulong 0x6e6772756e 583 for j ulong 1042 584 bits = fo_mix state 585 state = bits 586 if eq s 13 587 bits = and bits 0xffffffff 588 end 589 if lt j 18 590 bits = fo_special s j 591 end 592 fo_bcast s bits 593 fb = fo_mix bits 594 fc = fo_mix fb 595 if lt j 18 596 fb = 0x3ff0000000000000 597 fc = 0 598 if eq s 13 599 fb = 0x3f800000 600 end 601 end 602 if eq s 13 603 fb = and fb 0xffffffff 604 fc = and fc 0xffffffff 605 end 606 fo_fmacase s bits fb fc 607 fo_mmcase s 24 bits fb 608 fo_mmcase s 25 bits fb 609 for d uint 8 610 fo_intcase add d 2 s bits 611 end 612 if eq s 14 613 for m ulong 4 614 im = m 615 if ge m 2 616 im = add m 2 617 end 618 fo_narrow bits im 619 end 620 end 621 end 622 end 623 fo_mmmatrix 13 624 fo_mmmatrix 14 625 fo_mmset 13 626 fo_mmset 14 627 fo_fmacase 13 0x3f800001 0x3f7ffffe 0xbf800000 628 fo_fmacase 14 0x3ff0000000000001 0x3feffffffffffffe 0xbff0000000000000 629 note Exact witnesses (Wolfram, bay 7 review of `26f67fbb`): (1+2^-23)(1-2^-23)-1 = -2^-46 is 0xa8800000 and 630 note (1+2^-52)(1-2^-52)-1 = -2^-104 is 0xb970000000000000. 0x3f7ff001 * 0x33800800 + 1 = 1 + 2^-24 + 2^-60: 631 note fused f32 gives 0x3f800001; unfused, and f64 fma then narrowing to f32 (double rounding), give 0x3f800000. 632 note Native omc must reproduce the exact values, so its fma is not taken on trust; exit 3 otherwise. 633 fo_fmacase 13 0x3f7ff001 0x33800800 0x3f800000 634 if ne fo_fmexp 13 0x3f800001 0x3f7ffffe 0xbf800000 true 0xa8800000 635 ret 3 636 end 637 if ne fo_fmexp 14 0x3ff0000000000001 0x3feffffffffffffe 0xbff0000000000000 true 0xb970000000000000 638 ret 3 639 end 640 if ne fo_fmexp 13 0x3f7ff001 0x33800800 0x3f800000 true 0x3f800001 641 ret 3 642 end 643 if ne fo_fmexp 13 0x3f7ff001 0x33800800 0x3f800000 false 0x3f800000 644 ret 3 645 end 646 for d uint 8 647 t = add d 2 648 for j ulong 16 649 x = fo_boundary t j 650 fo_intcase t 14 bitcast ulong x 651 fo_intcase t 13 ulong bitcast uint f32 x 652 end 653 w = fo_width t 654 mx = sub ulong 0 1 655 if lt w 64 656 mx = sub shl ulong 1 w 1 657 end 658 if ge t 6 659 mx = sub shl ulong 1 sub w 1 1 660 end 661 for j ulong 6 662 v = mx 663 if eq j 1 664 v = sub mx 1 665 end 666 if eq j 2 667 v = 0 668 end 669 if eq j 3 670 v = 1 671 end 672 if ge j 4 673 if ge t 6 674 v = sub ulong 0 shl ulong 1 sub w 1 675 if eq j 5 676 v = add v 1 677 end 678 end 679 end 680 for k uint 2 681 ft = add k 13 682 for m ulong 4 683 im = m 684 if ge m 2 685 im = add m 2 686 end 687 fo_i2f ft t v im 688 end 689 end 690 end 691 end 692 note Precision boundaries across every possible u64 exponent, both float widths and modes. 693 for e ulong 64 694 p = shl ulong 1 e 695 for j ulong 3 696 v = p 697 if eq j 1 698 v = sub p 1 699 end 700 if eq j 2 701 v = add p 1 702 end 703 for k uint 2 704 for m ulong 2 705 fo_i2f add k 13 5 v m 706 end 707 end 708 end 709 end 710 zero = fo_const 8 0 711 o = ir_op get fo_s 0 63 0 0 2 712 ir_set o 0 get fo_s 1 713 ir_set o 1 zero 714 k = write 2 fo_msg0 0 len fo_msg0 715 fo_print ulong get fo_s 2 716 k = write 2 fo_msg1 0 len fo_msg1 717 fo_print ulong get fo_s 3 718 k = write 2 fo_msg2 0 len fo_msg2 719 n = ir_wr 720 if eq ar_out 1 1 0 n 0 721 ret 1 722 end 723 ret 0 724 end