Repository navigation
Fix signed_tree_multiplier for the most negative operand - #520
anirudh12032008 wants to merge 1 commit into
Conversation
The magnitude of the most negative value, like -8 in 4 bits, is 2**(n-1), which needs all n bits. signed_tree_multiplier dropped the top bit of each magnitude, so any product with such an operand was 0.
fdxmw
left a comment
There was a problem hiding this comment.
Thank you for finding and fixing this bug!
| ).zero_extended(len(A) + len(B)) | ||
| # `a` and `b` are magnitudes. Keep their top bits: the magnitude of the most | ||
| # negative value, like -8 in 4 bits, is 2**(n-1), which needs all n bits. | ||
| res = tree_multiplier(a, b, reducer=reducer, adder_func=adder_func).zero_extended( |
There was a problem hiding this comment.
Ideally, signed_mult and signed_tree_multiplier should work the same way, for consistency. signed_mult's approach seems much less complex than this pre-existing approach, so I'd recommend trying signed_mult's approach here (sign extend the operands then truncate the result), rather than patching up this code. This is all assuming signed_mult's implementation is correct :)
Let me know if you'd like to try this, if not we can just merge your PR and I can experiment with this later.
| def test_trivial_case_3(self): | ||
| self.mult_t_base(3, 4) | ||
|
|
||
| def test_most_negative_operand(self): |
There was a problem hiding this comment.
It looks like this exhaustively tests all combinations of operands, not just the most negative ones, so maybe call this test_all_operands or something similar?
| product = pyrtl.Output(name="product") | ||
| product <<= multipliers.signed_tree_multiplier(a, b) | ||
| sim = pyrtl.Simulation() | ||
| for x in range(-(2 ** (len_a - 1)), 2 ** (len_a - 1)): |
There was a problem hiding this comment.
Consider factoring out repeated code to improve readability, maybe something like
min_a = -(2 ** (len_a - 1))
min_b = -(2 ** (len_b - 1))
for x in range(min_a, -min_a):
for y in range(min_b, -min_b):
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## development #520 +/- ##
===========================================
Coverage 93.8% 93.8%
===========================================
Files 30 30
Lines 7178 7178
===========================================
Hits 6729 6729
Misses 449 449 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Fixes #519.
signed_tree_multipliertakes the magnitude of each operand and then multipliesa[:-1]andb[:-1]. The magnitude of the most negative value (-8 in 4 bits) is 2**(n-1), which needs all n bits, so the top bit was dropped and the product came out as 0.The fix multiplies the full magnitudes. Added
test_most_negative_operand, which checks every signed product for 2x2, 3x4 and 4x3 bit widths (fails before, passes after). The existing test never used the most negative value.pytest tests(1377 passed),ruff format --diffandruff checkare clean.