| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- """Bit-accurate model of door_rate_quantum_builder vs travel×scale oracle."""
- from __future__ import annotations
- import unittest
- PROFILE_SCALE = (
- 64,
- 66,
- 73,
- 84,
- 98,
- 114,
- 132,
- 150,
- 170,
- 188,
- 206,
- 222,
- 236,
- 247,
- 254,
- 256,
- )
- def shift_add_product(travel: int, scale: int, prod_width: int = 21, scale_bits: int = 9) -> int:
- """Independent shift-add oracle; must match travel×scale and RTL * product."""
- if travel < 0 or scale < 0:
- raise ValueError("travel/scale must be non-negative")
- mul_a = travel & ((1 << prod_width) - 1)
- mul_scale = scale & ((1 << scale_bits) - 1)
- mul_acc = 0
- mask = (1 << prod_width) - 1
- for _ in range(scale_bits):
- if mul_scale & 1:
- mul_acc = (mul_acc + mul_a) & mask
- mul_scale >>= 1
- mul_a = (mul_a << 1) & mask
- return mul_acc
- def rtl_product(travel: int, scale: int) -> int:
- """Production builder uses a registered multiply (DSP-friendly)."""
- return travel * scale
- class DoorRateQuantumBuilderTests(unittest.TestCase):
- def test_spotlight_matches_oracle(self) -> None:
- spotlight = [31, 32, 63, 64, 100, 2047, 2048, 4095, 4096, 4999, 5000]
- for travel in spotlight:
- for level, scale in enumerate(PROFILE_SCALE):
- want = travel * scale
- self.assertEqual(shift_add_product(travel, scale), want, f"travel={travel} level={level}")
- self.assertEqual(rtl_product(travel, scale), want, f"rtl travel={travel} level={level}")
- def test_known_truncation_regressions(self) -> None:
- # Cases that the old 13-bit left-shift builder got wrong.
- cases = [
- (5000, 256, 1_280_000),
- (5000, 64, 320_000),
- (4096, 256, 1_048_576),
- (2048, 64, 131_072),
- (32, 256, 8_192),
- ]
- for travel, scale, want in cases:
- self.assertEqual(shift_add_product(travel, scale), want)
- self.assertEqual(rtl_product(travel, scale), want)
- def test_full_exhaustive_2_to_5000(self) -> None:
- mismatches = 0
- zeros_bad = 0
- checked = 0
- for travel in range(2, 5001):
- for scale in PROFILE_SCALE:
- want = travel * scale
- if shift_add_product(travel, scale) != want or rtl_product(travel, scale) != want:
- mismatches += 1
- if want != 0 and rtl_product(travel, scale) == 0:
- zeros_bad += 1
- checked += 1
- self.assertEqual(checked, 79_984)
- self.assertEqual(mismatches, 0)
- self.assertEqual(zeros_bad, 0)
- self.assertLessEqual(5000 * 256, (1 << 21) - 1)
- if __name__ == "__main__":
- unittest.main()
|