1 # IEEE Floating Point Adder (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
5 from nmigen
import Module
, Signal
, Cat
6 from nmigen
.cli
import main
, verilog
8 from fpbase
import FPNumIn
, FPNumOut
, FPOp
, Overflow
, FPBase
10 class FPState(FPBase
):
11 def __init__(self
, state_from
, state_to
):
12 self
.state_from
= state_from
13 self
.state_to
= state_to
15 def set_inputs(self
, inputs
):
17 for k
,v
in inputs
.items():
20 def set_outputs(self
, outputs
):
21 self
.outputs
= outputs
22 for k
,v
in outputs
.items():
26 class FPGetOpA(FPState
):
29 self
.get_op(m
, self
.in_a
, self
.a
, self
.state_to
)
32 class FPGetOpB(FPState
):
35 self
.get_op(m
, self
.in_b
, self
.b
, self
.state_to
)
40 def __init__(self
, width
, single_cycle
=False):
43 self
.single_cycle
= single_cycle
45 self
.in_a
= FPOp(width
)
46 self
.in_b
= FPOp(width
)
47 self
.out_z
= FPOp(width
)
49 def get_fragment(self
, platform
=None):
50 """ creates the HDL code-fragment for FPAdd
55 a
= FPNumIn(self
.in_a
, self
.width
)
56 b
= FPNumIn(self
.in_b
, self
.width
)
57 z
= FPNumOut(self
.width
, False)
59 m
.submodules
.fpnum_a
= a
60 m
.submodules
.fpnum_b
= b
61 m
.submodules
.fpnum_z
= z
64 tot
= Signal(w
, reset_less
=True) # sticky/round/guard, {mantissa} result, 1 overflow
67 m
.submodules
.overflow
= of
69 geta
= FPGetOpA("get_a", "get_b")
70 geta
.set_inputs({"in_a": self
.in_a
})
71 geta
.set_outputs({"a": a
})
72 m
.d
.comb
+= a
.v
.eq(self
.in_a
.v
) # links in_a to a
74 getb
= FPGetOpB("get_b", "special_cases")
75 getb
.set_inputs({"in_b": self
.in_b
})
76 getb
.set_outputs({"b": b
})
77 m
.d
.comb
+= b
.v
.eq(self
.in_b
.v
) # links in_b to b
84 with m
.State("get_a"):
90 with m
.State("get_b"):
91 #self.get_op(m, self.in_b, b, "special_cases")
95 # special cases: NaNs, infs, zeros, denormalised
96 # NOTE: some of these are unique to add. see "Special Operations"
97 # https://steve.hollasch.net/cgindex/coding/ieeefloat.html
99 with m
.State("special_cases"):
102 m
.d
.comb
+= s_nomatch
.eq(a
.s
!= b
.s
)
105 m
.d
.comb
+= m_match
.eq(a
.m
== b
.m
)
107 # if a is NaN or b is NaN return NaN
108 with m
.If(a
.is_nan | b
.is_nan
):
112 # XXX WEIRDNESS for FP16 non-canonical NaN handling
115 ## if a is zero and b is NaN return -b
116 #with m.If(a.is_zero & (a.s==0) & b.is_nan):
118 # m.d.sync += z.create(b.s, b.e, Cat(b.m[3:-2], ~b.m[0]))
120 ## if b is zero and a is NaN return -a
121 #with m.Elif(b.is_zero & (b.s==0) & a.is_nan):
123 # m.d.sync += z.create(a.s, a.e, Cat(a.m[3:-2], ~a.m[0]))
125 ## if a is -zero and b is NaN return -b
126 #with m.Elif(a.is_zero & (a.s==1) & b.is_nan):
128 # m.d.sync += z.create(a.s & b.s, b.e, Cat(b.m[3:-2], 1))
130 ## if b is -zero and a is NaN return -a
131 #with m.Elif(b.is_zero & (b.s==1) & a.is_nan):
133 # m.d.sync += z.create(a.s & b.s, a.e, Cat(a.m[3:-2], 1))
135 # if a is inf return inf (or NaN)
136 with m
.Elif(a
.is_inf
):
138 m
.d
.sync
+= z
.inf(a
.s
)
139 # if a is inf and signs don't match return NaN
140 with m
.If(b
.exp_128
& s_nomatch
):
143 # if b is inf return inf
144 with m
.Elif(b
.is_inf
):
146 m
.d
.sync
+= z
.inf(b
.s
)
148 # if a is zero and b zero return signed-a/b
149 with m
.Elif(a
.is_zero
& b
.is_zero
):
151 m
.d
.sync
+= z
.create(a
.s
& b
.s
, b
.e
, b
.m
[3:-1])
153 # if a is zero return b
154 with m
.Elif(a
.is_zero
):
156 m
.d
.sync
+= z
.create(b
.s
, b
.e
, b
.m
[3:-1])
158 # if b is zero return a
159 with m
.Elif(b
.is_zero
):
161 m
.d
.sync
+= z
.create(a
.s
, a
.e
, a
.m
[3:-1])
163 # if a equal to -b return zero (+ve zero)
164 with m
.Elif(s_nomatch
& m_match
& (a
.e
== b
.e
)):
166 m
.d
.sync
+= z
.zero(0)
168 # Denormalised Number checks
170 m
.next
= "denormalise"
175 with m
.State("denormalise"):
176 # Denormalised Number checks
178 self
.denormalise(m
, a
)
179 self
.denormalise(m
, b
)
184 with m
.State("align"):
185 if not self
.single_cycle
:
186 # NOTE: this does *not* do single-cycle multi-shifting,
187 # it *STAYS* in the align state until exponents match
189 # exponent of a greater than b: shift b down
190 with m
.If(a
.e
> b
.e
):
191 m
.d
.sync
+= b
.shift_down()
192 # exponent of b greater than a: shift a down
193 with m
.Elif(a
.e
< b
.e
):
194 m
.d
.sync
+= a
.shift_down()
195 # exponents equal: move to next stage.
199 # This one however (single-cycle) will do the shift
202 # XXX TODO: the shifter used here is quite expensive
203 # having only one would be better
205 ediff
= Signal((len(a
.e
), True), reset_less
=True)
206 ediffr
= Signal((len(a
.e
), True), reset_less
=True)
207 m
.d
.comb
+= ediff
.eq(a
.e
- b
.e
)
208 m
.d
.comb
+= ediffr
.eq(b
.e
- a
.e
)
209 with m
.If(ediff
> 0):
210 m
.d
.sync
+= b
.shift_down_multi(ediff
)
211 # exponent of b greater than a: shift a down
212 with m
.Elif(ediff
< 0):
213 m
.d
.sync
+= a
.shift_down_multi(ediffr
)
218 # First stage of add. covers same-sign (add) and subtract
219 # special-casing when mantissas are greater or equal, to
220 # give greatest accuracy.
222 with m
.State("add_0"):
224 m
.d
.sync
+= z
.e
.eq(a
.e
)
225 # same-sign (both negative or both positive) add mantissas
226 with m
.If(a
.s
== b
.s
):
228 tot
.eq(Cat(a
.m
, 0) + Cat(b
.m
, 0)),
231 # a mantissa greater than b, use a
232 with m
.Elif(a
.m
>= b
.m
):
234 tot
.eq(Cat(a
.m
, 0) - Cat(b
.m
, 0)),
237 # b mantissa greater than a, use b
240 tot
.eq(Cat(b
.m
, 0) - Cat(a
.m
, 0)),
245 # Second stage of add: preparation for normalisation.
246 # detects when tot sum is too big (tot[27] is kinda a carry bit)
248 with m
.State("add_1"):
249 m
.next
= "normalise_1"
250 # tot[27] gets set when the sum overflows. shift result down
256 of
.round_bit
.eq(tot
[2]),
257 of
.sticky
.eq(tot
[1] | tot
[0]),
266 of
.round_bit
.eq(tot
[1]),
271 # First stage of normalisation.
273 with m
.State("normalise_1"):
274 self
.normalise_1(m
, z
, of
, "normalise_2")
277 # Second stage of normalisation.
279 with m
.State("normalise_2"):
280 self
.normalise_2(m
, z
, of
, "round")
285 with m
.State("round"):
286 self
.roundz(m
, z
, of
, "corrections")
291 with m
.State("corrections"):
292 self
.corrections(m
, z
, "pack")
297 with m
.State("pack"):
298 self
.pack(m
, z
, "put_z")
303 with m
.State("put_z"):
304 self
.put_z(m
, z
, self
.out_z
, "get_a")
309 if __name__
== "__main__":
310 alu
= FPADD(width
=32)
311 main(alu
, ports
=alu
.in_a
.ports() + alu
.in_b
.ports() + alu
.out_z
.ports())
314 # works... but don't use, just do "python fname.py convert -t v"
315 #print (verilog.convert(alu, ports=[
316 # ports=alu.in_a.ports() + \
317 # alu.in_b.ports() + \