1 # IEEE Floating Point Multiplier
3 from nmigen
import Module
, Signal
, Cat
, Const
, Elaboratable
4 from nmigen
.cli
import main
, verilog
7 from ieee754
.fpcommon
.fpbase
import FPNumDecode
, FPNumBaseRecord
8 from nmutil
.singlepipe
import SimpleHandshake
, StageChain
10 from ieee754
.fpcommon
.fpbase
import FPState
, FPID
11 from ieee754
.fpcommon
.getop
import FPADDBaseData
12 from ieee754
.fpcommon
.denorm
import (FPSCData
, FPAddDeNormMod
)
13 from ieee754
.fpmul
.align
import FPAlignModSingle
16 class FPMulSpecialCasesMod(Elaboratable
):
17 """ special cases: NaNs, infs, zeros, denormalised
18 see "Special Operations"
19 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
22 def __init__(self
, pspec
):
28 return FPADDBaseData(self
.pspec
)
31 return FPSCData(self
.pspec
, False)
33 def setup(self
, m
, i
):
34 """ links module to inputs and outputs
36 m
.submodules
.specialcases
= self
37 m
.d
.comb
+= self
.i
.eq(i
)
42 def elaborate(self
, platform
):
45 #m.submodules.sc_out_z = self.o.z
47 # decode: XXX really should move to separate stage
48 width
= self
.pspec
.width
49 a1
= FPNumBaseRecord(width
, False)
50 b1
= FPNumBaseRecord(width
, False)
51 m
.submodules
.sc_decode_a
= a1
= FPNumDecode(None, a1
)
52 m
.submodules
.sc_decode_b
= b1
= FPNumDecode(None, b1
)
53 m
.d
.comb
+= [a1
.v
.eq(self
.i
.a
),
59 obz
= Signal(reset_less
=True)
60 m
.d
.comb
+= obz
.eq(a1
.is_zero | b1
.is_zero
)
62 sabx
= Signal(reset_less
=True) # sign a xor b (sabx, get it?)
63 m
.d
.comb
+= sabx
.eq(a1
.s ^ b1
.s
)
65 abnan
= Signal(reset_less
=True)
66 m
.d
.comb
+= abnan
.eq(a1
.is_nan | b1
.is_nan
)
68 # if a is NaN or b is NaN return NaN
70 m
.d
.comb
+= self
.o
.out_do_z
.eq(1)
71 m
.d
.comb
+= self
.o
.z
.nan(0)
73 # if a is inf return inf (or NaN)
74 with m
.Elif(a1
.is_inf
):
75 m
.d
.comb
+= self
.o
.out_do_z
.eq(1)
76 m
.d
.comb
+= self
.o
.z
.inf(sabx
)
77 # b is zero return NaN
78 with m
.If(b1
.is_zero
):
79 m
.d
.comb
+= self
.o
.z
.nan(0)
81 # if b is inf return inf (or NaN)
82 with m
.Elif(b1
.is_inf
):
83 m
.d
.comb
+= self
.o
.out_do_z
.eq(1)
84 m
.d
.comb
+= self
.o
.z
.inf(sabx
)
85 # a is zero return NaN
86 with m
.If(a1
.is_zero
):
87 m
.d
.comb
+= self
.o
.z
.nan(0)
89 # if a is zero or b zero return signed-a/b
91 m
.d
.comb
+= self
.o
.out_do_z
.eq(1)
92 m
.d
.comb
+= self
.o
.z
.zero(sabx
)
94 # Denormalised Number checks next, so pass a/b data through
96 m
.d
.comb
+= self
.o
.out_do_z
.eq(0)
98 m
.d
.comb
+= self
.o
.oz
.eq(self
.o
.z
.v
)
99 m
.d
.comb
+= self
.o
.ctx
.eq(self
.i
.ctx
)
104 class FPMulSpecialCases(FPState
):
105 """ special cases: NaNs, infs, zeros, denormalised
106 NOTE: some of these are unique to add. see "Special Operations"
107 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
110 def __init__(self
, width
, id_wid
):
111 FPState
.__init
__(self
, "special_cases")
112 self
.mod
= FPMulSpecialCasesMod(width
)
113 self
.out_z
= self
.mod
.ospec()
114 self
.out_do_z
= Signal(reset_less
=True)
116 def setup(self
, m
, i
):
117 """ links module to inputs and outputs
119 self
.mod
.setup(m
, i
, self
.out_do_z
)
120 m
.d
.sync
+= self
.out_z
.v
.eq(self
.mod
.out_z
.v
) # only take the output
121 m
.d
.sync
+= self
.out_z
.ctx
.eq(self
.mod
.o
.ctx
) # (and context)
125 with m
.If(self
.out_do_z
):
128 m
.next
= "denormalise"
131 class FPMulSpecialCasesDeNorm(FPState
, SimpleHandshake
):
132 """ special cases: NaNs, infs, zeros, denormalised
135 def __init__(self
, pspec
):
136 FPState
.__init
__(self
, "special_cases")
138 SimpleHandshake
.__init
__(self
, self
) # pipe is its own stage
139 self
.out
= self
.ospec()
142 return FPADDBaseData(self
.pspec
)
145 return FPSCData(self
.pspec
, False)
147 def setup(self
, m
, i
):
148 """ links module to inputs and outputs
150 smod
= FPMulSpecialCasesMod(self
.pspec
)
151 dmod
= FPAddDeNormMod(self
.pspec
, False)
152 amod
= FPAlignModSingle(self
.pspec
, False)
154 chain
= StageChain([smod
, dmod
, amod
])
157 # only needed for break-out (early-out)
158 # self.out_do_z = smod.o.out_do_z
162 def process(self
, i
):
166 # for break-out (early-out)
167 #with m.If(self.out_do_z):
170 m
.d
.sync
+= self
.out
.eq(self
.process(None))