From 0e557669c53c1c7c0ee3ddbad3cf8bf151eeeb24 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Wed, 27 Feb 2019 17:02:50 +0000 Subject: [PATCH] create single and multi shift cycle, single doesnt work, multi does --- src/add/fpbase.py | 12 +-- src/add/nmigen_add_experiment.py | 125 ++++++++++++++++++++++++++----- 2 files changed, 113 insertions(+), 24 deletions(-) diff --git a/src/add/fpbase.py b/src/add/fpbase.py index e7344fdc..8b1259c2 100644 --- a/src/add/fpbase.py +++ b/src/add/fpbase.py @@ -205,14 +205,14 @@ class FPNumShift(FPNumBase): return m - def shift_down(self): + def shift_down(self, inp): """ shifts a mantissa down by one. exponent is increased to compensate accuracy is lost as a result in the mantissa however there are 3 guard bits (the latter of which is the "sticky" bit) """ - return [self.e.eq(self.e + 1), - self.m.eq(Cat(self.m[0] | self.m[1], self.m[2:], 0)) + return [self.e.eq(inp.e + 1), + self.m.eq(Cat(inp.m[0] | inp.m[1], inp.m[2:], 0)) ] def shift_down_multi(self, diff): @@ -293,14 +293,14 @@ class FPNumIn(FPNumBase): self.s.eq(v[-1]), # sign ] - def shift_down(self): + def shift_down(self, inp): """ shifts a mantissa down by one. exponent is increased to compensate accuracy is lost as a result in the mantissa however there are 3 guard bits (the latter of which is the "sticky" bit) """ - return [self.e.eq(self.e + 1), - self.m.eq(Cat(self.m[0] | self.m[1], self.m[2:], 0)) + return [self.e.eq(inp.e + 1), + self.m.eq(Cat(inp.m[0] | inp.m[1], inp.m[2:], 0)) ] def shift_down_multi(self, diff): diff --git a/src/add/nmigen_add_experiment.py b/src/add/nmigen_add_experiment.py index 5d932346..e80c149e 100644 --- a/src/add/nmigen_add_experiment.py +++ b/src/add/nmigen_add_experiment.py @@ -178,42 +178,126 @@ class FPAddDeNorm(FPState): self.denormalise(m, self.b) -class FPAddAlignMulti(FPState): +class FPAddAlignMultiMod(FPState): + + def __init__(self, width): + self.in_a = FPNumBase(width) + self.in_b = FPNumBase(width) + self.out_a = FPNumIn(None, width) + self.out_b = FPNumIn(None, width) + self.exp_eq = Signal(reset_less=True) + + def setup(self, m, in_a, in_b, out_a, out_b, exp_eq): + """ links module to inputs and outputs + """ + m.d.comb += self.in_a.copy(in_a) + m.d.comb += self.in_b.copy(in_b) + m.d.comb += out_a.copy(self.out_a) + m.d.comb += out_b.copy(self.out_b) + m.d.comb += exp_eq.eq(self.exp_eq) + + def elaborate(self, platform): + # This one however (single-cycle) will do the shift + # in one go. + + m = Module() + + #m.submodules.align_in_a = self.in_a + #m.submodules.align_in_b = self.in_b + m.submodules.align_out_a = self.out_a + m.submodules.align_out_b = self.out_b - def action(self, m): # NOTE: this does *not* do single-cycle multi-shifting, # it *STAYS* in the align state until exponents match # exponent of a greater than b: shift b down - with m.If(self.a.e > self.b.e): - m.d.sync += self.b.shift_down() + m.d.comb += self.exp_eq.eq(0) + m.d.comb += self.out_a.copy(self.in_a) + m.d.comb += self.out_b.copy(self.in_b) + with m.If(self.in_a.e > self.in_b.e): + m.d.comb += self.out_b.shift_down(self.in_b) # exponent of b greater than a: shift a down - with m.Elif(self.a.e < self.b.e): - m.d.sync += self.a.shift_down() + with m.Elif(self.in_a.e < self.in_b.e): + m.d.comb += self.out_a.shift_down(self.in_a) # exponents equal: move to next stage. with m.Else(): - m.next = "add_0" + m.d.comb += self.exp_eq.eq(1) + return m -class FPAddAlignSingle(FPState): +class FPAddAlignMulti(FPState): + + def __init__(self, width): + FPState.__init__(self, "align") + self.mod = FPAddAlignMultiMod(width) + self.out_a = FPNumIn(None, width) + self.out_b = FPNumIn(None, width) + self.exp_eq = Signal(reset_less=True) def action(self, m): + m.d.sync += self.a.copy(self.out_a) + m.d.sync += self.b.copy(self.out_b) + with m.If(self.exp_eq): + m.next = "add_0" + + +class FPAddAlignSingleMod: + + def __init__(self, width): + self.in_a = FPNumBase(width) + self.in_b = FPNumBase(width) + self.out_a = FPNumIn(None, width) + self.out_b = FPNumIn(None, width) + #self.out_a = FPNumBase(width) + #self.out_b = FPNumBase(width) + + def setup(self, m, in_a, in_b, out_a, out_b): + """ links module to inputs and outputs + """ + m.d.comb += self.in_a.copy(in_a) + m.d.comb += self.in_b.copy(in_b) + m.d.comb += out_a.copy(self.out_a) + m.d.comb += out_b.copy(self.out_b) + + def elaborate(self, platform): # This one however (single-cycle) will do the shift # in one go. + m = Module() + + #m.submodules.align_in_a = self.in_a + #m.submodules.align_in_b = self.in_b + m.submodules.align_out_a = self.out_a + m.submodules.align_out_b = self.out_b + # XXX TODO: the shifter used here is quite expensive # having only one would be better - ediff = Signal((len(self.a.e), True), reset_less=True) - ediffr = Signal((len(self.a.e), True), reset_less=True) - m.d.comb += ediff.eq(self.a.e - self.b.e) - m.d.comb += ediffr.eq(self.b.e - self.a.e) + ediff = Signal((len(self.in_a.e), True), reset_less=True) + ediffr = Signal((len(self.in_a.e), True), reset_less=True) + m.d.comb += ediff.eq(self.in_a.e - self.in_b.e) + m.d.comb += ediffr.eq(self.in_b.e - self.in_a.e) + m.d.comb += self.out_a.copy(self.in_a) + m.d.comb += self.out_b.copy(self.in_b) with m.If(ediff > 0): - m.d.sync += self.b.shift_down_multi(ediff) + m.d.comb += self.out_b.shift_down_multi(ediff) # exponent of b greater than a: shift a down with m.Elif(ediff < 0): - m.d.sync += self.a.shift_down_multi(ediffr) + m.d.comb += self.out_a.shift_down_multi(ediffr) + return m + +class FPAddAlignSingle(FPState): + + def __init__(self, width): + FPState.__init__(self, "align") + self.mod = FPAddAlignSingleMod(width) + self.out_a = FPNumIn(None, width) + self.out_b = FPNumIn(None, width) + + def action(self, m): + m.d.sync += self.a.copy(self.out_a) + m.d.sync += self.b.copy(self.out_b) m.next = "add_0" @@ -629,11 +713,16 @@ class FPADD: dn.set_outputs({"a": a, "b": b}) # XXX outputs same as inputs if self.single_cycle: - alm = self.add_state(FPAddAlignSingle("align")) + alm = self.add_state(FPAddAlignSingle(self.width)) + alm.set_inputs({"a": a, "b": b}) + alm.set_outputs({"a": a, "b": b}) # XXX outputs same as inputs + alm.mod.setup(m, a, b, alm.out_a, alm.out_b) else: - alm = self.add_state(FPAddAlignMulti("align")) - alm.set_inputs({"a": a, "b": b}) - alm.set_outputs({"a": a, "b": b}) # XXX outputs same as inputs + alm = self.add_state(FPAddAlignMulti(self.width)) + alm.set_inputs({"a": a, "b": b}) + alm.set_outputs({"a": a, "b": b}) # XXX outputs same as inputs + alm.mod.setup(m, a, b, alm.out_a, alm.out_b, alm.exp_eq) + m.submodules.align = alm.mod add0 = self.add_state(FPAddStage0(self.width)) add0.set_inputs({"a": a, "b": b}) -- 2.30.2