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 FPNum
, FPOp
, Overflow
, FPBase
13 def __init__(self
, width
, single_cycle
=False):
16 self
.single_cycle
= single_cycle
18 self
.in_a
= FPOp(width
)
19 self
.in_b
= FPOp(width
)
20 self
.out_z
= FPOp(width
)
22 def get_fragment(self
, platform
=None):
23 """ creates the HDL code-fragment for FPAdd
30 z
= FPNum(self
.width
, False)
32 m
.submodules
.fpnum_a
= a
33 m
.submodules
.fpnum_b
= b
34 m
.submodules
.fpnum_z
= z
37 tot
= Signal(w
, reset_less
=True) # sticky/round/guard, {mantissa} result, 1 overflow
46 with m
.State("get_a"):
47 self
.get_op(m
, self
.in_a
, a
, "get_b")
52 with m
.State("get_b"):
53 self
.get_op(m
, self
.in_b
, b
, "special_cases")
56 # special cases: NaNs, infs, zeros, denormalised
57 # NOTE: some of these are unique to add. see "Special Operations"
58 # https://steve.hollasch.net/cgindex/coding/ieeefloat.html
60 with m
.State("special_cases"):
63 m
.d
.comb
+= s_nomatch
.eq(a
.s
!= b
.s
)
66 m
.d
.comb
+= m_match
.eq(a
.m
== b
.m
)
68 # if a is NaN or b is NaN return NaN
69 with m
.If(a
.is_nan | b
.is_nan
):
73 # XXX WEIRDNESS for FP16 non-canonical NaN handling
76 ## if a is zero and b is NaN return -b
77 #with m.If(a.is_zero & (a.s==0) & b.is_nan):
79 # m.d.sync += z.create(b.s, b.e, Cat(b.m[3:-2], ~b.m[0]))
81 ## if b is zero and a is NaN return -a
82 #with m.Elif(b.is_zero & (b.s==0) & a.is_nan):
84 # m.d.sync += z.create(a.s, a.e, Cat(a.m[3:-2], ~a.m[0]))
86 ## if a is -zero and b is NaN return -b
87 #with m.Elif(a.is_zero & (a.s==1) & b.is_nan):
89 # m.d.sync += z.create(a.s & b.s, b.e, Cat(b.m[3:-2], 1))
91 ## if b is -zero and a is NaN return -a
92 #with m.Elif(b.is_zero & (b.s==1) & a.is_nan):
94 # m.d.sync += z.create(a.s & b.s, a.e, Cat(a.m[3:-2], 1))
96 # if a is inf return inf (or NaN)
97 with m
.Elif(a
.is_inf
):
99 m
.d
.sync
+= z
.inf(a
.s
)
100 # if a is inf and signs don't match return NaN
101 with m
.If(b
.exp_128
& s_nomatch
):
104 # if b is inf return inf
105 with m
.Elif(b
.is_inf
):
107 m
.d
.sync
+= z
.inf(b
.s
)
109 # if a is zero and b zero return signed-a/b
110 with m
.Elif(a
.is_zero
& b
.is_zero
):
112 m
.d
.sync
+= z
.create(a
.s
& b
.s
, b
.e
, b
.m
[3:-1])
114 # if a is zero return b
115 with m
.Elif(a
.is_zero
):
117 m
.d
.sync
+= z
.create(b
.s
, b
.e
, b
.m
[3:-1])
119 # if b is zero return a
120 with m
.Elif(b
.is_zero
):
122 m
.d
.sync
+= z
.create(a
.s
, a
.e
, a
.m
[3:-1])
124 # if a equal to -b return zero (+ve zero)
125 with m
.Elif(s_nomatch
& m_match
& (a
.e
== b
.e
)):
127 m
.d
.sync
+= z
.zero(0)
129 # Denormalised Number checks
132 self
.denormalise(m
, a
)
133 self
.denormalise(m
, b
)
138 with m
.State("align"):
139 if not self
.single_cycle
:
140 # NOTE: this does *not* do single-cycle multi-shifting,
141 # it *STAYS* in the align state until exponents match
143 # exponent of a greater than b: shift b down
144 with m
.If(a
.e
> b
.e
):
145 m
.d
.sync
+= b
.shift_down()
146 # exponent of b greater than a: shift a down
147 with m
.Elif(a
.e
< b
.e
):
148 m
.d
.sync
+= a
.shift_down()
149 # exponents equal: move to next stage.
153 # This one however (single-cycle) will do the shift
156 # XXX TODO: the shifter used here is quite expensive
157 # having only one would be better
159 ediff
= Signal((len(a
.e
), True), reset_less
=True)
160 ediffr
= Signal((len(a
.e
), True), reset_less
=True)
161 m
.d
.comb
+= ediff
.eq(a
.e
- b
.e
)
162 m
.d
.comb
+= ediffr
.eq(b
.e
- a
.e
)
163 with m
.If(ediff
> 0):
164 m
.d
.sync
+= b
.shift_down_multi(ediff
)
165 # exponent of b greater than a: shift a down
166 with m
.Elif(ediff
< 0):
167 m
.d
.sync
+= a
.shift_down_multi(ediffr
)
172 # First stage of add. covers same-sign (add) and subtract
173 # special-casing when mantissas are greater or equal, to
174 # give greatest accuracy.
176 with m
.State("add_0"):
178 m
.d
.sync
+= z
.e
.eq(a
.e
)
179 # same-sign (both negative or both positive) add mantissas
180 with m
.If(a
.s
== b
.s
):
182 tot
.eq(Cat(a
.m
, 0) + Cat(b
.m
, 0)),
185 # a mantissa greater than b, use a
186 with m
.Elif(a
.m
>= b
.m
):
188 tot
.eq(Cat(a
.m
, 0) - Cat(b
.m
, 0)),
191 # b mantissa greater than a, use b
194 tot
.eq(Cat(b
.m
, 0) - Cat(a
.m
, 0)),
199 # Second stage of add: preparation for normalisation.
200 # detects when tot sum is too big (tot[27] is kinda a carry bit)
202 with m
.State("add_1"):
203 m
.next
= "normalise_1"
204 # tot[27] gets set when the sum overflows. shift result down
209 of
.round_bit
.eq(tot
[2]),
210 of
.sticky
.eq(tot
[1] | tot
[0]),
218 of
.round_bit
.eq(tot
[1]),
223 # First stage of normalisation.
225 with m
.State("normalise_1"):
226 self
.normalise_1(m
, z
, of
, "normalise_2")
229 # Second stage of normalisation.
231 with m
.State("normalise_2"):
232 self
.normalise_2(m
, z
, of
, "round")
237 with m
.State("round"):
238 self
.roundz(m
, z
, of
, "corrections")
243 with m
.State("corrections"):
244 self
.corrections(m
, z
, "pack")
249 with m
.State("pack"):
250 self
.pack(m
, z
, "put_z")
255 with m
.State("put_z"):
256 self
.put_z(m
, z
, self
.out_z
, "get_a")
261 if __name__
== "__main__":
262 alu
= FPADD(width
=32)
263 main(alu
, ports
=alu
.in_a
.ports() + alu
.in_b
.ports() + alu
.out_z
.ports())
266 # works... but don't use, just do "python fname.py convert -t v"
267 #print (verilog.convert(alu, ports=[
268 # ports=alu.in_a.ports() + \
269 # alu.in_b.ports() + \