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 ieee754
.fpcommon
.fpbase
import (FPNumIn
, FPNumOut
, FPOpIn
,
9 FPOpOut
, Overflow
, FPBase
, FPState
)
10 from nmutil
.nmoperator
import eq
14 def __init__(self
, width
):
16 self
.quot
= Signal(width
) # quotient
17 self
.dor
= Signal(width
) # divisor
18 self
.dend
= Signal(width
) # dividend
19 self
.rem
= Signal(width
) # remainder
20 self
.count
= Signal(7) # loop count
22 self
.czero
= Const(0, width
)
26 self
.quot
.eq(self
.czero
),
27 self
.rem
.eq(self
.czero
),
28 self
.count
.eq(Const(0, 7))
34 def __init__(self
, width
):
38 self
.in_a
= FPOpIn(width
)
39 self
.in_b
= FPOpIn(width
)
40 self
.out_z
= FPOpOut(width
)
44 def add_state(self
, state
):
45 self
.states
.append(state
)
48 def elaborate(self
, platform
=None):
49 """ creates the HDL code-fragment for FPDiv
54 a
= FPNumIn(None, self
.width
, False)
55 b
= FPNumIn(None, self
.width
, False)
56 z
= FPNumOut(self
.width
, False)
58 div
= Div(a
.m_width
*2 + 3) # double the mantissa width plus g/r/sticky
66 m
.d
.comb
+= a
.v
.eq(self
.in_a
.v
)
67 m
.d
.comb
+= b
.v
.eq(self
.in_b
.v
)
74 with m
.State("get_a"):
75 res
= self
.get_op(m
, self
.in_a
, a
, "get_b")
76 m
.d
.sync
+= eq([a
, self
.in_a
.ready_o
], res
)
81 with m
.State("get_b"):
82 res
= self
.get_op(m
, self
.in_b
, b
, "special_cases")
83 m
.d
.sync
+= eq([b
, self
.in_b
.ready_o
], res
)
86 # special cases: NaNs, infs, zeros, denormalised
87 # NOTE: some of these are unique to div. see "Special Operations"
88 # https://steve.hollasch.net/cgindex/coding/ieeefloat.html
90 with m
.State("special_cases"):
92 # if a is NaN or b is NaN return NaN
93 with m
.If(a
.is_nan | b
.is_nan
):
97 # if a is Inf and b is Inf return NaN
98 with m
.Elif(a
.is_inf
& b
.is_inf
):
102 # if a is inf return inf (or NaN if b is zero)
103 with m
.Elif(a
.is_inf
):
105 m
.d
.sync
+= z
.inf(a
.s ^ b
.s
)
107 # if b is inf return zero
108 with m
.Elif(b
.is_inf
):
110 m
.d
.sync
+= z
.zero(a
.s ^ b
.s
)
112 # if a is zero return zero (or NaN if b is zero)
113 with m
.Elif(a
.is_zero
):
115 # if b is zero return NaN
116 with m
.If(b
.is_zero
):
119 m
.d
.sync
+= z
.zero(a
.s ^ b
.s
)
121 # if b is zero return Inf
122 with m
.Elif(b
.is_zero
):
124 m
.d
.sync
+= z
.inf(a
.s ^ b
.s
)
126 # Denormalised Number checks
128 m
.next
= "normalise_a"
129 self
.denormalise(m
, a
)
130 self
.denormalise(m
, b
)
135 with m
.State("normalise_a"):
136 self
.op_normalise(m
, a
, "normalise_b")
141 with m
.State("normalise_b"):
142 self
.op_normalise(m
, b
, "divide_0")
145 # First stage of divide. initialise state
147 with m
.State("divide_0"):
150 z
.s
.eq(a
.s ^ b
.s
), # sign
151 z
.e
.eq(a
.e
- b
.e
), # exponent
152 div
.dend
.eq(a
.m
<<(a
.m_width
+3)), # 3 bits for g/r/sticky
158 # Second stage of divide.
160 with m
.State("divide_1"):
163 div
.quot
.eq(div
.quot
<< 1),
164 div
.rem
.eq(Cat(div
.dend
[-1], div
.rem
[0:])),
165 div
.dend
.eq(div
.dend
<< 1),
169 # Third stage of divide.
170 # This stage ends by jumping out to divide_3
171 # However it defaults to jumping to divide_1 (which comes back here)
173 with m
.State("divide_2"):
174 with m
.If(div
.rem
>= div
.dor
):
177 div
.rem
.eq(div
.rem
- div
.dor
),
179 with m
.If(div
.count
== div
.width
-2):
184 div
.count
.eq(div
.count
+ 1),
188 # Fourth stage of divide.
190 with m
.State("divide_3"):
191 m
.next
= "normalise_1"
193 z
.m
.eq(div
.quot
[3:]),
194 of
.guard
.eq(div
.quot
[2]),
195 of
.round_bit
.eq(div
.quot
[1]),
196 of
.sticky
.eq(div
.quot
[0] |
(div
.rem
!= 0))
200 # First stage of normalisation.
202 with m
.State("normalise_1"):
203 self
.normalise_1(m
, z
, of
, "normalise_2")
206 # Second stage of normalisation.
208 with m
.State("normalise_2"):
209 self
.normalise_2(m
, z
, of
, "round")
214 with m
.State("round"):
215 self
.roundz(m
, z
, of
.roundz
)
216 m
.next
= "corrections"
221 with m
.State("corrections"):
222 self
.corrections(m
, z
, "pack")
227 with m
.State("pack"):
228 self
.pack(m
, z
, "put_z")
233 with m
.State("put_z"):
234 self
.put_z(m
, z
, self
.out_z
, "get_a")
239 if __name__
== "__main__":
240 alu
= FPDIV(width
=32)
241 main(alu
, ports
=alu
.in_a
.ports() + alu
.in_b
.ports() + alu
.out_z
.ports())
244 # works... but don't use, just do "python fname.py convert -t v"
245 #print (verilog.convert(alu, ports=[
246 # ports=alu.in_a.ports() + \
247 # alu.in_b.ports() + \