diff --git a/amaranth/hdl/ast.py b/amaranth/hdl/ast.py index 0b4939d..b431a6d 100644 --- a/amaranth/hdl/ast.py +++ b/amaranth/hdl/ast.py @@ -263,7 +263,7 @@ class Value(metaclass=ABCMeta): def __abs__(self): if self.shape().signed: - return Mux(self >= 0, self, -self) + return Mux(self >= 0, self, -self)[:len(self)] else: return self diff --git a/tests/test_hdl_ast.py b/tests/test_hdl_ast.py index af4b175..5878a6c 100644 --- a/tests/test_hdl_ast.py +++ b/tests/test_hdl_ast.py @@ -675,6 +675,17 @@ class OperatorTestCase(FHDLTestCase): with self.assertRaises(TypeError): hash(Const(0) + Const(0)) + def test_abs(self): + s = Signal(4) + self.assertRepr(abs(s), """ + (sig s) + """) + s = Signal(signed(4)) + self.assertRepr(abs(s), """ + (slice (m (>= (sig s) (const 1'd0)) (sig s) (- (sig s))) 0:4) + """) + self.assertEqual(abs(s).shape(), unsigned(4)) + class SliceTestCase(FHDLTestCase): def test_shape(self):