Add subtraction to partsig.py
authorMichael Nolan <mtnolan2640@gmail.com>
Mon, 10 Feb 2020 14:37:26 +0000 (09:37 -0500)
committerMichael Nolan <mtnolan2640@gmail.com>
Mon, 10 Feb 2020 14:43:09 +0000 (09:43 -0500)
src/ieee754/part/partsig.py
src/ieee754/part/test/test_partsig.py

index 0347d340d29c7acdc9288e02d2362c987521a4c9..a4cad8d6f990e880491e206b7e8158d5c615193d 100644 (file)
@@ -114,18 +114,32 @@ class PartitionedSignal:
         comb += pa.carry_in.eq(carry)
         return (pa.output, pa.carry_out)
 
+    def sub_op(self, op1, op2, carry=~0):
+        op1 = getsig(op1)
+        op2 = getsig(op2)
+        shape = op1.shape()
+        pa = PartitionedAdder(shape[0], self.partpoints)
+        setattr(self.m.submodules, self.get_modname('add'), pa)
+        comb = self.m.d.comb
+        comb += pa.a.eq(op1)
+        comb += pa.b.eq(~op2)
+        comb += pa.carry_in.eq(carry)
+        return (pa.output, pa.carry_out)
+
     def __add__(self, other):
         result, _ =self.add_op(self, other, carry=0)
         return result
 
-
     def __radd__(self, other):
-        return self.add_op(other, self)
+        result, _ =self.add_op(other, self)
+        return result
 
     def __sub__(self, other):
-        return self.sub_op(self, other) # TODO, subop
+        result, _ = self.sub_op(self, other) # TODO, subop
+        return result
     def __rsub__(self, other):
-        return self.sub_op(other, self) # TODO, subop
+        result, _ = self.sub_op(other, self) # TODO, subop
+        return result
 
     def __mul__(self, other):
         return Operator("*", [self, other])
index 9c5c9359b92ef6cb5be7b395fe8dcebf4861f80a..ce8464c98e7a03314bbe0c593770b023267dcc41 100644 (file)
@@ -37,6 +37,7 @@ class TestAddMod(Elaboratable):
         self.a = PartitionedSignal(partpoints, width)
         self.b = PartitionedSignal(partpoints, width)
         self.add_output = Signal(width)
+        self.sub_output = Signal(width)
         self.eq_output = Signal(len(partpoints)+1)
         self.gt_output = Signal(len(partpoints)+1)
         self.ge_output = Signal(len(partpoints)+1)
@@ -96,10 +97,8 @@ class TestPartitionPoints(unittest.TestCase):
                 return mask & ((a & mask) + (b & mask) + lsb)
 
             def test_sub_fn(carry_in, a, b, mask):
-                raise NotImplementedError
-                # TODO
                 lsb = mask & ~(mask-1) if carry_in else 0
-                return mask & ((a & mask) + (b & mask) + lsb)
+                return mask & ((a & mask) + (~b & mask) + lsb)
 
             def test_op(msg_prefix, carry, test_fn, mod_attr, *mask_list):
                 rand_data = []
@@ -129,7 +128,7 @@ class TestPartitionPoints(unittest.TestCase):
                     self.assertEqual(y, outval, msg)
 
             for (test_fn, mod_attr) in ((test_add_fn, "add"),
-                                        #(test_sub_fn, "sub"),
+                                        (test_sub_fn, "sub"),
                                         ):
                 yield part_mask.eq(0)
                 yield from test_op("16-bit", 1, test_fn, mod_attr, 0xFFFF)