Skip to content

Fix signed_tree_multiplier for the most negative operand - #520

Open
anirudh12032008 wants to merge 1 commit into
UCSBarchlab:developmentfrom
anirudh12032008:fix-signed-tree-mult-most-negative
Open

anirudh12032008 wants to merge 1 commit into
UCSBarchlab:developmentfrom
anirudh12032008:fix-signed-tree-mult-most-negative

Conversation

@anirudh12032008

Copy link
Copy Markdown

Fixes #519.

signed_tree_multiplier takes the magnitude of each operand and then multiplies a[:-1] and b[:-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.

a=-8, b=3 (4 bits): got 0, expected -24

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 --diff and ruff check are clean.

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 fdxmw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.8%. Comparing base (9c4fd68) to head (248a4ad).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

signed_tree_multiplier returns 0 when an operand is the most negative value

2 participants