hdl.ast: make Value.__abs__ return unsigned shape.

This commit is contained in:
Marcelina Kościelnicka 2023-06-07 17:50:01 +02:00 committed by Catherine
parent 51391be1ae
commit b1cce87630
2 changed files with 12 additions and 1 deletions

View file

@ -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

View file

@ -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):