remove m.If/Elif from fpdiv specialcases
[ieee754fpu.git] / src / ieee754 / fpdiv / div0.py
index ca0b6f2b054fd7bd83b53bf25a690bb6ca8267af..971224bef2bc94558aeac7a5eaabdd01494d83a9 100644 (file)
@@ -1,25 +1,37 @@
-"""IEEE754 Floating Point Divider
+"""IEEE754 Floating Point Divider / Square-Root / Reciprocal-Square-Root
+
+Copyright (C) 2019 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
+Copyright (C) 2019 Jacob Lifshay
+
+Relevant bugreports:
+* http://bugs.libre-riscv.org/show_bug.cgi?id=99
+* http://bugs.libre-riscv.org/show_bug.cgi?id=43
+* http://bugs.libre-riscv.org/show_bug.cgi?id=44
 
-Relevant bugreport: http://bugs.libre-riscv.org/show_bug.cgi?id=99
 """
 
 from nmigen import Module, Signal, Cat, Elaboratable, Const, Mux
 from nmigen.cli import main, verilog
 
-from ieee754.fpcommon.fpbase import (FPNumBaseRecord, Overflow)
-from ieee754.fpcommon.fpbase import FPState
+from nmutil.pipemodbase import PipeModBase
+from ieee754.fpcommon.fpbase import FPNumBaseRecord
 from ieee754.fpcommon.denorm import FPSCData
 from ieee754.fpcommon.getop import FPPipeContext
 from ieee754.div_rem_sqrt_rsqrt.div_pipe import DivPipeInputData
 from ieee754.div_rem_sqrt_rsqrt.core import DivPipeCoreOperation as DPCOp
 
 
-class FPDivStage0Mod(Elaboratable):
+class FPDivPreFPAdjust(PipeModBase):
+    """ DIV/SQRT/RSQRT "preparation" module.
 
+    adjusts mantissa and exponent (sqrt/rsqrt exponent must be even),
+    puts exponent (and sign) into data structures for passing through to
+    the end, and puts the (adjusted) mantissa into the processing engine.
+
+    no *actual* processing occurs here: it is *purely* preparation work.
+    """
     def __init__(self, pspec):
-        self.pspec = pspec
-        self.i = self.ispec()
-        self.o = self.ospec()
+        super().__init__(pspec, "pre_fp_adjust")
 
     def ispec(self):
         return FPSCData(self.pspec, False)
@@ -27,31 +39,10 @@ class FPDivStage0Mod(Elaboratable):
     def ospec(self):
         return DivPipeInputData(self.pspec)
 
-    def process(self, i):
-        return self.o
-
-    def setup(self, m, i):
-        """ links module to inputs and outputs
-        """
-        m.submodules.div0 = self
-        m.d.comb += self.i.eq(i)
-
     def elaborate(self, platform):
         m = Module()
         comb = m.d.comb
 
-        # XXX TODO, actual DIV code here.  this class would be
-        # "step one" which takes the pre-normalised data (see ispec) and
-        # *begins* the processing phase (enters the massive DIV
-        # pipeline chain) - see ospec.
-
-        # INPUT SPEC: FPSCData
-        # OUTPUT SPEC: DivPipeInputData
-
-        # NOTE: this stage does *NOT* do *ACTUAL* DIV processing,
-        # it is PURELY the *ENTRY* point into the chain, performing
-        # "preparation" work.
-
         # mantissas start in the range [1.0, 2.0)
 
         # intermediary temp signals
@@ -85,32 +76,31 @@ class FPDivStage0Mod(Elaboratable):
                  self.o.divisor_radicand.eq(divr_rad),
         ]
 
-        # set default since it's not always set; non-zero value for debugging
-        comb += self.o.operation.eq(1)
-
-        with m.If(~self.i.out_do_z):
-            # DIV
-            with m.If(self.i.ctx.op == int(DPCOp.UDivRem)):
-                comb += [self.o.z.e.eq(self.i.a.e - self.i.b.e),
-                         self.o.z.s.eq(self.i.a.s ^ self.i.b.s),
-                         self.o.operation.eq(int(DPCOp.UDivRem))
-                        ]
-
-            # SQRT
-            with m.Elif(self.i.ctx.op == int(DPCOp.SqrtRem)):
-                comb += [self.o.z.e.eq(adj_a_e >> 1),
-                         self.o.z.s.eq(self.i.a.s),
-                         self.o.operation.eq(int(DPCOp.SqrtRem))
-                        ]
-
-            # RSQRT
-            with m.Elif(self.i.ctx.op == int(DPCOp.RSqrtRem)):
-                comb += [self.o.z.e.eq(-(adj_a_e >> 1)),
-                         self.o.z.s.eq(self.i.a.s),
-                         self.o.operation.eq(int(DPCOp.RSqrtRem))
-                        ]
-
-        # these are required and must not be touched
+        ############# DIV #############
+        with m.If(self.i.ctx.op == int(DPCOp.UDivRem)):
+            # DIV: subtract exponents, XOR sign
+            comb += [self.o.z.e.eq(self.i.a.e - self.i.b.e),
+                     self.o.z.s.eq(self.i.a.s ^ self.i.b.s),
+                     self.o.operation.eq(int(DPCOp.UDivRem))
+                    ]
+
+        ############# SQRT #############
+        with m.Elif(self.i.ctx.op == int(DPCOp.SqrtRem)):
+            # SQRT: sign is the same, [adjusted] exponent is halved
+            comb += [self.o.z.e.eq(adj_a_e >> 1), # halve
+                     self.o.z.s.eq(self.i.a.s),
+                     self.o.operation.eq(int(DPCOp.SqrtRem))
+                    ]
+
+        ############# RSQRT #############
+        with m.Elif(self.i.ctx.op == int(DPCOp.RSqrtRem)):
+            # RSQRT: sign same, [adjusted] exponent halved and inverted
+            comb += [self.o.z.e.eq(-(adj_a_e >> 1)), # NEGATE and halve
+                     self.o.z.s.eq(self.i.a.s),
+                     self.o.operation.eq(int(DPCOp.RSqrtRem))
+                    ]
+
+        # pass through context
         comb += self.o.oz.eq(self.i.oz)
         comb += self.o.out_do_z.eq(self.i.out_do_z)
         comb += self.o.ctx.eq(self.i.ctx)
@@ -118,22 +108,3 @@ class FPDivStage0Mod(Elaboratable):
         return m
 
 
-class FPDivStage0(FPState):
-    """ First stage of div.
-    """
-
-    def __init__(self, pspec):
-        FPState.__init__(self, "divider_0")
-        self.mod = FPDivStage0Mod(pspec)
-        self.o = self.mod.ospec()
-
-    def setup(self, m, i):
-        """ links module to inputs and outputs
-        """
-        self.mod.setup(m, i)
-
-        # NOTE: these could be done as combinatorial (merge div0+div1)
-        m.d.sync += self.o.eq(self.mod.o)
-
-    def action(self, m):
-        m.next = "divider_1"