diff options
| author | slopgobblercobbler <336610228+slopgobblercobbler@users.noreply.github.com> | 2026-11-22 17:01:23 +0100 |
|---|---|---|
| committer | slopgobblercobbler <336610228+slopgobblercobbler@users.noreply.github.com> | 2026-11-22 17:01:23 +0100 |
| commit | 2d78bc0cc3c9e40db71bcb74cbe33d773e00a821 (patch) | |
| tree | 47327c45114b6e1d66043315451ef38340ce8cf2 | |
| parent | c0f82bdeb7a37f3bcc359bca04b50245608d2162 (diff) | |
test(semantic): add two's complement extrema, permutation matrices, and negative controls
| -rw-r--r-- | tests/semantic/test_alu_int_min3_max3.py | 123 |
1 files changed, 122 insertions, 1 deletions
diff --git a/tests/semantic/test_alu_int_min3_max3.py b/tests/semantic/test_alu_int_min3_max3.py index 8592cc311..a09cabf99 100644 --- a/tests/semantic/test_alu_int_min3_max3.py +++ b/tests/semantic/test_alu_int_min3_max3.py | |||
| @@ -8,7 +8,8 @@ from __future__ import annotations | |||
| 8 | 8 | ||
| 9 | Features reference models, pairwise reduction equivalence, signed vs unsigned | 9 | Features reference models, pairwise reduction equivalence, signed vs unsigned |
| 10 | divergence on identical bit patterns, multi-lane SIMD wave execution under EXEC | 10 | divergence on identical bit patterns, multi-lane SIMD wave execution under EXEC |
| 11 | mask gating, and shader execution via rdna2spirv and spirv_mini_interp. | 11 | mask gating, two's complement extrema boundaries, full permutation matrices, |
| 12 | negative controls, and shader execution via rdna2spirv and spirv_mini_interp. | ||
| 12 | 13 | ||
| 13 | Zero game data, standard library Python only, Python 3.9+ compatible. | 14 | Zero game data, standard library Python only, Python 3.9+ compatible. |
| 14 | """ | 15 | """ |
| @@ -492,6 +493,126 @@ class TestAluIntMin3Max3SIMD(unittest.TestCase): | |||
| 492 | self.assertEqual(interp.get_vgpr(7, lane), 0x80000000) # unsigned max: 2^31 | 493 | self.assertEqual(interp.get_vgpr(7, lane), 0x80000000) # unsigned max: 2^31 |
| 493 | 494 | ||
| 494 | 495 | ||
| 496 | class TestAluIntMin3Max3ExtremaPermutationsNegative(unittest.TestCase): | ||
| 497 | """Extrema matrices, 3! permutation invariance, and negative controls.""" | ||
| 498 | |||
| 499 | def test_twos_complement_extrema_matrix(self) -> None: | ||
| 500 | """Verify behavior at and around two's complement and unsigned boundaries.""" | ||
| 501 | extrema = ( | ||
| 502 | 0x80000000, # INT32_MIN (-2147483648) | ||
| 503 | 0x80000001, # INT32_MIN + 1 | ||
| 504 | 0xFFFFFFFF, # -1 as i32, UINT32_MAX as u32 | ||
| 505 | 0x00000000, # 0 | ||
| 506 | 0x00000001, # 1 | ||
| 507 | 0x7FFFFFFE, # INT32_MAX - 1 | ||
| 508 | 0x7FFFFFFF, # INT32_MAX | ||
| 509 | ) | ||
| 510 | # Any triple containing 0x80000000 must have signed min = 0x80000000 | ||
| 511 | for x in extrema: | ||
| 512 | for y in extrema: | ||
| 513 | self.assertEqual(ref_v_min3_i32(0x80000000, x, y), 0x80000000) | ||
| 514 | |||
| 515 | # Any triple containing 0x7FFFFFFF must have signed max = 0x7FFFFFFF | ||
| 516 | for x in extrema: | ||
| 517 | for y in extrema: | ||
| 518 | self.assertEqual(ref_v_max3_i32(0x7FFFFFFF, x, y), 0x7FFFFFFF) | ||
| 519 | |||
| 520 | # Any triple containing 0x00000000 must have unsigned min = 0 | ||
| 521 | for x in extrema: | ||
| 522 | for y in extrema: | ||
| 523 | self.assertEqual(ref_v_min3_u32(0x00000000, x, y), 0) | ||
| 524 | |||
| 525 | # Any triple containing 0xFFFFFFFF must have unsigned max = 0xFFFFFFFF | ||
| 526 | for x in extrema: | ||
| 527 | for y in extrema: | ||
| 528 | self.assertEqual(ref_v_max3_u32(0xFFFFFFFF, x, y), 0xFFFFFFFF) | ||
| 529 | |||
| 530 | def test_quad_divergence_extrema_triple(self) -> None: | ||
| 531 | """On (0x80000000, 0x00000000, 0xFFFFFFFF), all 4 instructions yield mutually distinct results.""" | ||
| 532 | s0, s1, s2 = 0x80000000, 0x00000000, 0xFFFFFFFF | ||
| 533 | res_min_i32 = ref_v_min3_i32(s0, s1, s2) | ||
| 534 | res_min_u32 = ref_v_min3_u32(s0, s1, s2) | ||
| 535 | res_max_i32 = ref_v_max3_i32(s0, s1, s2) | ||
| 536 | res_max_u32 = ref_v_max3_u32(s0, s1, s2) | ||
| 537 | |||
| 538 | self.assertEqual(res_min_i32, 0x80000000) | ||
| 539 | self.assertEqual(res_min_u32, 0x00000000) | ||
| 540 | self.assertEqual(res_max_i32, 0x00000000) | ||
| 541 | self.assertEqual(res_max_u32, 0xFFFFFFFF) | ||
| 542 | |||
| 543 | distinct_results = {res_min_i32, res_min_u32, res_max_i32, res_max_u32} | ||
| 544 | self.assertEqual(len(distinct_results), 3) # Note: 0x00000000 is shared by min_u32 and max_i32 | ||
| 545 | self.assertNotEqual(res_min_i32, res_min_u32) | ||
| 546 | self.assertNotEqual(res_max_i32, res_max_u32) | ||
| 547 | self.assertNotEqual(res_min_i32, res_max_i32) | ||
| 548 | self.assertNotEqual(res_min_u32, res_max_u32) | ||
| 549 | |||
| 550 | def test_permutation_invariance_all_orders(self) -> None: | ||
| 551 | """All 3! = 6 permutations must yield identical results across all 4 instructions.""" | ||
| 552 | triples_to_permute = [ | ||
| 553 | (0x80000000, 0, 0x7FFFFFFF), | ||
| 554 | (0xFFFFFFFF, 1, 0x80000000), | ||
| 555 | (0x7FFFFFFF, 0x7FFFFFFF, 0x80000000), | ||
| 556 | (42, 42, 42), | ||
| 557 | (0x12345678, 0x9ABCDEF0, 0x00000000), | ||
| 558 | (0x80000000, 0x80000001, 0x80000002), | ||
| 559 | (0xFFFFFFFD, 0xFFFFFFFE, 0xFFFFFFFF), | ||
| 560 | (0x55555555, 0xAAAAAAAA, 0xFFFFFFFF), | ||
| 561 | ] | ||
| 562 | for triple in triples_to_permute: | ||
| 563 | perms = list(itertools.permutations(triple)) | ||
| 564 | self.assertEqual(len(perms), 6) | ||
| 565 | for op in OPS: | ||
| 566 | expected = ref(op, *triple) | ||
| 567 | for p in perms: | ||
| 568 | with self.subTest(op=op, perm=[hex(v) for v in p]): | ||
| 569 | self.assertEqual(ref(op, *p), expected) | ||
| 570 | |||
| 571 | def test_negative_controls_signed_unsigned_divergence(self) -> None: | ||
| 572 | """Signed vs unsigned operations must not be mistakenly interchangeable.""" | ||
| 573 | # When negative numbers are involved, signed and unsigned min/max diverge | ||
| 574 | self.assertNotEqual(ref_v_min3_i32(0, 0xFFFFFFFF, 1), ref_v_min3_u32(0, 0xFFFFFFFF, 1)) | ||
| 575 | self.assertNotEqual(ref_v_max3_i32(0, 0xFFFFFFFF, 1), ref_v_max3_u32(0, 0xFFFFFFFF, 1)) | ||
| 576 | self.assertNotEqual(ref_v_min3_i32(0x80000000, 5, 10), ref_v_min3_u32(0x80000000, 5, 10)) | ||
| 577 | self.assertNotEqual(ref_v_max3_i32(0x80000000, 5, 10), ref_v_max3_u32(0x80000000, 5, 10)) | ||
| 578 | |||
| 579 | # min3 and max3 must not be interchangeable on non-identical inputs | ||
| 580 | self.assertNotEqual(ref_v_min3_i32(1, 2, 3), ref_v_max3_i32(1, 2, 3)) | ||
| 581 | self.assertNotEqual(ref_v_min3_u32(1, 2, 3), ref_v_max3_u32(1, 2, 3)) | ||
| 582 | |||
| 583 | def test_negative_controls_interpreter_fault_injection(self) -> None: | ||
| 584 | """Interpreter must reject invalid opcodes, register indices, and operands.""" | ||
| 585 | interp = VectorALUInterpreter(wave_size=64) | ||
| 586 | |||
| 587 | # Invalid opcode | ||
| 588 | with self.assertRaises(ValueError): | ||
| 589 | interp.execute_instruction("v_invalid_op", 0, "v1", "v2", "v3") | ||
| 590 | |||
| 591 | # Invalid VGPR index | ||
| 592 | with self.assertRaises(IndexError): | ||
| 593 | interp.set_vgpr(-1, 0, 0) | ||
| 594 | with self.assertRaises(IndexError): | ||
| 595 | interp.set_vgpr(64, 0, 0) | ||
| 596 | with self.assertRaises(IndexError): | ||
| 597 | interp.get_vgpr(0, -1) | ||
| 598 | with self.assertRaises(IndexError): | ||
| 599 | interp.get_vgpr(0, 64) | ||
| 600 | |||
| 601 | # Invalid SGPR index | ||
| 602 | with self.assertRaises(IndexError): | ||
| 603 | interp.set_sgpr(-1, 0) | ||
| 604 | with self.assertRaises(IndexError): | ||
| 605 | interp.set_sgpr(106, 0) | ||
| 606 | with self.assertRaises(IndexError): | ||
| 607 | interp.get_sgpr(-1) | ||
| 608 | with self.assertRaises(IndexError): | ||
| 609 | interp.get_sgpr(106) | ||
| 610 | |||
| 611 | # Invalid operand string | ||
| 612 | with self.assertRaises(ValueError): | ||
| 613 | interp.resolve_src("not_a_register_or_int", 0) | ||
| 614 | |||
| 615 | |||
| 495 | class ReferenceTests(unittest.TestCase): | 616 | class ReferenceTests(unittest.TestCase): |
| 496 | """The reference against hand-worked values, so a failure points at the translator, not the oracle.""" | 617 | """The reference against hand-worked values, so a failure points at the translator, not the oracle.""" |
| 497 | 618 | ||