Add reversed add and subtract, as well as lshift and rshift
authorMichael Nolan <mtnolan2640@gmail.com>
Sat, 9 May 2020 14:41:29 +0000 (10:41 -0400)
committerMichael Nolan <mtnolan2640@gmail.com>
Sat, 9 May 2020 14:52:28 +0000 (10:52 -0400)
src/soc/decoder/selectable_int.py

index 6ee92535d70c43e649e2f9adbae4cb34067beae2..a275de0724d0c6fed689d6ceaef2904cf99c01a6 100644 (file)
@@ -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)