Skip to content

Commit

Permalink
hdl.ast: make Value.__abs__ return unsigned shape.
Browse files Browse the repository at this point in the history
  • Loading branch information
wanda-phi authored and whitequark committed Jun 7, 2023
1 parent 51391be commit b1cce87
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion amaranth/hdl/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def __ge__(self, other):

def __abs__(self):
if self.shape().signed:
return Mux(self >= 0, self, -self)
return Mux(self >= 0, self, -self)[:len(self)]
else:
return self

Expand Down
11 changes: 11 additions & 0 deletions tests/test_hdl_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,17 @@ def test_hash(self):
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):
Expand Down

0 comments on commit b1cce87

Please sign in to comment.