1 # IEEE Floating Point Multiplier
3 from nmigen
import Module
, Signal
, Elaboratable
4 from nmigen
.cli
import main
, verilog
6 from ieee754
.fpcommon
.fpbase
import FPState
7 from ieee754
.fpcommon
.postcalc
import FPAddStage1Data
8 from .mul0
import FPMulStage0Data
11 class FPMulStage1Mod(FPState
, Elaboratable
):
12 """ Second stage of mul: preparation for normalisation.
15 def __init__(self
, pspec
):
21 return FPMulStage0Data(self
.pspec
)
24 return FPAddStage1Data(self
.pspec
)
29 def setup(self
, m
, i
):
30 """ links module to inputs and outputs
32 m
.submodules
.mul1
= self
33 m
.d
.comb
+= self
.i
.eq(i
)
35 def elaborate(self
, platform
):
37 m
.d
.comb
+= self
.o
.z
.eq(self
.i
.z
)
38 with m
.If(~self
.i
.out_do_z
):
39 # results are in the range 0.25 to 0.999999999999
40 # sometimes the MSB will be zero, (0.5 * 0.5 = 0.25 which
41 # in binary is 0b010000) so to compensate for that we have
42 # to shift the mantissa up (and reduce the exponent by 1)
43 p
= Signal(len(self
.i
.product
), reset_less
=True)
44 with m
.If(self
.i
.product
[-1]):
45 m
.d
.comb
+= p
.eq(self
.i
.product
)
47 # get 1 bit of extra accuracy if the mantissa top bit is zero
48 m
.d
.comb
+= p
.eq(self
.i
.product
<<1)
49 m
.d
.comb
+= self
.o
.z
.e
.eq(self
.i
.z
.e
-1)
51 # top bits are mantissa, then guard and round, and the rest of
52 # the product is sticky
55 self
.o
.z
.m
.eq(p
[mw
+2:]), # mantissa
56 self
.o
.of
.m0
.eq(p
[mw
+2]), # copy of LSB
57 self
.o
.of
.guard
.eq(p
[mw
+1]), # guard
58 self
.o
.of
.round_bit
.eq(p
[mw
]), # round
59 self
.o
.of
.sticky
.eq(p
[0:mw
].bool()) # sticky
62 m
.d
.comb
+= self
.o
.out_do_z
.eq(self
.i
.out_do_z
)
63 m
.d
.comb
+= self
.o
.oz
.eq(self
.i
.oz
)
64 m
.d
.comb
+= self
.o
.ctx
.eq(self
.i
.ctx
)
69 class FPMulStage1(FPState
):
71 def __init__(self
, pspec
):
72 FPState
.__init
__(self
, "multiply_1")
74 self
.mod
= FPMulStage1Mod(pspec
)
75 self
.out_z
= FPNumBaseRecord(width
, False)
76 self
.norm_stb
= Signal()
78 def setup(self
, m
, i
):
79 """ links module to inputs and outputs
83 m
.d
.sync
+= self
.norm_stb
.eq(0) # sets to zero when not in mul1 state
85 m
.d
.sync
+= self
.out_z
.eq(self
.mod
.out_z
)
86 m
.d
.sync
+= self
.norm_stb
.eq(1)
89 m
.next
= "normalise_1"