From 1d5e09058072fbf952b44ab77815d6b02d9a0965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcelina=20Ko=C5=9Bcielnicka?= Date: Wed, 7 Jun 2023 14:29:33 +0200 Subject: [PATCH] hdl.ast: fix shape for subtraction. Fixes #813. --- amaranth/hdl/ast.py | 5 ++++- tests/test_hdl_ast.py | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/amaranth/hdl/ast.py b/amaranth/hdl/ast.py index d181305..da55380 100644 --- a/amaranth/hdl/ast.py +++ b/amaranth/hdl/ast.py @@ -730,9 +730,12 @@ class Operator(Value): return Shape(a_shape.width, True) elif len(op_shapes) == 2: a_shape, b_shape = op_shapes - if self.operator in ("+", "-"): + if self.operator == "+": o_shape = _bitwise_binary_shape(*op_shapes) return Shape(o_shape.width + 1, o_shape.signed) + if self.operator == "-": + o_shape = _bitwise_binary_shape(*op_shapes) + return Shape(o_shape.width + 1, True) if self.operator == "*": return Shape(a_shape.width + b_shape.width, a_shape.signed or b_shape.signed) if self.operator == "//": diff --git a/tests/test_hdl_ast.py b/tests/test_hdl_ast.py index 25c6be8..af4b175 100644 --- a/tests/test_hdl_ast.py +++ b/tests/test_hdl_ast.py @@ -443,7 +443,7 @@ class OperatorTestCase(FHDLTestCase): def test_sub(self): v1 = Const(0, unsigned(4)) - Const(0, unsigned(6)) self.assertEqual(repr(v1), "(- (const 4'd0) (const 6'd0))") - self.assertEqual(v1.shape(), unsigned(7)) + self.assertEqual(v1.shape(), signed(7)) v2 = Const(0, signed(4)) - Const(0, signed(6)) self.assertEqual(v2.shape(), signed(7)) v3 = Const(0, signed(4)) - Const(0, unsigned(4)) @@ -451,7 +451,9 @@ class OperatorTestCase(FHDLTestCase): v4 = Const(0, unsigned(4)) - Const(0, signed(4)) self.assertEqual(v4.shape(), signed(6)) v5 = 10 - Const(0, 4) - self.assertEqual(v5.shape(), unsigned(5)) + self.assertEqual(v5.shape(), signed(5)) + v6 = 1 - Const(2) + self.assertEqual(v6.shape(), signed(3)) def test_mul(self): v1 = Const(0, unsigned(4)) * Const(0, unsigned(6))