1 # IEEE Floating Point Divider (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
5 from nmigen
import Module
, Signal
, Const
, Cat
6 from nmigen
.cli
import main
, verilog
8 from fpbase
import FPNumIn
, FPNumOut
, FPOp
, Overflow
, FPBase
9 from nmigen_add_experiment
import FPState
12 def __init__(self
, width
):
14 self
.quot
= Signal(width
) # quotient
15 self
.dor
= Signal(width
) # divisor
16 self
.dend
= Signal(width
) # dividend
17 self
.rem
= Signal(width
) # remainder
18 self
.count
= Signal(7) # loop count
20 self
.czero
= Const(0, width
)
24 self
.quot
.eq(self
.czero
),
25 self
.rem
.eq(self
.czero
),
26 self
.count
.eq(Const(0, 7))
32 def __init__(self
, width
):
36 self
.in_a
= FPOp(width
)
37 self
.in_b
= FPOp(width
)
38 self
.out_z
= FPOp(width
)
42 def add_state(self
, state
):
43 self
.states
.append(state
)
46 def get_fragment(self
, platform
=None):
47 """ creates the HDL code-fragment for FPDiv
52 a
= FPNumIn(None, self
.width
, False)
53 b
= FPNumIn(None, self
.width
, False)
54 z
= FPNumOut(self
.width
, False)
56 div
= Div(a
.m_width
*2 + 3) # double the mantissa width plus g/r/sticky
69 with m
.State("get_a"):
70 self
.get_op(m
, self
.in_a
, a
, "get_b")
75 with m
.State("get_b"):
76 self
.get_op(m
, self
.in_b
, b
, "special_cases")
79 # special cases: NaNs, infs, zeros, denormalised
80 # NOTE: some of these are unique to div. see "Special Operations"
81 # https://steve.hollasch.net/cgindex/coding/ieeefloat.html
83 with m
.State("special_cases"):
85 # if a is NaN or b is NaN return NaN
86 with m
.If(a
.is_nan | b
.is_nan
):
90 # if a is Inf and b is Inf return NaN
91 with m
.Elif(a
.is_inf
& b
.is_inf
):
95 # if a is inf return inf (or NaN if b is zero)
96 with m
.Elif(a
.is_inf
):
98 m
.d
.sync
+= z
.inf(a
.s ^ b
.s
)
100 # if b is inf return zero
101 with m
.Elif(b
.is_inf
):
103 m
.d
.sync
+= z
.zero(a
.s ^ b
.s
)
105 # if a is zero return zero (or NaN if b is zero)
106 with m
.Elif(a
.is_zero
):
108 # if b is zero return NaN
109 with m
.If(b
.is_zero
):
112 m
.d
.sync
+= z
.zero(a
.s ^ b
.s
)
114 # if b is zero return Inf
115 with m
.Elif(b
.is_zero
):
117 m
.d
.sync
+= z
.inf(a
.s ^ b
.s
)
119 # Denormalised Number checks
121 m
.next
= "normalise_a"
122 self
.denormalise(m
, a
)
123 self
.denormalise(m
, b
)
128 with m
.State("normalise_a"):
129 self
.op_normalise(m
, a
, "normalise_b")
134 with m
.State("normalise_b"):
135 self
.op_normalise(m
, b
, "divide_0")
138 # First stage of divide. initialise state
140 with m
.State("divide_0"):
143 z
.s
.eq(a
.s ^ b
.s
), # sign
144 z
.e
.eq(a
.e
- b
.e
), # exponent
145 div
.dend
.eq(a
.m
<<(a
.m_width
+3)), # 3 bits for g/r/sticky
151 # Second stage of divide.
153 with m
.State("divide_1"):
156 div
.quot
.eq(div
.quot
<< 1),
157 div
.rem
.eq(Cat(div
.dend
[-1], div
.rem
[0:])),
158 div
.dend
.eq(div
.dend
<< 1),
162 # Third stage of divide.
163 # This stage ends by jumping out to divide_3
164 # However it defaults to jumping to divide_1 (which comes back here)
166 with m
.State("divide_2"):
167 with m
.If(div
.rem
>= div
.dor
):
170 div
.rem
.eq(div
.rem
- div
.dor
),
172 with m
.If(div
.count
== div
.width
-2):
177 div
.count
.eq(div
.count
+ 1),
181 # Fourth stage of divide.
183 with m
.State("divide_3"):
184 m
.next
= "normalise_1"
186 z
.m
.eq(div
.quot
[3:]),
187 of
.guard
.eq(div
.quot
[2]),
188 of
.round_bit
.eq(div
.quot
[1]),
189 of
.sticky
.eq(div
.quot
[0] |
(div
.rem
!= 0))
193 # First stage of normalisation.
195 with m
.State("normalise_1"):
196 self
.normalise_1(m
, z
, of
, "normalise_2")
199 # Second stage of normalisation.
201 with m
.State("normalise_2"):
202 self
.normalise_2(m
, z
, of
, "round")
207 with m
.State("round"):
208 self
.roundz(m
, z
, of
.roundz
)
209 m
.next
= "corrections"
214 with m
.State("corrections"):
215 self
.corrections(m
, z
, "pack")
220 with m
.State("pack"):
221 self
.pack(m
, z
, "put_z")
226 with m
.State("put_z"):
227 self
.put_z(m
, z
, self
.out_z
, "get_a")
232 if __name__
== "__main__":
233 alu
= FPDIV(width
=32)
234 main(alu
, ports
=alu
.in_a
.ports() + alu
.in_b
.ports() + alu
.out_z
.ports())
237 # works... but don't use, just do "python fname.py convert -t v"
238 #print (verilog.convert(alu, ports=[
239 # ports=alu.in_a.ports() + \
240 # alu.in_b.ports() + \