From: Michael Nolan Date: Sat, 9 May 2020 14:41:29 +0000 (-0400) Subject: Add reversed add and subtract, as well as lshift and rshift X-Git-Tag: div_pipeline~1315 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=2785f48f4eea8c938348ea84a2e62d2ff7b9de8a;p=soc.git Add reversed add and subtract, as well as lshift and rshift --- diff --git a/src/soc/decoder/selectable_int.py b/src/soc/decoder/selectable_int.py index 6ee92535..a275de07 100644 --- a/src/soc/decoder/selectable_int.py +++ b/src/soc/decoder/selectable_int.py @@ -171,6 +171,20 @@ class SelectableInt: assert b.bits == self.bits return SelectableInt(self.value - b.value, self.bits) + def __rsub__(self, b): + if isinstance(b, int): + b = SelectableInt(b, self.bits) + b = check_extsign(self, b) + assert b.bits == self.bits + return SelectableInt(b.value - self.value, self.bits) + + def __radd__(self, b): + if isinstance(b, int): + b = SelectableInt(b, self.bits) + b = check_extsign(self, b) + assert b.bits == self.bits + return SelectableInt(b.value + self.value, self.bits) + def __mul__(self, b): b = check_extsign(self, b) assert b.bits == self.bits @@ -208,6 +222,14 @@ class SelectableInt: def __neg__(self): return SelectableInt(~self.value + 1, self.bits) + def __lshift__(self, b): + b = check_extsign(self, b) + return SelectableInt(self.value << b.value, self.bits) + + def __rshift__(self, b): + b = check_extsign(self, b) + return SelectableInt(self.value >> b.value, self.bits) + def __getitem__(self, key): if isinstance(key, int): assert key < self.bits, "key %d accessing %d" % (key, self.bits)