if value.operator == "r^":
# Believe it or not, this is the fastest way to compute a sideways XOR in Python.
return f"(format({mask(arg)}, 'b').count('1') % 2)"
+ if value.operator in ("u", "s"):
+ # These operators don't change the bit pattern, only its interpretation.
+ return self(arg)
elif len(value.operands) == 2:
lhs, rhs = value.operands
lhs_mask = (1 << len(lhs)) - 1
def on_Operator_unary(self, value):
arg, = value.operands
+ if value.operator in ("u", "s"):
+ # These operators don't change the bit pattern, only its interpretation.
+ return self(arg)
+
arg_bits, arg_sign = arg.shape()
res_bits, res_sign = value.shape()
res = self.s.rtlil.wire(width=res_bits, src=src(value.src_loc))
else:
raise TypeError("Cannot index value with {}".format(repr(key)))
+ def as_unsigned(self):
+ """Conversion to unsigned.
+
+ Returns
+ -------
+ Value, out
+ This ``Value`` reinterpreted as a unsigned integer.
+ """
+ return Operator("u", [self])
+
+ def as_signed(self):
+ """Conversion to signed.
+
+ Returns
+ -------
+ Value, out
+ This ``Value`` reinterpreted as a signed integer.
+ """
+ return Operator("s", [self])
+
def bool(self):
"""Conversion to boolean.
return Shape(a_width + 1, True)
if self.operator in ("b", "r|", "r&", "r^"):
return Shape(1, False)
+ if self.operator == "u":
+ return Shape(a_width, False)
+ if self.operator == "s":
+ return Shape(a_width, True)
elif len(op_shapes) == 2:
(a_width, a_signed), (b_width, b_signed) = op_shapes
if self.operator in ("+", "-"):
self.assertEqual(repr(v), "(~ (const 4'd0))")
self.assertEqual(v.shape(), unsigned(4))
+ def test_as_unsigned(self):
+ v = Const(-1, signed(4)).as_unsigned()
+ self.assertEqual(repr(v), "(u (const 4'sd-1))")
+ self.assertEqual(v.shape(), unsigned(4))
+
+ def test_as_signed(self):
+ v = Const(1, unsigned(4)).as_signed()
+ self.assertEqual(repr(v), "(s (const 4'd1))")
+ self.assertEqual(v.shape(), signed(4))
+
def test_neg(self):
v1 = -Const(0, unsigned(4))
self.assertEqual(repr(v1), "(- (const 4'd0))")
self.assertStatement(stmt, [C(1, 4)], C(1))
self.assertStatement(stmt, [C(2, 4)], C(1))
+ def test_as_unsigned(self):
+ stmt = lambda y, a, b: y.eq(a.as_unsigned() == b)
+ self.assertStatement(stmt, [C(0b01, signed(2)), C(0b0001, unsigned(4))], C(1))
+ self.assertStatement(stmt, [C(0b11, signed(2)), C(0b0011, unsigned(4))], C(1))
+
+ def test_as_signed(self):
+ stmt = lambda y, a, b: y.eq(a.as_signed() == b)
+ self.assertStatement(stmt, [C(0b01, unsigned(2)), C(0b0001, signed(4))], C(1))
+ self.assertStatement(stmt, [C(0b11, unsigned(2)), C(0b1111, signed(4))], C(1))
+
def test_any(self):
stmt = lambda y, a: y.eq(a.any())
self.assertStatement(stmt, [C(0b00, 2)], C(0))