From 6ea789c9e5a54e7dfbfbd96cb0c620e52b01c2a4 Mon Sep 17 00:00:00 2001 From: Michael Nolan Date: Mon, 10 Feb 2020 09:37:26 -0500 Subject: [PATCH] Add subtraction to partsig.py --- src/ieee754/part/partsig.py | 22 ++++++++++++++++++---- src/ieee754/part/test/test_partsig.py | 7 +++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/ieee754/part/partsig.py b/src/ieee754/part/partsig.py index 0347d340..a4cad8d6 100644 --- a/src/ieee754/part/partsig.py +++ b/src/ieee754/part/partsig.py @@ -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]) diff --git a/src/ieee754/part/test/test_partsig.py b/src/ieee754/part/test/test_partsig.py index 9c5c9359..ce8464c9 100644 --- a/src/ieee754/part/test/test_partsig.py +++ b/src/ieee754/part/test/test_partsig.py @@ -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) -- 2.30.2