SelectableInt: make __mul__ return enough space to fit the result
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 6 Jul 2020 18:44:58 +0000 (19:44 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 6 Jul 2020 18:44:58 +0000 (19:44 +0100)
src/soc/decoder/selectable_int.py

index 0c5e560c3f54462b406aef7864dacb5537d2c7a7..3036b6f6412afd87c8aabc9fe6a58ef1652e57b6 100644 (file)
@@ -178,7 +178,12 @@ class SelectableInt:
     def __sub__(self, b):
         return self._op(sub, b)
     def __mul__(self, b):
-        return self._op(mul, b)
+        # different case: mul result needs to fit the total bitsize 
+        if isinstance(b, int):
+            b = SelectableInt(b, self.bits)
+        print ("SelectableInt mul", hex(self.value), hex(b.value),
+                                    self.bits, b.bits)
+        return SelectableInt(self.value * b.value, self.bits + b.bits)
     def __floordiv__(self, b):
         return self._op(floordiv, b)
     def __truediv__(self, b):