Skip to content

Commit

Permalink
hdl.ast: Fix width for unary minus operator on signed argument.
Browse files Browse the repository at this point in the history
To properly represent a negation of a signed X-bit quantity we may, in
general, need a signed (X+1)-bit signal — for example, negation of
3-bit -4 is 4, which is not representable in signed 3 bits.
  • Loading branch information
wanda-phi authored and whitequark committed Dec 4, 2019
1 parent 7650431 commit 6765021
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
5 changes: 1 addition & 4 deletions nmigen/hdl/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,7 @@ def _bitwise_binary_shape(a_shape, b_shape):
if self.operator in ("+", "~"):
return Shape(a_width, a_signed)
if self.operator == "-":
if not a_signed:
return Shape(a_width + 1, True)
else:
return Shape(a_width, a_signed)
return Shape(a_width + 1, True)
if self.operator in ("b", "r|", "r&", "r^"):
return Shape(1, False)
elif len(op_shapes) == 2:
Expand Down
2 changes: 1 addition & 1 deletion nmigen/test/test_hdl_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def test_neg(self):
self.assertEqual(v1.shape(), signed(5))
v2 = -Const(0, signed(4))
self.assertEqual(repr(v2), "(- (const 4'sd0))")
self.assertEqual(v2.shape(), signed(4))
self.assertEqual(v2.shape(), signed(5))

def test_add(self):
v1 = Const(0, unsigned(4)) + Const(0, unsigned(6))
Expand Down

0 comments on commit 6765021

Please sign in to comment.