hdl.ast: fix shape calculation for *.

This was carried over from Migen, and is wrong there too.
Counterexample: 1'sd-1 * 4'sd-4 = 4'sd-4 (but should be 5'sd4).
This commit is contained in:
whitequark 2019-01-26 00:54:02 +00:00
parent 7b25665fde
commit f71e0fffbb
2 changed files with 2 additions and 9 deletions

View file

@ -333,14 +333,7 @@ class Operator(Value):
bits, sign = self._bitwise_binary_shape(*op_shapes)
return bits + 1, sign
if self.op == "*":
if not a_sign and not b_sign:
# both operands unsigned
return a_bits + b_bits, False
if a_sign and b_sign:
# both operands signed
return a_bits + b_bits - 1, True
# one operand signed, the other unsigned (add sign bit)
return a_bits + b_bits + 1 - 1, True
return a_bits + b_bits, a_sign or b_sign
if self.op == "%":
return a_bits, a_sign
if self.op in ("<", "<=", "==", "!=", ">", ">=", "b"):

View file

@ -137,7 +137,7 @@ class OperatorTestCase(FHDLTestCase):
self.assertEqual(repr(v1), "(* (const 4'd0) (const 6'd0))")
self.assertEqual(v1.shape(), (10, False))
v2 = Const(0, (4, True)) * Const(0, (6, True))
self.assertEqual(v2.shape(), (9, True))
self.assertEqual(v2.shape(), (10, True))
v3 = Const(0, (4, True)) * Const(0, (4, False))
self.assertEqual(v3.shape(), (8, True))
v5 = 10 * Const(0, 4)