1 # IEEE Floating Point Adder (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
5 from nmigen
import Module
, Signal
, Cat
6 from nmigen
.lib
.coding
import PriorityEncoder
7 from nmigen
.cli
import main
, verilog
9 from fpbase
import FPNumIn
, FPNumOut
, FPOp
, Overflow
, FPBase
, FPNumBase
12 class FPState(FPBase
):
13 def __init__(self
, state_from
):
14 self
.state_from
= state_from
16 def set_inputs(self
, inputs
):
18 for k
,v
in inputs
.items():
21 def set_outputs(self
, outputs
):
22 self
.outputs
= outputs
23 for k
,v
in outputs
.items():
28 def __init__(self
, width
):
29 self
.in_op
= FPOp(width
)
30 self
.out_op
= FPNumIn(self
.in_op
, width
)
31 self
.out_decode
= Signal(reset_less
=True)
33 def setup(self
, m
, in_op
, out_op
, out_decode
):
34 """ links module to inputs and outputs
36 m
.d
.comb
+= self
.in_op
.copy(in_op
)
37 m
.d
.comb
+= out_op
.v
.eq(self
.out_op
.v
)
38 m
.d
.comb
+= out_decode
.eq(self
.out_decode
)
40 def elaborate(self
, platform
):
42 m
.d
.comb
+= self
.out_decode
.eq((self
.in_op
.ack
) & (self
.in_op
.stb
))
43 #m.submodules.get_op_in = self.in_op
44 m
.submodules
.get_op_out
= self
.out_op
45 with m
.If(self
.out_decode
):
47 self
.out_op
.decode(self
.in_op
.v
),
52 class FPGetOp(FPState
):
56 def __init__(self
, in_state
, out_state
, in_op
, width
):
57 FPState
.__init
__(self
, in_state
)
58 self
.out_state
= out_state
59 self
.mod
= FPGetOpMod(width
)
61 self
.out_op
= FPNumIn(in_op
, width
)
62 self
.out_decode
= Signal(reset_less
=True)
65 with m
.If(self
.out_decode
):
66 m
.next
= self
.out_state
69 self
.out_op
.copy(self
.mod
.out_op
)
72 m
.d
.sync
+= self
.in_op
.ack
.eq(1)
75 class FPGetOpB(FPState
):
79 def __init__(self
, in_b
, width
):
80 FPState
.__init
__(self
, "get_b")
82 self
.b
= FPNumIn(self
.in_b
, width
)
85 self
.get_op(m
, self
.in_b
, self
.b
, "special_cases")
88 class FPAddSpecialCasesMod
:
89 """ special cases: NaNs, infs, zeros, denormalised
90 NOTE: some of these are unique to add. see "Special Operations"
91 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
94 def __init__(self
, width
):
95 self
.in_a
= FPNumBase(width
)
96 self
.in_b
= FPNumBase(width
)
97 self
.out_z
= FPNumOut(width
, False)
98 self
.out_do_z
= Signal(reset_less
=True)
100 def setup(self
, m
, in_a
, in_b
, out_z
, out_do_z
):
101 """ links module to inputs and outputs
103 m
.d
.comb
+= self
.in_a
.copy(in_a
)
104 m
.d
.comb
+= self
.in_b
.copy(in_b
)
105 #m.d.comb += out_z.v.eq(self.out_z.v)
106 m
.d
.comb
+= out_do_z
.eq(self
.out_do_z
)
108 def elaborate(self
, platform
):
111 m
.submodules
.sc_in_a
= self
.in_a
112 m
.submodules
.sc_in_b
= self
.in_b
113 m
.submodules
.sc_out_z
= self
.out_z
116 m
.d
.comb
+= s_nomatch
.eq(self
.in_a
.s
!= self
.in_b
.s
)
119 m
.d
.comb
+= m_match
.eq(self
.in_a
.m
== self
.in_b
.m
)
121 # if a is NaN or b is NaN return NaN
122 with m
.If(self
.in_a
.is_nan | self
.in_b
.is_nan
):
123 m
.d
.comb
+= self
.out_do_z
.eq(1)
124 m
.d
.comb
+= self
.out_z
.nan(0)
126 # XXX WEIRDNESS for FP16 non-canonical NaN handling
129 ## if a is zero and b is NaN return -b
130 #with m.If(a.is_zero & (a.s==0) & b.is_nan):
131 # m.d.comb += self.out_do_z.eq(1)
132 # m.d.comb += z.create(b.s, b.e, Cat(b.m[3:-2], ~b.m[0]))
134 ## if b is zero and a is NaN return -a
135 #with m.Elif(b.is_zero & (b.s==0) & a.is_nan):
136 # m.d.comb += self.out_do_z.eq(1)
137 # m.d.comb += z.create(a.s, a.e, Cat(a.m[3:-2], ~a.m[0]))
139 ## if a is -zero and b is NaN return -b
140 #with m.Elif(a.is_zero & (a.s==1) & b.is_nan):
141 # m.d.comb += self.out_do_z.eq(1)
142 # m.d.comb += z.create(a.s & b.s, b.e, Cat(b.m[3:-2], 1))
144 ## if b is -zero and a is NaN return -a
145 #with m.Elif(b.is_zero & (b.s==1) & a.is_nan):
146 # m.d.comb += self.out_do_z.eq(1)
147 # m.d.comb += z.create(a.s & b.s, a.e, Cat(a.m[3:-2], 1))
149 # if a is inf return inf (or NaN)
150 with m
.Elif(self
.in_a
.is_inf
):
151 m
.d
.comb
+= self
.out_do_z
.eq(1)
152 m
.d
.comb
+= self
.out_z
.inf(self
.in_a
.s
)
153 # if a is inf and signs don't match return NaN
154 with m
.If(self
.in_b
.exp_128
& s_nomatch
):
155 m
.d
.comb
+= self
.out_z
.nan(0)
157 # if b is inf return inf
158 with m
.Elif(self
.in_b
.is_inf
):
159 m
.d
.comb
+= self
.out_do_z
.eq(1)
160 m
.d
.comb
+= self
.out_z
.inf(self
.in_b
.s
)
162 # if a is zero and b zero return signed-a/b
163 with m
.Elif(self
.in_a
.is_zero
& self
.in_b
.is_zero
):
164 m
.d
.comb
+= self
.out_do_z
.eq(1)
165 m
.d
.comb
+= self
.out_z
.create(self
.in_a
.s
& self
.in_b
.s
,
169 # if a is zero return b
170 with m
.Elif(self
.in_a
.is_zero
):
171 m
.d
.comb
+= self
.out_do_z
.eq(1)
172 m
.d
.comb
+= self
.out_z
.create(self
.in_b
.s
, self
.in_b
.e
,
175 # if b is zero return a
176 with m
.Elif(self
.in_b
.is_zero
):
177 m
.d
.comb
+= self
.out_do_z
.eq(1)
178 m
.d
.comb
+= self
.out_z
.create(self
.in_a
.s
, self
.in_a
.e
,
181 # if a equal to -b return zero (+ve zero)
182 with m
.Elif(s_nomatch
& m_match
& (self
.in_a
.e
== self
.in_b
.e
)):
183 m
.d
.comb
+= self
.out_do_z
.eq(1)
184 m
.d
.comb
+= self
.out_z
.zero(0)
186 # Denormalised Number checks
188 m
.d
.comb
+= self
.out_do_z
.eq(0)
193 class FPAddSpecialCases(FPState
):
194 """ special cases: NaNs, infs, zeros, denormalised
195 NOTE: some of these are unique to add. see "Special Operations"
196 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
199 def __init__(self
, width
):
200 FPState
.__init
__(self
, "special_cases")
201 self
.mod
= FPAddSpecialCasesMod(width
)
202 self
.out_z
= FPNumOut(width
, False)
203 self
.out_do_z
= Signal(reset_less
=True)
206 with m
.If(self
.out_do_z
):
207 m
.d
.sync
+= self
.out_z
.v
.eq(self
.mod
.out_z
.v
) # only take the output
210 m
.next
= "denormalise"
213 class FPAddDeNormMod(FPState
):
215 def __init__(self
, width
):
216 self
.in_a
= FPNumBase(width
)
217 self
.in_b
= FPNumBase(width
)
218 self
.out_a
= FPNumBase(width
)
219 self
.out_b
= FPNumBase(width
)
221 def setup(self
, m
, in_a
, in_b
, out_a
, out_b
):
222 """ links module to inputs and outputs
224 m
.d
.comb
+= self
.in_a
.copy(in_a
)
225 m
.d
.comb
+= self
.in_b
.copy(in_b
)
226 m
.d
.comb
+= out_a
.copy(self
.out_a
)
227 m
.d
.comb
+= out_b
.copy(self
.out_b
)
229 def elaborate(self
, platform
):
231 m
.submodules
.denorm_in_a
= self
.in_a
232 m
.submodules
.denorm_in_b
= self
.in_b
233 m
.submodules
.denorm_out_a
= self
.out_a
234 m
.submodules
.denorm_out_b
= self
.out_b
235 # hmmm, don't like repeating identical code
236 m
.d
.comb
+= self
.out_a
.copy(self
.in_a
)
237 with m
.If(self
.in_a
.exp_n127
):
238 m
.d
.comb
+= self
.out_a
.e
.eq(self
.in_a
.N126
) # limit a exponent
240 m
.d
.comb
+= self
.out_a
.m
[-1].eq(1) # set top mantissa bit
242 m
.d
.comb
+= self
.out_b
.copy(self
.in_b
)
243 with m
.If(self
.in_b
.exp_n127
):
244 m
.d
.comb
+= self
.out_b
.e
.eq(self
.in_b
.N126
) # limit a exponent
246 m
.d
.comb
+= self
.out_b
.m
[-1].eq(1) # set top mantissa bit
251 class FPAddDeNorm(FPState
):
253 def __init__(self
, width
):
254 FPState
.__init
__(self
, "denormalise")
255 self
.mod
= FPAddDeNormMod(width
)
256 self
.out_a
= FPNumBase(width
)
257 self
.out_b
= FPNumBase(width
)
260 # Denormalised Number checks
262 m
.d
.sync
+= self
.a
.copy(self
.out_a
)
263 m
.d
.sync
+= self
.b
.copy(self
.out_b
)
266 class FPAddAlignMultiMod(FPState
):
268 def __init__(self
, width
):
269 self
.in_a
= FPNumBase(width
)
270 self
.in_b
= FPNumBase(width
)
271 self
.out_a
= FPNumIn(None, width
)
272 self
.out_b
= FPNumIn(None, width
)
273 self
.exp_eq
= Signal(reset_less
=True)
275 def setup(self
, m
, in_a
, in_b
, out_a
, out_b
, exp_eq
):
276 """ links module to inputs and outputs
278 m
.d
.comb
+= self
.in_a
.copy(in_a
)
279 m
.d
.comb
+= self
.in_b
.copy(in_b
)
280 m
.d
.comb
+= out_a
.copy(self
.out_a
)
281 m
.d
.comb
+= out_b
.copy(self
.out_b
)
282 m
.d
.comb
+= exp_eq
.eq(self
.exp_eq
)
284 def elaborate(self
, platform
):
285 # This one however (single-cycle) will do the shift
290 #m.submodules.align_in_a = self.in_a
291 #m.submodules.align_in_b = self.in_b
292 m
.submodules
.align_out_a
= self
.out_a
293 m
.submodules
.align_out_b
= self
.out_b
295 # NOTE: this does *not* do single-cycle multi-shifting,
296 # it *STAYS* in the align state until exponents match
298 # exponent of a greater than b: shift b down
299 m
.d
.comb
+= self
.exp_eq
.eq(0)
300 m
.d
.comb
+= self
.out_a
.copy(self
.in_a
)
301 m
.d
.comb
+= self
.out_b
.copy(self
.in_b
)
302 agtb
= Signal(reset_less
=True)
303 altb
= Signal(reset_less
=True)
304 m
.d
.comb
+= agtb
.eq(self
.in_a
.e
> self
.in_b
.e
)
305 m
.d
.comb
+= altb
.eq(self
.in_a
.e
< self
.in_b
.e
)
307 m
.d
.comb
+= self
.out_b
.shift_down(self
.in_b
)
308 # exponent of b greater than a: shift a down
310 m
.d
.comb
+= self
.out_a
.shift_down(self
.in_a
)
311 # exponents equal: move to next stage.
313 m
.d
.comb
+= self
.exp_eq
.eq(1)
317 class FPAddAlignMulti(FPState
):
319 def __init__(self
, width
):
320 FPState
.__init
__(self
, "align")
321 self
.mod
= FPAddAlignMultiMod(width
)
322 self
.out_a
= FPNumIn(None, width
)
323 self
.out_b
= FPNumIn(None, width
)
324 self
.exp_eq
= Signal(reset_less
=True)
327 m
.d
.sync
+= self
.a
.copy(self
.out_a
)
328 m
.d
.sync
+= self
.b
.copy(self
.out_b
)
329 with m
.If(self
.exp_eq
):
333 class FPAddAlignSingleMod
:
335 def __init__(self
, width
):
336 self
.in_a
= FPNumBase(width
)
337 self
.in_b
= FPNumBase(width
)
338 self
.out_a
= FPNumIn(None, width
)
339 self
.out_b
= FPNumIn(None, width
)
340 #self.out_a = FPNumBase(width)
341 #self.out_b = FPNumBase(width)
343 def setup(self
, m
, in_a
, in_b
, out_a
, out_b
):
344 """ links module to inputs and outputs
346 m
.d
.comb
+= self
.in_a
.copy(in_a
)
347 m
.d
.comb
+= self
.in_b
.copy(in_b
)
348 m
.d
.comb
+= out_a
.copy(self
.out_a
)
349 m
.d
.comb
+= out_b
.copy(self
.out_b
)
351 def elaborate(self
, platform
):
352 # This one however (single-cycle) will do the shift
357 m
.submodules
.align_in_a
= self
.in_a
358 m
.submodules
.align_in_b
= self
.in_b
359 m
.submodules
.align_out_a
= self
.out_a
360 m
.submodules
.align_out_b
= self
.out_b
362 # XXX TODO: the shifter used here is quite expensive
363 # having only one would be better
365 ediff
= Signal((len(self
.in_a
.e
), True), reset_less
=True)
366 ediffr
= Signal((len(self
.in_a
.e
), True), reset_less
=True)
368 m
.d
.comb
+= ediff
.eq(self
.in_a
.e
- self
.in_b
.e
)
369 m
.d
.comb
+= ediffr
.eq(self
.in_b
.e
- self
.in_a
.e
)
370 m
.d
.comb
+= self
.out_a
.copy(self
.in_a
)
371 m
.d
.comb
+= self
.out_b
.copy(self
.in_b
)
372 with m
.If(ediff
> 0):
373 m
.d
.comb
+= self
.out_b
.shift_down_multi(ediff
, self
.in_b
)
374 # exponent of b greater than a: shift a down
375 with m
.Elif(ediff
< 0):
376 m
.d
.comb
+= self
.out_a
.shift_down_multi(ediffr
, self
.in_a
)
380 class FPAddAlignSingle(FPState
):
382 def __init__(self
, width
):
383 FPState
.__init
__(self
, "align")
384 self
.mod
= FPAddAlignSingleMod(width
)
385 self
.out_a
= FPNumIn(None, width
)
386 self
.out_b
= FPNumIn(None, width
)
389 m
.d
.sync
+= self
.a
.copy(self
.out_a
)
390 m
.d
.sync
+= self
.b
.copy(self
.out_b
)
394 class FPAddStage0Mod
:
396 def __init__(self
, width
):
397 self
.in_a
= FPNumBase(width
)
398 self
.in_b
= FPNumBase(width
)
399 self
.in_z
= FPNumBase(width
, False)
400 self
.out_z
= FPNumBase(width
, False)
401 self
.out_tot
= Signal(self
.out_z
.m_width
+ 4, reset_less
=True)
403 def elaborate(self
, platform
):
405 m
.submodules
.add0_in_a
= self
.in_a
406 m
.submodules
.add0_in_b
= self
.in_b
407 m
.submodules
.add0_out_z
= self
.out_z
409 m
.d
.comb
+= self
.out_z
.e
.eq(self
.in_a
.e
)
411 # store intermediate tests (and zero-extended mantissas)
412 seq
= Signal(reset_less
=True)
413 mge
= Signal(reset_less
=True)
414 am0
= Signal(len(self
.in_a
.m
)+1, reset_less
=True)
415 bm0
= Signal(len(self
.in_b
.m
)+1, reset_less
=True)
416 m
.d
.comb
+= [seq
.eq(self
.in_a
.s
== self
.in_b
.s
),
417 mge
.eq(self
.in_a
.m
>= self
.in_b
.m
),
418 am0
.eq(Cat(self
.in_a
.m
, 0)),
419 bm0
.eq(Cat(self
.in_b
.m
, 0))
421 # same-sign (both negative or both positive) add mantissas
424 self
.out_tot
.eq(am0
+ bm0
),
425 self
.out_z
.s
.eq(self
.in_a
.s
)
427 # a mantissa greater than b, use a
430 self
.out_tot
.eq(am0
- bm0
),
431 self
.out_z
.s
.eq(self
.in_a
.s
)
433 # b mantissa greater than a, use b
436 self
.out_tot
.eq(bm0
- am0
),
437 self
.out_z
.s
.eq(self
.in_b
.s
)
442 class FPAddStage0(FPState
):
443 """ First stage of add. covers same-sign (add) and subtract
444 special-casing when mantissas are greater or equal, to
445 give greatest accuracy.
448 def __init__(self
, width
):
449 FPState
.__init
__(self
, "add_0")
450 self
.mod
= FPAddStage0Mod(width
)
451 self
.out_z
= FPNumBase(width
, False)
452 self
.out_tot
= Signal(self
.out_z
.m_width
+ 4, reset_less
=True)
454 def setup(self
, m
, in_a
, in_b
):
455 """ links module to inputs and outputs
457 m
.submodules
.add0
= self
.mod
459 m
.d
.comb
+= self
.mod
.in_a
.copy(in_a
)
460 m
.d
.comb
+= self
.mod
.in_b
.copy(in_b
)
464 # NOTE: these could be done as combinatorial (merge add0+add1)
465 m
.d
.sync
+= self
.out_z
.copy(self
.mod
.out_z
)
466 m
.d
.sync
+= self
.out_tot
.eq(self
.mod
.out_tot
)
469 class FPAddStage1Mod(FPState
):
470 """ Second stage of add: preparation for normalisation.
471 detects when tot sum is too big (tot[27] is kinda a carry bit)
474 def __init__(self
, width
):
475 self
.out_norm
= Signal(reset_less
=True)
476 self
.in_z
= FPNumBase(width
, False)
477 self
.in_tot
= Signal(self
.in_z
.m_width
+ 4, reset_less
=True)
478 self
.out_z
= FPNumBase(width
, False)
479 self
.out_of
= Overflow()
481 def elaborate(self
, platform
):
483 #m.submodules.norm1_in_overflow = self.in_of
484 #m.submodules.norm1_out_overflow = self.out_of
485 #m.submodules.norm1_in_z = self.in_z
486 #m.submodules.norm1_out_z = self.out_z
487 m
.d
.comb
+= self
.out_z
.copy(self
.in_z
)
488 # tot[27] gets set when the sum overflows. shift result down
489 with m
.If(self
.in_tot
[-1]):
491 self
.out_z
.m
.eq(self
.in_tot
[4:]),
492 self
.out_of
.m0
.eq(self
.in_tot
[4]),
493 self
.out_of
.guard
.eq(self
.in_tot
[3]),
494 self
.out_of
.round_bit
.eq(self
.in_tot
[2]),
495 self
.out_of
.sticky
.eq(self
.in_tot
[1] | self
.in_tot
[0]),
496 self
.out_z
.e
.eq(self
.in_z
.e
+ 1)
501 self
.out_z
.m
.eq(self
.in_tot
[3:]),
502 self
.out_of
.m0
.eq(self
.in_tot
[3]),
503 self
.out_of
.guard
.eq(self
.in_tot
[2]),
504 self
.out_of
.round_bit
.eq(self
.in_tot
[1]),
505 self
.out_of
.sticky
.eq(self
.in_tot
[0])
510 class FPAddStage1(FPState
):
512 def __init__(self
, width
):
513 FPState
.__init
__(self
, "add_1")
514 self
.mod
= FPAddStage1Mod(width
)
515 self
.out_z
= FPNumBase(width
, False)
516 self
.out_of
= Overflow()
517 self
.norm_stb
= Signal()
519 def setup(self
, m
, in_tot
, in_z
):
520 """ links module to inputs and outputs
522 m
.submodules
.add1
= self
.mod
524 m
.d
.comb
+= self
.mod
.in_z
.copy(in_z
)
525 m
.d
.comb
+= self
.mod
.in_tot
.eq(in_tot
)
527 m
.d
.sync
+= self
.norm_stb
.eq(0) # sets to zero when not in add1 state
530 m
.submodules
.add1_out_overflow
= self
.out_of
531 m
.d
.sync
+= self
.out_of
.copy(self
.mod
.out_of
)
532 m
.d
.sync
+= self
.out_z
.copy(self
.mod
.out_z
)
533 m
.d
.sync
+= self
.norm_stb
.eq(1)
534 m
.next
= "normalise_1"
539 def __init__(self
, width
, single_cycle
=True):
540 self
.single_cycle
= single_cycle
542 self
.in_select
= Signal(reset_less
=True)
543 self
.out_norm
= Signal(reset_less
=True)
544 self
.in_z
= FPNumBase(width
, False)
545 self
.in_of
= Overflow()
546 self
.temp_z
= FPNumBase(width
, False)
547 self
.temp_of
= Overflow()
548 self
.out_z
= FPNumBase(width
, False)
549 self
.out_of
= Overflow()
551 def elaborate(self
, platform
):
553 mwid
= self
.out_z
.m_width
+2
554 pe
= PriorityEncoder(mwid
)
555 m
.submodules
.norm_pe
= pe
556 m
.submodules
.norm1_out_z
= self
.out_z
557 m
.submodules
.norm1_out_overflow
= self
.out_of
558 m
.submodules
.norm1_temp_z
= self
.temp_z
559 m
.submodules
.norm1_temp_of
= self
.temp_of
560 m
.submodules
.norm1_in_z
= self
.in_z
561 m
.submodules
.norm1_in_overflow
= self
.in_of
562 in_z
= FPNumBase(self
.width
, False)
564 m
.submodules
.norm1_insel_z
= in_z
565 m
.submodules
.norm1_insel_overflow
= in_of
566 # select which of temp or in z/of to use
567 with m
.If(self
.in_select
):
568 m
.d
.comb
+= in_z
.copy(self
.in_z
)
569 m
.d
.comb
+= in_of
.copy(self
.in_of
)
571 m
.d
.comb
+= in_z
.copy(self
.temp_z
)
572 m
.d
.comb
+= in_of
.copy(self
.temp_of
)
573 # initialise out from in (overridden below)
574 m
.d
.comb
+= self
.out_z
.copy(in_z
)
575 m
.d
.comb
+= self
.out_of
.copy(in_of
)
576 # normalisation increase/decrease conditions
577 decrease
= Signal(reset_less
=True)
578 increase
= Signal(reset_less
=True)
579 m
.d
.comb
+= decrease
.eq(in_z
.m_msbzero
& in_z
.exp_gt_n126
)
580 m
.d
.comb
+= increase
.eq(in_z
.exp_lt_n126
)
581 m
.d
.comb
+= self
.out_norm
.eq(decrease | increase
) # loop-end condition
584 if not self
.single_cycle
:
586 self
.out_z
.e
.eq(in_z
.e
- 1), # DECREASE exponent
587 self
.out_z
.m
.eq(in_z
.m
<< 1), # shift mantissa UP
588 self
.out_z
.m
[0].eq(in_of
.guard
), # steal guard (was tot[2])
589 self
.out_of
.guard
.eq(in_of
.round_bit
), # round (was tot[1])
590 self
.out_of
.round_bit
.eq(0), # reset round bit
591 self
.out_of
.m0
.eq(in_of
.guard
),
594 # *sigh* not entirely obvious: count leading zeros (clz)
595 # with a PriorityEncoder: to find from the MSB
596 # we reverse the order of the bits.
597 temp_m
= Signal(mwid
, reset_less
=True)
598 temp_s
= Signal(mwid
+1, reset_less
=True)
599 clz
= Signal((len(in_z
.e
), True), reset_less
=True)
601 # cat round and guard bits back into the mantissa
602 temp_m
.eq(Cat(in_of
.round_bit
,
605 pe
.i
.eq(temp_m
[::-1]), # inverted
606 clz
.eq(pe
.o
), # count zeros from MSB down
607 temp_s
.eq(temp_m
<< clz
), # shift mantissa UP
608 self
.out_z
.e
.eq(in_z
.e
- clz
), # DECREASE exponent
609 self
.out_z
.m
.eq(temp_s
[2:]), # exclude bits 0&1
610 self
.out_of
.m0
.eq(temp_s
[2]), # copy of mantissa[0]
611 # overflow in bits 0..1: got shifted too (leave sticky)
612 self
.out_of
.guard
.eq(temp_s
[1]), # guard
613 self
.out_of
.round_bit
.eq(temp_s
[0]), # round
618 self
.out_z
.e
.eq(in_z
.e
+ 1), # INCREASE exponent
619 self
.out_z
.m
.eq(in_z
.m
>> 1), # shift mantissa DOWN
620 self
.out_of
.guard
.eq(in_z
.m
[0]),
621 self
.out_of
.m0
.eq(in_z
.m
[1]),
622 self
.out_of
.round_bit
.eq(in_of
.guard
),
623 self
.out_of
.sticky
.eq(in_of
.sticky | in_of
.round_bit
)
629 class FPNorm1(FPState
):
631 def __init__(self
, width
):
632 FPState
.__init
__(self
, "normalise_1")
633 self
.mod
= FPNorm1Mod(width
)
634 self
.stb
= Signal(reset_less
=True)
635 self
.ack
= Signal(reset
=0, reset_less
=True)
636 self
.out_norm
= Signal(reset_less
=True)
637 self
.in_accept
= Signal(reset_less
=True)
638 self
.temp_z
= FPNumBase(width
)
639 self
.temp_of
= Overflow()
640 self
.out_z
= FPNumBase(width
)
641 self
.out_roundz
= Signal(reset_less
=True)
643 def setup(self
, m
, in_z
, in_of
, norm_stb
):
644 """ links module to inputs and outputs
646 m
.submodules
.normalise_1
= self
.mod
648 m
.d
.comb
+= self
.mod
.in_z
.copy(in_z
)
649 m
.d
.comb
+= self
.mod
.in_of
.copy(in_of
)
651 m
.d
.comb
+= self
.mod
.in_select
.eq(self
.in_accept
)
652 m
.d
.comb
+= self
.mod
.temp_z
.copy(self
.temp_z
)
653 m
.d
.comb
+= self
.mod
.temp_of
.copy(self
.temp_of
)
655 m
.d
.comb
+= self
.out_z
.copy(self
.mod
.out_z
)
656 m
.d
.comb
+= self
.out_norm
.eq(self
.mod
.out_norm
)
658 m
.d
.comb
+= self
.stb
.eq(norm_stb
)
659 m
.d
.sync
+= self
.ack
.eq(0) # sets to zero when not in normalise_1 state
663 m
.d
.comb
+= self
.in_accept
.eq((~self
.ack
) & (self
.stb
))
664 m
.d
.sync
+= self
.temp_of
.copy(self
.mod
.out_of
)
665 m
.d
.sync
+= self
.temp_z
.copy(self
.out_z
)
666 with m
.If(self
.out_norm
):
667 with m
.If(self
.in_accept
):
672 m
.d
.sync
+= self
.ack
.eq(0)
674 # normalisation not required (or done).
676 m
.d
.sync
+= self
.ack
.eq(1)
677 m
.d
.sync
+= self
.out_roundz
.eq(self
.mod
.out_of
.roundz
)
682 def __init__(self
, width
):
683 self
.in_roundz
= Signal(reset_less
=True)
684 self
.in_z
= FPNumBase(width
, False)
685 self
.out_z
= FPNumBase(width
, False)
687 def elaborate(self
, platform
):
689 m
.d
.comb
+= self
.out_z
.copy(self
.in_z
)
690 with m
.If(self
.in_roundz
):
691 m
.d
.comb
+= self
.out_z
.m
.eq(self
.in_z
.m
+ 1) # mantissa rounds up
692 with m
.If(self
.in_z
.m
== self
.in_z
.m1s
): # all 1s
693 m
.d
.comb
+= self
.out_z
.e
.eq(self
.in_z
.e
+ 1) # exponent up
697 class FPRound(FPState
):
699 def __init__(self
, width
):
700 FPState
.__init
__(self
, "round")
701 self
.mod
= FPRoundMod(width
)
702 self
.out_z
= FPNumBase(width
)
704 def setup(self
, m
, in_z
, roundz
):
705 """ links module to inputs and outputs
707 m
.submodules
.roundz
= self
.mod
709 m
.d
.comb
+= self
.mod
.in_z
.copy(in_z
)
710 m
.d
.comb
+= self
.mod
.in_roundz
.eq(roundz
)
713 m
.d
.sync
+= self
.out_z
.copy(self
.mod
.out_z
)
714 m
.next
= "corrections"
717 class FPCorrectionsMod
:
719 def __init__(self
, width
):
720 self
.in_z
= FPNumOut(width
, False)
721 self
.out_z
= FPNumOut(width
, False)
723 def elaborate(self
, platform
):
725 m
.submodules
.corr_in_z
= self
.in_z
726 m
.submodules
.corr_out_z
= self
.out_z
727 m
.d
.comb
+= self
.out_z
.copy(self
.in_z
)
728 with m
.If(self
.in_z
.is_denormalised
):
729 m
.d
.comb
+= self
.out_z
.e
.eq(self
.in_z
.N127
)
731 # with m.If(self.in_z.is_overflowed):
732 # m.d.comb += self.out_z.inf(self.in_z.s)
734 # m.d.comb += self.out_z.create(self.in_z.s, self.in_z.e, self.in_z.m)
738 class FPCorrections(FPState
):
740 def __init__(self
, width
):
741 FPState
.__init
__(self
, "corrections")
742 self
.mod
= FPCorrectionsMod(width
)
743 self
.out_z
= FPNumBase(width
)
745 def setup(self
, m
, in_z
):
746 """ links module to inputs and outputs
748 m
.submodules
.corrections
= self
.mod
749 m
.d
.comb
+= self
.mod
.in_z
.copy(in_z
)
752 m
.d
.sync
+= self
.out_z
.copy(self
.mod
.out_z
)
758 def __init__(self
, width
):
759 self
.in_z
= FPNumOut(width
, False)
760 self
.out_z
= FPNumOut(width
, False)
762 def elaborate(self
, platform
):
764 m
.submodules
.pack_in_z
= self
.in_z
765 with m
.If(self
.in_z
.is_overflowed
):
766 m
.d
.comb
+= self
.out_z
.inf(self
.in_z
.s
)
768 m
.d
.comb
+= self
.out_z
.create(self
.in_z
.s
, self
.in_z
.e
, self
.in_z
.m
)
772 class FPPack(FPState
):
774 def __init__(self
, width
):
775 FPState
.__init
__(self
, "pack")
776 self
.mod
= FPPackMod(width
)
777 self
.out_z
= FPNumOut(width
, False)
779 def setup(self
, m
, in_z
):
780 """ links module to inputs and outputs
782 m
.submodules
.pack
= self
.mod
783 m
.d
.comb
+= self
.mod
.in_z
.copy(in_z
)
786 m
.d
.sync
+= self
.out_z
.v
.eq(self
.mod
.out_z
.v
)
787 m
.next
= "pack_put_z"
790 class FPPutZ(FPState
):
792 def __init__(self
, state
, in_z
, out_z
):
793 FPState
.__init
__(self
, state
)
799 self
.out_z
.v
.eq(self
.in_z
.v
)
801 with m
.If(self
.out_z
.stb
& self
.out_z
.ack
):
802 m
.d
.sync
+= self
.out_z
.stb
.eq(0)
805 m
.d
.sync
+= self
.out_z
.stb
.eq(1)
810 def __init__(self
, width
, single_cycle
=False):
812 self
.single_cycle
= single_cycle
814 self
.in_a
= FPOp(width
)
815 self
.in_b
= FPOp(width
)
816 self
.out_z
= FPOp(width
)
820 def add_state(self
, state
):
821 self
.states
.append(state
)
824 def get_fragment(self
, platform
=None):
825 """ creates the HDL code-fragment for FPAdd
828 m
.submodules
.in_a
= self
.in_a
829 m
.submodules
.in_b
= self
.in_b
830 m
.submodules
.out_z
= self
.out_z
832 geta
= self
.add_state(FPGetOp("get_a", "get_b",
833 self
.in_a
, self
.width
))
835 geta
.mod
.setup(m
, self
.in_a
, geta
.out_op
, geta
.out_decode
)
836 m
.submodules
.get_a
= geta
.mod
838 getb
= self
.add_state(FPGetOp("get_b", "special_cases",
839 self
.in_b
, self
.width
))
841 getb
.mod
.setup(m
, self
.in_b
, getb
.out_op
, getb
.out_decode
)
842 m
.submodules
.get_b
= getb
.mod
844 sc
= self
.add_state(FPAddSpecialCases(self
.width
))
845 sc
.mod
.setup(m
, a
, b
, sc
.out_z
, sc
.out_do_z
)
846 m
.submodules
.specialcases
= sc
.mod
848 dn
= self
.add_state(FPAddDeNorm(self
.width
))
849 dn
.set_inputs({"a": a
, "b": b
})
850 #dn.set_outputs({"a": a, "b": b}) # XXX outputs same as inputs
851 dn
.mod
.setup(m
, a
, b
, dn
.out_a
, dn
.out_b
)
852 m
.submodules
.denormalise
= dn
.mod
854 if self
.single_cycle
:
855 alm
= self
.add_state(FPAddAlignSingle(self
.width
))
856 alm
.set_inputs({"a": a
, "b": b
})
857 alm
.set_outputs({"a": a
, "b": b
}) # XXX outputs same as inputs
858 alm
.mod
.setup(m
, a
, b
, alm
.out_a
, alm
.out_b
)
860 alm
= self
.add_state(FPAddAlignMulti(self
.width
))
861 alm
.set_inputs({"a": a
, "b": b
})
862 #alm.set_outputs({"a": a, "b": b}) # XXX outputs same as inputs
863 alm
.mod
.setup(m
, a
, b
, alm
.out_a
, alm
.out_b
, alm
.exp_eq
)
864 m
.submodules
.align
= alm
.mod
866 add0
= self
.add_state(FPAddStage0(self
.width
))
867 add0
.setup(m
, alm
.out_a
, alm
.out_b
)
869 add1
= self
.add_state(FPAddStage1(self
.width
))
870 add1
.setup(m
, add0
.out_tot
, add0
.out_z
)
872 n1
= self
.add_state(FPNorm1(self
.width
))
873 n1
.setup(m
, add1
.out_z
, add1
.out_of
, add1
.norm_stb
)
875 rn
= self
.add_state(FPRound(self
.width
))
876 rn
.setup(m
, n1
.out_z
, n1
.out_roundz
)
878 cor
= self
.add_state(FPCorrections(self
.width
))
879 cor
.setup(m
, rn
.out_z
)
881 pa
= self
.add_state(FPPack(self
.width
))
882 pa
.setup(m
, cor
.out_z
)
884 ppz
= self
.add_state(FPPutZ("pack_put_z", pa
.out_z
, self
.out_z
))
886 pz
= self
.add_state(FPPutZ("put_z", sc
.out_z
, self
.out_z
))
890 for state
in self
.states
:
891 with m
.State(state
.state_from
):
897 if __name__
== "__main__":
898 alu
= FPADD(width
=32, single_cycle
=True)
899 main(alu
, ports
=alu
.in_a
.ports() + alu
.in_b
.ports() + alu
.out_z
.ports())
902 # works... but don't use, just do "python fname.py convert -t v"
903 #print (verilog.convert(alu, ports=[
904 # ports=alu.in_a.ports() + \
905 # alu.in_b.ports() + \