From: whitequark Date: Sat, 26 Jan 2019 00:54:02 +0000 (+0000) Subject: hdl.ast: fix shape calculation for *. X-Git-Tag: working~47 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=f71e0fffbbe7e2a5269e4944316ac7246ea4a986;p=nmigen.git 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). --- diff --git a/nmigen/hdl/ast.py b/nmigen/hdl/ast.py index 6e2155e..79b6ed7 100644 --- a/nmigen/hdl/ast.py +++ b/nmigen/hdl/ast.py @@ -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"): diff --git a/nmigen/test/test_hdl_ast.py b/nmigen/test/test_hdl_ast.py index d1c5bb5..9f20d17 100644 --- a/nmigen/test/test_hdl_ast.py +++ b/nmigen/test/test_hdl_ast.py @@ -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)