1 # IEEE Floating Point Adder (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
5 from nmigen
import Module
, Signal
, Cat
, Mux
6 from nmigen
.lib
.coding
import PriorityEncoder
7 from nmigen
.cli
import main
, verilog
9 from fpbase
import FPNumIn
, FPNumOut
, FPOp
, Overflow
, FPBase
, FPNumBase
10 from fpbase
import MultiShiftRMerge
11 #from fpbase import FPNumShiftMultiRight
13 class FPState(FPBase
):
14 def __init__(self
, state_from
):
15 self
.state_from
= state_from
17 def set_inputs(self
, inputs
):
19 for k
,v
in inputs
.items():
22 def set_outputs(self
, outputs
):
23 self
.outputs
= outputs
24 for k
,v
in outputs
.items():
29 def __init__(self
, width
):
30 self
.in_op
= FPOp(width
)
31 self
.out_op
= FPNumIn(self
.in_op
, width
)
32 self
.out_decode
= Signal(reset_less
=True)
34 def elaborate(self
, platform
):
36 m
.d
.comb
+= self
.out_decode
.eq((self
.in_op
.ack
) & (self
.in_op
.stb
))
37 #m.submodules.get_op_in = self.in_op
38 m
.submodules
.get_op_out
= self
.out_op
39 with m
.If(self
.out_decode
):
41 self
.out_op
.decode(self
.in_op
.v
),
46 class FPGetOp(FPState
):
50 def __init__(self
, in_state
, out_state
, in_op
, width
):
51 FPState
.__init
__(self
, in_state
)
52 self
.out_state
= out_state
53 self
.mod
= FPGetOpMod(width
)
55 self
.out_op
= FPNumIn(in_op
, width
)
56 self
.out_decode
= Signal(reset_less
=True)
58 def setup(self
, m
, in_op
):
59 """ links module to inputs and outputs
61 setattr(m
.submodules
, self
.state_from
, self
.mod
)
62 m
.d
.comb
+= self
.mod
.in_op
.copy(in_op
)
63 m
.d
.comb
+= self
.out_op
.v
.eq(self
.mod
.out_op
.v
)
64 m
.d
.comb
+= self
.out_decode
.eq(self
.mod
.out_decode
)
67 with m
.If(self
.out_decode
):
68 m
.next
= self
.out_state
71 self
.out_op
.copy(self
.mod
.out_op
)
74 m
.d
.sync
+= self
.in_op
.ack
.eq(1)
77 class FPAddSpecialCasesMod
:
78 """ special cases: NaNs, infs, zeros, denormalised
79 NOTE: some of these are unique to add. see "Special Operations"
80 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
83 def __init__(self
, width
):
84 self
.in_a
= FPNumBase(width
)
85 self
.in_b
= FPNumBase(width
)
86 self
.out_z
= FPNumOut(width
, False)
87 self
.out_do_z
= Signal(reset_less
=True)
89 def elaborate(self
, platform
):
92 m
.submodules
.sc_in_a
= self
.in_a
93 m
.submodules
.sc_in_b
= self
.in_b
94 m
.submodules
.sc_out_z
= self
.out_z
97 m
.d
.comb
+= s_nomatch
.eq(self
.in_a
.s
!= self
.in_b
.s
)
100 m
.d
.comb
+= m_match
.eq(self
.in_a
.m
== self
.in_b
.m
)
102 # if a is NaN or b is NaN return NaN
103 with m
.If(self
.in_a
.is_nan | self
.in_b
.is_nan
):
104 m
.d
.comb
+= self
.out_do_z
.eq(1)
105 m
.d
.comb
+= self
.out_z
.nan(0)
107 # XXX WEIRDNESS for FP16 non-canonical NaN handling
110 ## if a is zero and b is NaN return -b
111 #with m.If(a.is_zero & (a.s==0) & b.is_nan):
112 # m.d.comb += self.out_do_z.eq(1)
113 # m.d.comb += z.create(b.s, b.e, Cat(b.m[3:-2], ~b.m[0]))
115 ## if b is zero and a is NaN return -a
116 #with m.Elif(b.is_zero & (b.s==0) & a.is_nan):
117 # m.d.comb += self.out_do_z.eq(1)
118 # m.d.comb += z.create(a.s, a.e, Cat(a.m[3:-2], ~a.m[0]))
120 ## if a is -zero and b is NaN return -b
121 #with m.Elif(a.is_zero & (a.s==1) & b.is_nan):
122 # m.d.comb += self.out_do_z.eq(1)
123 # m.d.comb += z.create(a.s & b.s, b.e, Cat(b.m[3:-2], 1))
125 ## if b is -zero and a is NaN return -a
126 #with m.Elif(b.is_zero & (b.s==1) & a.is_nan):
127 # m.d.comb += self.out_do_z.eq(1)
128 # m.d.comb += z.create(a.s & b.s, a.e, Cat(a.m[3:-2], 1))
130 # if a is inf return inf (or NaN)
131 with m
.Elif(self
.in_a
.is_inf
):
132 m
.d
.comb
+= self
.out_do_z
.eq(1)
133 m
.d
.comb
+= self
.out_z
.inf(self
.in_a
.s
)
134 # if a is inf and signs don't match return NaN
135 with m
.If(self
.in_b
.exp_128
& s_nomatch
):
136 m
.d
.comb
+= self
.out_z
.nan(0)
138 # if b is inf return inf
139 with m
.Elif(self
.in_b
.is_inf
):
140 m
.d
.comb
+= self
.out_do_z
.eq(1)
141 m
.d
.comb
+= self
.out_z
.inf(self
.in_b
.s
)
143 # if a is zero and b zero return signed-a/b
144 with m
.Elif(self
.in_a
.is_zero
& self
.in_b
.is_zero
):
145 m
.d
.comb
+= self
.out_do_z
.eq(1)
146 m
.d
.comb
+= self
.out_z
.create(self
.in_a
.s
& self
.in_b
.s
,
150 # if a is zero return b
151 with m
.Elif(self
.in_a
.is_zero
):
152 m
.d
.comb
+= self
.out_do_z
.eq(1)
153 m
.d
.comb
+= self
.out_z
.create(self
.in_b
.s
, self
.in_b
.e
,
156 # if b is zero return a
157 with m
.Elif(self
.in_b
.is_zero
):
158 m
.d
.comb
+= self
.out_do_z
.eq(1)
159 m
.d
.comb
+= self
.out_z
.create(self
.in_a
.s
, self
.in_a
.e
,
162 # if a equal to -b return zero (+ve zero)
163 with m
.Elif(s_nomatch
& m_match
& (self
.in_a
.e
== self
.in_b
.e
)):
164 m
.d
.comb
+= self
.out_do_z
.eq(1)
165 m
.d
.comb
+= self
.out_z
.zero(0)
167 # Denormalised Number checks
169 m
.d
.comb
+= self
.out_do_z
.eq(0)
175 def __init__(self
, id_wid
):
178 self
.in_mid
= Signal(width
, reset_less
)
179 self
.out_mid
= Signal(width
, reset_less
)
186 m
.d
.sync
+= self
.out_mid
.eq(self
.in_mid
)
189 class FPAddSpecialCases(FPState
, FPID
):
190 """ special cases: NaNs, infs, zeros, denormalised
191 NOTE: some of these are unique to add. see "Special Operations"
192 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
195 def __init__(self
, width
, id_wid
):
196 FPState
.__init
__(self
, "special_cases")
197 FPID
.__init
__(self
, id_wid
)
198 self
.mod
= FPAddSpecialCasesMod(width
)
199 self
.out_z
= FPNumOut(width
, False)
200 self
.out_do_z
= Signal(reset_less
=True)
202 def setup(self
, m
, in_a
, in_b
, in_mid
):
203 """ links module to inputs and outputs
205 m
.submodules
.specialcases
= self
.mod
206 m
.d
.comb
+= self
.mod
.in_a
.copy(in_a
)
207 m
.d
.comb
+= self
.mod
.in_b
.copy(in_b
)
208 #m.d.comb += self.out_z.v.eq(self.mod.out_z.v)
209 m
.d
.comb
+= self
.out_do_z
.eq(self
.mod
.out_do_z
)
211 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
215 with m
.If(self
.out_do_z
):
216 m
.d
.sync
+= self
.out_z
.v
.eq(self
.mod
.out_z
.v
) # only take the output
219 m
.next
= "denormalise"
222 class FPAddDeNormMod(FPState
):
224 def __init__(self
, width
):
225 self
.in_a
= FPNumBase(width
)
226 self
.in_b
= FPNumBase(width
)
227 self
.out_a
= FPNumBase(width
)
228 self
.out_b
= FPNumBase(width
)
230 def elaborate(self
, platform
):
232 m
.submodules
.denorm_in_a
= self
.in_a
233 m
.submodules
.denorm_in_b
= self
.in_b
234 m
.submodules
.denorm_out_a
= self
.out_a
235 m
.submodules
.denorm_out_b
= self
.out_b
236 # hmmm, don't like repeating identical code
237 m
.d
.comb
+= self
.out_a
.copy(self
.in_a
)
238 with m
.If(self
.in_a
.exp_n127
):
239 m
.d
.comb
+= self
.out_a
.e
.eq(self
.in_a
.N126
) # limit a exponent
241 m
.d
.comb
+= self
.out_a
.m
[-1].eq(1) # set top mantissa bit
243 m
.d
.comb
+= self
.out_b
.copy(self
.in_b
)
244 with m
.If(self
.in_b
.exp_n127
):
245 m
.d
.comb
+= self
.out_b
.e
.eq(self
.in_b
.N126
) # limit a exponent
247 m
.d
.comb
+= self
.out_b
.m
[-1].eq(1) # set top mantissa bit
252 class FPAddDeNorm(FPState
):
254 def __init__(self
, width
):
255 FPState
.__init
__(self
, "denormalise")
256 self
.mod
= FPAddDeNormMod(width
)
257 self
.out_a
= FPNumBase(width
)
258 self
.out_b
= FPNumBase(width
)
260 def setup(self
, m
, in_a
, in_b
):
261 """ links module to inputs and outputs
263 m
.submodules
.denormalise
= self
.mod
264 m
.d
.comb
+= self
.mod
.in_a
.copy(in_a
)
265 m
.d
.comb
+= self
.mod
.in_b
.copy(in_b
)
268 # Denormalised Number checks
270 m
.d
.sync
+= self
.out_a
.copy(self
.mod
.out_a
)
271 m
.d
.sync
+= self
.out_b
.copy(self
.mod
.out_b
)
274 class FPAddAlignMultiMod(FPState
):
276 def __init__(self
, width
):
277 self
.in_a
= FPNumBase(width
)
278 self
.in_b
= FPNumBase(width
)
279 self
.out_a
= FPNumIn(None, width
)
280 self
.out_b
= FPNumIn(None, width
)
281 self
.exp_eq
= Signal(reset_less
=True)
283 def elaborate(self
, platform
):
284 # This one however (single-cycle) will do the shift
289 m
.submodules
.align_in_a
= self
.in_a
290 m
.submodules
.align_in_b
= self
.in_b
291 m
.submodules
.align_out_a
= self
.out_a
292 m
.submodules
.align_out_b
= self
.out_b
294 # NOTE: this does *not* do single-cycle multi-shifting,
295 # it *STAYS* in the align state until exponents match
297 # exponent of a greater than b: shift b down
298 m
.d
.comb
+= self
.exp_eq
.eq(0)
299 m
.d
.comb
+= self
.out_a
.copy(self
.in_a
)
300 m
.d
.comb
+= self
.out_b
.copy(self
.in_b
)
301 agtb
= Signal(reset_less
=True)
302 altb
= Signal(reset_less
=True)
303 m
.d
.comb
+= agtb
.eq(self
.in_a
.e
> self
.in_b
.e
)
304 m
.d
.comb
+= altb
.eq(self
.in_a
.e
< self
.in_b
.e
)
306 m
.d
.comb
+= self
.out_b
.shift_down(self
.in_b
)
307 # exponent of b greater than a: shift a down
309 m
.d
.comb
+= self
.out_a
.shift_down(self
.in_a
)
310 # exponents equal: move to next stage.
312 m
.d
.comb
+= self
.exp_eq
.eq(1)
316 class FPAddAlignMulti(FPState
):
318 def __init__(self
, width
):
319 FPState
.__init
__(self
, "align")
320 self
.mod
= FPAddAlignMultiMod(width
)
321 self
.out_a
= FPNumIn(None, width
)
322 self
.out_b
= FPNumIn(None, width
)
323 self
.exp_eq
= Signal(reset_less
=True)
325 def setup(self
, m
, in_a
, in_b
):
326 """ links module to inputs and outputs
328 m
.submodules
.align
= self
.mod
329 m
.d
.comb
+= self
.mod
.in_a
.copy(in_a
)
330 m
.d
.comb
+= self
.mod
.in_b
.copy(in_b
)
331 #m.d.comb += self.out_a.copy(self.mod.out_a)
332 #m.d.comb += self.out_b.copy(self.mod.out_b)
333 m
.d
.comb
+= self
.exp_eq
.eq(self
.mod
.exp_eq
)
336 m
.d
.sync
+= self
.out_a
.copy(self
.mod
.out_a
)
337 m
.d
.sync
+= self
.out_b
.copy(self
.mod
.out_b
)
338 with m
.If(self
.exp_eq
):
342 class FPAddAlignSingleMod
:
344 def __init__(self
, width
):
346 self
.in_a
= FPNumBase(width
)
347 self
.in_b
= FPNumBase(width
)
348 self
.out_a
= FPNumIn(None, width
)
349 self
.out_b
= FPNumIn(None, width
)
351 def elaborate(self
, platform
):
352 """ Aligns A against B or B against A, depending on which has the
353 greater exponent. This is done in a *single* cycle using
354 variable-width bit-shift
356 the shifter used here is quite expensive in terms of gates.
357 Mux A or B in (and out) into temporaries, as only one of them
358 needs to be aligned against the other
362 m
.submodules
.align_in_a
= self
.in_a
363 m
.submodules
.align_in_b
= self
.in_b
364 m
.submodules
.align_out_a
= self
.out_a
365 m
.submodules
.align_out_b
= self
.out_b
367 # temporary (muxed) input and output to be shifted
368 t_inp
= FPNumBase(self
.width
)
369 t_out
= FPNumIn(None, self
.width
)
370 espec
= (len(self
.in_a
.e
), True)
371 msr
= MultiShiftRMerge(self
.in_a
.m_width
, espec
)
372 m
.submodules
.align_t_in
= t_inp
373 m
.submodules
.align_t_out
= t_out
374 m
.submodules
.multishift_r
= msr
376 ediff
= Signal(espec
, reset_less
=True)
377 ediffr
= Signal(espec
, reset_less
=True)
378 tdiff
= Signal(espec
, reset_less
=True)
379 elz
= Signal(reset_less
=True)
380 egz
= Signal(reset_less
=True)
382 # connect multi-shifter to t_inp/out mantissa (and tdiff)
383 m
.d
.comb
+= msr
.inp
.eq(t_inp
.m
)
384 m
.d
.comb
+= msr
.diff
.eq(tdiff
)
385 m
.d
.comb
+= t_out
.m
.eq(msr
.m
)
386 m
.d
.comb
+= t_out
.e
.eq(t_inp
.e
+ tdiff
)
387 m
.d
.comb
+= t_out
.s
.eq(t_inp
.s
)
389 m
.d
.comb
+= ediff
.eq(self
.in_a
.e
- self
.in_b
.e
)
390 m
.d
.comb
+= ediffr
.eq(self
.in_b
.e
- self
.in_a
.e
)
391 m
.d
.comb
+= elz
.eq(self
.in_a
.e
< self
.in_b
.e
)
392 m
.d
.comb
+= egz
.eq(self
.in_a
.e
> self
.in_b
.e
)
394 # default: A-exp == B-exp, A and B untouched (fall through)
395 m
.d
.comb
+= self
.out_a
.copy(self
.in_a
)
396 m
.d
.comb
+= self
.out_b
.copy(self
.in_b
)
397 # only one shifter (muxed)
398 #m.d.comb += t_out.shift_down_multi(tdiff, t_inp)
399 # exponent of a greater than b: shift b down
401 m
.d
.comb
+= [t_inp
.copy(self
.in_b
),
403 self
.out_b
.copy(t_out
),
404 self
.out_b
.s
.eq(self
.in_b
.s
), # whoops forgot sign
406 # exponent of b greater than a: shift a down
408 m
.d
.comb
+= [t_inp
.copy(self
.in_a
),
410 self
.out_a
.copy(t_out
),
411 self
.out_a
.s
.eq(self
.in_a
.s
), # whoops forgot sign
416 class FPAddAlignSingle(FPState
):
418 def __init__(self
, width
):
419 FPState
.__init
__(self
, "align")
420 self
.mod
= FPAddAlignSingleMod(width
)
421 self
.out_a
= FPNumIn(None, width
)
422 self
.out_b
= FPNumIn(None, width
)
424 def setup(self
, m
, in_a
, in_b
):
425 """ links module to inputs and outputs
427 m
.submodules
.align
= self
.mod
428 m
.d
.comb
+= self
.mod
.in_a
.copy(in_a
)
429 m
.d
.comb
+= self
.mod
.in_b
.copy(in_b
)
432 # NOTE: could be done as comb
433 m
.d
.sync
+= self
.out_a
.copy(self
.mod
.out_a
)
434 m
.d
.sync
+= self
.out_b
.copy(self
.mod
.out_b
)
438 class FPAddStage0Mod
:
440 def __init__(self
, width
):
441 self
.in_a
= FPNumBase(width
)
442 self
.in_b
= FPNumBase(width
)
443 self
.in_z
= FPNumBase(width
, False)
444 self
.out_z
= FPNumBase(width
, False)
445 self
.out_tot
= Signal(self
.out_z
.m_width
+ 4, reset_less
=True)
447 def elaborate(self
, platform
):
449 m
.submodules
.add0_in_a
= self
.in_a
450 m
.submodules
.add0_in_b
= self
.in_b
451 m
.submodules
.add0_out_z
= self
.out_z
453 m
.d
.comb
+= self
.out_z
.e
.eq(self
.in_a
.e
)
455 # store intermediate tests (and zero-extended mantissas)
456 seq
= Signal(reset_less
=True)
457 mge
= Signal(reset_less
=True)
458 am0
= Signal(len(self
.in_a
.m
)+1, reset_less
=True)
459 bm0
= Signal(len(self
.in_b
.m
)+1, reset_less
=True)
460 m
.d
.comb
+= [seq
.eq(self
.in_a
.s
== self
.in_b
.s
),
461 mge
.eq(self
.in_a
.m
>= self
.in_b
.m
),
462 am0
.eq(Cat(self
.in_a
.m
, 0)),
463 bm0
.eq(Cat(self
.in_b
.m
, 0))
465 # same-sign (both negative or both positive) add mantissas
468 self
.out_tot
.eq(am0
+ bm0
),
469 self
.out_z
.s
.eq(self
.in_a
.s
)
471 # a mantissa greater than b, use a
474 self
.out_tot
.eq(am0
- bm0
),
475 self
.out_z
.s
.eq(self
.in_a
.s
)
477 # b mantissa greater than a, use b
480 self
.out_tot
.eq(bm0
- am0
),
481 self
.out_z
.s
.eq(self
.in_b
.s
)
486 class FPAddStage0(FPState
):
487 """ First stage of add. covers same-sign (add) and subtract
488 special-casing when mantissas are greater or equal, to
489 give greatest accuracy.
492 def __init__(self
, width
):
493 FPState
.__init
__(self
, "add_0")
494 self
.mod
= FPAddStage0Mod(width
)
495 self
.out_z
= FPNumBase(width
, False)
496 self
.out_tot
= Signal(self
.out_z
.m_width
+ 4, reset_less
=True)
498 def setup(self
, m
, in_a
, in_b
):
499 """ links module to inputs and outputs
501 m
.submodules
.add0
= self
.mod
503 m
.d
.comb
+= self
.mod
.in_a
.copy(in_a
)
504 m
.d
.comb
+= self
.mod
.in_b
.copy(in_b
)
508 # NOTE: these could be done as combinatorial (merge add0+add1)
509 m
.d
.sync
+= self
.out_z
.copy(self
.mod
.out_z
)
510 m
.d
.sync
+= self
.out_tot
.eq(self
.mod
.out_tot
)
513 class FPAddStage1Mod(FPState
):
514 """ Second stage of add: preparation for normalisation.
515 detects when tot sum is too big (tot[27] is kinda a carry bit)
518 def __init__(self
, width
):
519 self
.out_norm
= Signal(reset_less
=True)
520 self
.in_z
= FPNumBase(width
, False)
521 self
.in_tot
= Signal(self
.in_z
.m_width
+ 4, reset_less
=True)
522 self
.out_z
= FPNumBase(width
, False)
523 self
.out_of
= Overflow()
525 def elaborate(self
, platform
):
527 #m.submodules.norm1_in_overflow = self.in_of
528 #m.submodules.norm1_out_overflow = self.out_of
529 #m.submodules.norm1_in_z = self.in_z
530 #m.submodules.norm1_out_z = self.out_z
531 m
.d
.comb
+= self
.out_z
.copy(self
.in_z
)
532 # tot[27] gets set when the sum overflows. shift result down
533 with m
.If(self
.in_tot
[-1]):
535 self
.out_z
.m
.eq(self
.in_tot
[4:]),
536 self
.out_of
.m0
.eq(self
.in_tot
[4]),
537 self
.out_of
.guard
.eq(self
.in_tot
[3]),
538 self
.out_of
.round_bit
.eq(self
.in_tot
[2]),
539 self
.out_of
.sticky
.eq(self
.in_tot
[1] | self
.in_tot
[0]),
540 self
.out_z
.e
.eq(self
.in_z
.e
+ 1)
545 self
.out_z
.m
.eq(self
.in_tot
[3:]),
546 self
.out_of
.m0
.eq(self
.in_tot
[3]),
547 self
.out_of
.guard
.eq(self
.in_tot
[2]),
548 self
.out_of
.round_bit
.eq(self
.in_tot
[1]),
549 self
.out_of
.sticky
.eq(self
.in_tot
[0])
554 class FPAddStage1(FPState
):
556 def __init__(self
, width
):
557 FPState
.__init
__(self
, "add_1")
558 self
.mod
= FPAddStage1Mod(width
)
559 self
.out_z
= FPNumBase(width
, False)
560 self
.out_of
= Overflow()
561 self
.norm_stb
= Signal()
563 def setup(self
, m
, in_tot
, in_z
):
564 """ links module to inputs and outputs
566 m
.submodules
.add1
= self
.mod
568 m
.d
.comb
+= self
.mod
.in_z
.copy(in_z
)
569 m
.d
.comb
+= self
.mod
.in_tot
.eq(in_tot
)
571 m
.d
.sync
+= self
.norm_stb
.eq(0) # sets to zero when not in add1 state
574 m
.submodules
.add1_out_overflow
= self
.out_of
575 m
.d
.sync
+= self
.out_of
.copy(self
.mod
.out_of
)
576 m
.d
.sync
+= self
.out_z
.copy(self
.mod
.out_z
)
577 m
.d
.sync
+= self
.norm_stb
.eq(1)
578 m
.next
= "normalise_1"
581 class FPNorm1ModSingle
:
583 def __init__(self
, width
):
585 self
.in_select
= Signal(reset_less
=True)
586 self
.out_norm
= Signal(reset_less
=True)
587 self
.in_z
= FPNumBase(width
, False)
588 self
.in_of
= Overflow()
589 self
.temp_z
= FPNumBase(width
, False)
590 self
.temp_of
= Overflow()
591 self
.out_z
= FPNumBase(width
, False)
592 self
.out_of
= Overflow()
594 def elaborate(self
, platform
):
597 mwid
= self
.out_z
.m_width
+2
598 pe
= PriorityEncoder(mwid
)
599 m
.submodules
.norm_pe
= pe
601 m
.submodules
.norm1_out_z
= self
.out_z
602 m
.submodules
.norm1_out_overflow
= self
.out_of
603 m
.submodules
.norm1_temp_z
= self
.temp_z
604 m
.submodules
.norm1_temp_of
= self
.temp_of
605 m
.submodules
.norm1_in_z
= self
.in_z
606 m
.submodules
.norm1_in_overflow
= self
.in_of
608 in_z
= FPNumBase(self
.width
, False)
610 m
.submodules
.norm1_insel_z
= in_z
611 m
.submodules
.norm1_insel_overflow
= in_of
613 espec
= (len(in_z
.e
), True)
614 ediff_n126
= Signal(espec
, reset_less
=True)
615 msr
= MultiShiftRMerge(mwid
, espec
)
616 m
.submodules
.multishift_r
= msr
618 # select which of temp or in z/of to use
619 with m
.If(self
.in_select
):
620 m
.d
.comb
+= in_z
.copy(self
.in_z
)
621 m
.d
.comb
+= in_of
.copy(self
.in_of
)
623 m
.d
.comb
+= in_z
.copy(self
.temp_z
)
624 m
.d
.comb
+= in_of
.copy(self
.temp_of
)
625 # initialise out from in (overridden below)
626 m
.d
.comb
+= self
.out_z
.copy(in_z
)
627 m
.d
.comb
+= self
.out_of
.copy(in_of
)
628 # normalisation increase/decrease conditions
629 decrease
= Signal(reset_less
=True)
630 increase
= Signal(reset_less
=True)
631 m
.d
.comb
+= decrease
.eq(in_z
.m_msbzero
& in_z
.exp_gt_n126
)
632 m
.d
.comb
+= increase
.eq(in_z
.exp_lt_n126
)
633 m
.d
.comb
+= self
.out_norm
.eq(0) # loop-end condition
636 # *sigh* not entirely obvious: count leading zeros (clz)
637 # with a PriorityEncoder: to find from the MSB
638 # we reverse the order of the bits.
639 temp_m
= Signal(mwid
, reset_less
=True)
640 temp_s
= Signal(mwid
+1, reset_less
=True)
641 clz
= Signal((len(in_z
.e
), True), reset_less
=True)
642 # make sure that the amount to decrease by does NOT
643 # go below the minimum non-INF/NaN exponent
644 limclz
= Mux(in_z
.exp_sub_n126
> pe
.o
, pe
.o
,
647 # cat round and guard bits back into the mantissa
648 temp_m
.eq(Cat(in_of
.round_bit
, in_of
.guard
, in_z
.m
)),
649 pe
.i
.eq(temp_m
[::-1]), # inverted
650 clz
.eq(limclz
), # count zeros from MSB down
651 temp_s
.eq(temp_m
<< clz
), # shift mantissa UP
652 self
.out_z
.e
.eq(in_z
.e
- clz
), # DECREASE exponent
653 self
.out_z
.m
.eq(temp_s
[2:]), # exclude bits 0&1
654 self
.out_of
.m0
.eq(temp_s
[2]), # copy of mantissa[0]
655 # overflow in bits 0..1: got shifted too (leave sticky)
656 self
.out_of
.guard
.eq(temp_s
[1]), # guard
657 self
.out_of
.round_bit
.eq(temp_s
[0]), # round
660 with m
.Elif(increase
):
661 temp_m
= Signal(mwid
+1, reset_less
=True)
663 temp_m
.eq(Cat(in_of
.sticky
, in_of
.round_bit
, in_of
.guard
,
665 ediff_n126
.eq(in_z
.N126
- in_z
.e
),
666 # connect multi-shifter to inp/out mantissa (and ediff)
668 msr
.diff
.eq(ediff_n126
),
669 self
.out_z
.m
.eq(msr
.m
[3:]),
670 self
.out_of
.m0
.eq(temp_s
[3]), # copy of mantissa[0]
671 # overflow in bits 0..1: got shifted too (leave sticky)
672 self
.out_of
.guard
.eq(temp_s
[2]), # guard
673 self
.out_of
.round_bit
.eq(temp_s
[1]), # round
674 self
.out_of
.sticky
.eq(temp_s
[0]), # sticky
675 self
.out_z
.e
.eq(in_z
.e
+ ediff_n126
),
681 class FPNorm1ModMulti
:
683 def __init__(self
, width
, single_cycle
=True):
685 self
.in_select
= Signal(reset_less
=True)
686 self
.out_norm
= Signal(reset_less
=True)
687 self
.in_z
= FPNumBase(width
, False)
688 self
.in_of
= Overflow()
689 self
.temp_z
= FPNumBase(width
, False)
690 self
.temp_of
= Overflow()
691 self
.out_z
= FPNumBase(width
, False)
692 self
.out_of
= Overflow()
694 def elaborate(self
, platform
):
697 m
.submodules
.norm1_out_z
= self
.out_z
698 m
.submodules
.norm1_out_overflow
= self
.out_of
699 m
.submodules
.norm1_temp_z
= self
.temp_z
700 m
.submodules
.norm1_temp_of
= self
.temp_of
701 m
.submodules
.norm1_in_z
= self
.in_z
702 m
.submodules
.norm1_in_overflow
= self
.in_of
704 in_z
= FPNumBase(self
.width
, False)
706 m
.submodules
.norm1_insel_z
= in_z
707 m
.submodules
.norm1_insel_overflow
= in_of
709 # select which of temp or in z/of to use
710 with m
.If(self
.in_select
):
711 m
.d
.comb
+= in_z
.copy(self
.in_z
)
712 m
.d
.comb
+= in_of
.copy(self
.in_of
)
714 m
.d
.comb
+= in_z
.copy(self
.temp_z
)
715 m
.d
.comb
+= in_of
.copy(self
.temp_of
)
716 # initialise out from in (overridden below)
717 m
.d
.comb
+= self
.out_z
.copy(in_z
)
718 m
.d
.comb
+= self
.out_of
.copy(in_of
)
719 # normalisation increase/decrease conditions
720 decrease
= Signal(reset_less
=True)
721 increase
= Signal(reset_less
=True)
722 m
.d
.comb
+= decrease
.eq(in_z
.m_msbzero
& in_z
.exp_gt_n126
)
723 m
.d
.comb
+= increase
.eq(in_z
.exp_lt_n126
)
724 m
.d
.comb
+= self
.out_norm
.eq(decrease | increase
) # loop-end
728 self
.out_z
.e
.eq(in_z
.e
- 1), # DECREASE exponent
729 self
.out_z
.m
.eq(in_z
.m
<< 1), # shift mantissa UP
730 self
.out_z
.m
[0].eq(in_of
.guard
), # steal guard (was tot[2])
731 self
.out_of
.guard
.eq(in_of
.round_bit
), # round (was tot[1])
732 self
.out_of
.round_bit
.eq(0), # reset round bit
733 self
.out_of
.m0
.eq(in_of
.guard
),
736 with m
.Elif(increase
):
738 self
.out_z
.e
.eq(in_z
.e
+ 1), # INCREASE exponent
739 self
.out_z
.m
.eq(in_z
.m
>> 1), # shift mantissa DOWN
740 self
.out_of
.guard
.eq(in_z
.m
[0]),
741 self
.out_of
.m0
.eq(in_z
.m
[1]),
742 self
.out_of
.round_bit
.eq(in_of
.guard
),
743 self
.out_of
.sticky
.eq(in_of
.sticky | in_of
.round_bit
)
749 class FPNorm1(FPState
):
751 def __init__(self
, width
, single_cycle
=True):
752 FPState
.__init
__(self
, "normalise_1")
754 self
.mod
= FPNorm1ModSingle(width
)
756 self
.mod
= FPNorm1ModMulti(width
)
757 self
.stb
= Signal(reset_less
=True)
758 self
.ack
= Signal(reset
=0, reset_less
=True)
759 self
.out_norm
= Signal(reset_less
=True)
760 self
.in_accept
= Signal(reset_less
=True)
761 self
.temp_z
= FPNumBase(width
)
762 self
.temp_of
= Overflow()
763 self
.out_z
= FPNumBase(width
)
764 self
.out_roundz
= Signal(reset_less
=True)
766 def setup(self
, m
, in_z
, in_of
, norm_stb
):
767 """ links module to inputs and outputs
769 m
.submodules
.normalise_1
= self
.mod
771 m
.d
.comb
+= self
.mod
.in_z
.copy(in_z
)
772 m
.d
.comb
+= self
.mod
.in_of
.copy(in_of
)
774 m
.d
.comb
+= self
.mod
.in_select
.eq(self
.in_accept
)
775 m
.d
.comb
+= self
.mod
.temp_z
.copy(self
.temp_z
)
776 m
.d
.comb
+= self
.mod
.temp_of
.copy(self
.temp_of
)
778 m
.d
.comb
+= self
.out_z
.copy(self
.mod
.out_z
)
779 m
.d
.comb
+= self
.out_norm
.eq(self
.mod
.out_norm
)
781 m
.d
.comb
+= self
.stb
.eq(norm_stb
)
782 m
.d
.sync
+= self
.ack
.eq(0) # sets to zero when not in normalise_1 state
786 m
.d
.comb
+= self
.in_accept
.eq((~self
.ack
) & (self
.stb
))
787 m
.d
.sync
+= self
.temp_of
.copy(self
.mod
.out_of
)
788 m
.d
.sync
+= self
.temp_z
.copy(self
.out_z
)
789 with m
.If(self
.out_norm
):
790 with m
.If(self
.in_accept
):
795 m
.d
.sync
+= self
.ack
.eq(0)
797 # normalisation not required (or done).
799 m
.d
.sync
+= self
.ack
.eq(1)
800 m
.d
.sync
+= self
.out_roundz
.eq(self
.mod
.out_of
.roundz
)
805 def __init__(self
, width
):
806 self
.in_roundz
= Signal(reset_less
=True)
807 self
.in_z
= FPNumBase(width
, False)
808 self
.out_z
= FPNumBase(width
, False)
810 def elaborate(self
, platform
):
812 m
.d
.comb
+= self
.out_z
.copy(self
.in_z
)
813 with m
.If(self
.in_roundz
):
814 m
.d
.comb
+= self
.out_z
.m
.eq(self
.in_z
.m
+ 1) # mantissa rounds up
815 with m
.If(self
.in_z
.m
== self
.in_z
.m1s
): # all 1s
816 m
.d
.comb
+= self
.out_z
.e
.eq(self
.in_z
.e
+ 1) # exponent up
820 class FPRound(FPState
):
822 def __init__(self
, width
):
823 FPState
.__init
__(self
, "round")
824 self
.mod
= FPRoundMod(width
)
825 self
.out_z
= FPNumBase(width
)
827 def setup(self
, m
, in_z
, roundz
):
828 """ links module to inputs and outputs
830 m
.submodules
.roundz
= self
.mod
832 m
.d
.comb
+= self
.mod
.in_z
.copy(in_z
)
833 m
.d
.comb
+= self
.mod
.in_roundz
.eq(roundz
)
836 m
.d
.sync
+= self
.out_z
.copy(self
.mod
.out_z
)
837 m
.next
= "corrections"
840 class FPCorrectionsMod
:
842 def __init__(self
, width
):
843 self
.in_z
= FPNumOut(width
, False)
844 self
.out_z
= FPNumOut(width
, False)
846 def elaborate(self
, platform
):
848 m
.submodules
.corr_in_z
= self
.in_z
849 m
.submodules
.corr_out_z
= self
.out_z
850 m
.d
.comb
+= self
.out_z
.copy(self
.in_z
)
851 with m
.If(self
.in_z
.is_denormalised
):
852 m
.d
.comb
+= self
.out_z
.e
.eq(self
.in_z
.N127
)
856 class FPCorrections(FPState
):
858 def __init__(self
, width
):
859 FPState
.__init
__(self
, "corrections")
860 self
.mod
= FPCorrectionsMod(width
)
861 self
.out_z
= FPNumBase(width
)
863 def setup(self
, m
, in_z
):
864 """ links module to inputs and outputs
866 m
.submodules
.corrections
= self
.mod
867 m
.d
.comb
+= self
.mod
.in_z
.copy(in_z
)
870 m
.d
.sync
+= self
.out_z
.copy(self
.mod
.out_z
)
876 def __init__(self
, width
):
877 self
.in_z
= FPNumOut(width
, False)
878 self
.out_z
= FPNumOut(width
, False)
880 def elaborate(self
, platform
):
882 m
.submodules
.pack_in_z
= self
.in_z
883 with m
.If(self
.in_z
.is_overflowed
):
884 m
.d
.comb
+= self
.out_z
.inf(self
.in_z
.s
)
886 m
.d
.comb
+= self
.out_z
.create(self
.in_z
.s
, self
.in_z
.e
, self
.in_z
.m
)
890 class FPPack(FPState
):
892 def __init__(self
, width
):
893 FPState
.__init
__(self
, "pack")
894 self
.mod
= FPPackMod(width
)
895 self
.out_z
= FPNumOut(width
, False)
897 def setup(self
, m
, in_z
):
898 """ links module to inputs and outputs
900 m
.submodules
.pack
= self
.mod
901 m
.d
.comb
+= self
.mod
.in_z
.copy(in_z
)
904 m
.d
.sync
+= self
.out_z
.v
.eq(self
.mod
.out_z
.v
)
905 m
.next
= "pack_put_z"
908 class FPPutZ(FPState
):
910 def __init__(self
, state
, in_z
, out_z
):
911 FPState
.__init
__(self
, state
)
917 self
.out_z
.v
.eq(self
.in_z
.v
)
919 with m
.If(self
.out_z
.stb
& self
.out_z
.ack
):
920 m
.d
.sync
+= self
.out_z
.stb
.eq(0)
923 m
.d
.sync
+= self
.out_z
.stb
.eq(1)
928 def __init__(self
, width
, id_wid
=None, single_cycle
=False):
931 * width: bit-width of IEEE754. supported: 16, 32, 64
932 * id_wid: an identifier that is sync-connected to the input
933 * single_cycle: True indicates each stage to complete in 1 clock
935 FPID
.__init
__(self
, id_wid
)
937 self
.single_cycle
= single_cycle
939 self
.in_a
= FPOp(width
)
940 self
.in_b
= FPOp(width
)
941 self
.out_z
= FPOp(width
)
945 def add_state(self
, state
):
946 self
.states
.append(state
)
949 def get_fragment(self
, platform
=None):
950 """ creates the HDL code-fragment for FPAdd
953 m
.submodules
.in_a
= self
.in_a
954 m
.submodules
.in_b
= self
.in_b
955 m
.submodules
.out_z
= self
.out_z
957 geta
= self
.add_state(FPGetOp("get_a", "get_b",
958 self
.in_a
, self
.width
))
959 geta
.setup(m
, self
.in_a
)
962 getb
= self
.add_state(FPGetOp("get_b", "special_cases",
963 self
.in_b
, self
.width
))
964 getb
.setup(m
, self
.in_b
)
967 sc
= self
.add_state(FPAddSpecialCases(self
.width
, self
.id_wid
))
968 sc
.setup(m
, a
, b
, self
.in_mid
)
970 dn
= self
.add_state(FPAddDeNorm(self
.width
))
973 if self
.single_cycle
:
974 alm
= self
.add_state(FPAddAlignSingle(self
.width
))
975 alm
.setup(m
, dn
.out_a
, dn
.out_b
)
977 alm
= self
.add_state(FPAddAlignMulti(self
.width
))
978 alm
.setup(m
, dn
.out_a
, dn
.out_b
)
980 add0
= self
.add_state(FPAddStage0(self
.width
))
981 add0
.setup(m
, alm
.out_a
, alm
.out_b
)
983 add1
= self
.add_state(FPAddStage1(self
.width
))
984 add1
.setup(m
, add0
.out_tot
, add0
.out_z
)
986 n1
= self
.add_state(FPNorm1(self
.width
))
987 n1
.setup(m
, add1
.out_z
, add1
.out_of
, add1
.norm_stb
)
989 rn
= self
.add_state(FPRound(self
.width
))
990 rn
.setup(m
, n1
.out_z
, n1
.out_roundz
)
992 cor
= self
.add_state(FPCorrections(self
.width
))
993 cor
.setup(m
, rn
.out_z
)
995 pa
= self
.add_state(FPPack(self
.width
))
996 pa
.setup(m
, cor
.out_z
)
998 ppz
= self
.add_state(FPPutZ("pack_put_z", pa
.out_z
, self
.out_z
))
1000 pz
= self
.add_state(FPPutZ("put_z", sc
.out_z
, self
.out_z
))
1002 with m
.FSM() as fsm
:
1004 for state
in self
.states
:
1005 with m
.State(state
.state_from
):
1011 if __name__
== "__main__":
1012 alu
= FPADD(width
=32, single_cycle
=True)
1013 main(alu
, ports
=alu
.in_a
.ports() + alu
.in_b
.ports() + alu
.out_z
.ports())
1016 # works... but don't use, just do "python fname.py convert -t v"
1017 #print (verilog.convert(alu, ports=[
1018 # ports=alu.in_a.ports() + \
1019 # alu.in_b.ports() + \
1020 # alu.out_z.ports())