1 # IEEE Floating Point Adder (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
5 from nmigen
import Module
, Signal
, Cat
, Mux
, Array
, Const
6 from nmigen
.lib
.coding
import PriorityEncoder
7 from nmigen
.cli
import main
, verilog
10 from fpbase
import FPNumIn
, FPNumOut
, FPOp
, Overflow
, FPBase
, FPNumBase
11 from fpbase
import MultiShiftRMerge
, Trigger
12 #from fpbase import FPNumShiftMultiRight
15 class FPState(FPBase
):
16 def __init__(self
, state_from
):
17 self
.state_from
= state_from
19 def set_inputs(self
, inputs
):
21 for k
,v
in inputs
.items():
24 def set_outputs(self
, outputs
):
25 self
.outputs
= outputs
26 for k
,v
in outputs
.items():
30 class FPGetSyncOpsMod
:
31 def __init__(self
, width
, num_ops
=2):
33 self
.num_ops
= num_ops
36 for i
in range(num_ops
):
37 inops
.append(Signal(width
, reset_less
=True))
38 outops
.append(Signal(width
, reset_less
=True))
41 self
.stb
= Signal(num_ops
)
43 self
.ready
= Signal(reset_less
=True)
44 self
.out_decode
= Signal(reset_less
=True)
46 def elaborate(self
, platform
):
48 m
.d
.comb
+= self
.ready
.eq(self
.stb
== Const(-1, (self
.num_ops
, False)))
49 m
.d
.comb
+= self
.out_decode
.eq(self
.ack
& self
.ready
)
50 with m
.If(self
.out_decode
):
51 for i
in range(self
.num_ops
):
53 self
.out_op
[i
].eq(self
.in_op
[i
]),
58 return self
.in_op
+ self
.out_op
+ [self
.stb
, self
.ack
]
62 def __init__(self
, width
, num_ops
):
63 Trigger
.__init
__(self
)
65 self
.num_ops
= num_ops
68 for i
in range(num_ops
):
69 res
.append(Signal(width
))
74 for i
in range(self
.num_ops
):
82 def __init__(self
, width
, num_ops
=2, num_rows
=4):
84 self
.num_ops
= num_ops
85 self
.num_rows
= num_rows
86 self
.mmax
= int(log(self
.num_rows
) / log(2))
88 self
.mid
= Signal(self
.mmax
, reset_less
=True) # multiplex id
89 for i
in range(num_rows
):
90 self
.rs
.append(FPGetSyncOpsMod(width
, num_ops
))
91 self
.rs
= Array(self
.rs
)
93 self
.out_op
= FPOps(width
, num_ops
)
95 def elaborate(self
, platform
):
98 pe
= PriorityEncoder(self
.num_rows
)
99 m
.submodules
.selector
= pe
100 m
.submodules
.out_op
= self
.out_op
101 m
.submodules
+= self
.rs
103 # connect priority encoder
105 for i
in range(self
.num_rows
):
106 in_ready
.append(self
.rs
[i
].ready
)
107 m
.d
.comb
+= pe
.i
.eq(Cat(*in_ready
))
109 active
= Signal(reset_less
=True)
110 out_en
= Signal(reset_less
=True)
111 m
.d
.comb
+= active
.eq(~pe
.n
) # encoder active
112 m
.d
.comb
+= out_en
.eq(active
& self
.out_op
.trigger
)
114 # encoder active: ack relevant input, record MID, pass output
117 m
.d
.sync
+= self
.mid
.eq(pe
.o
)
118 m
.d
.sync
+= rs
.ack
.eq(0)
119 m
.d
.sync
+= self
.out_op
.stb
.eq(0)
120 for j
in range(self
.num_ops
):
121 m
.d
.sync
+= self
.out_op
.v
[j
].eq(rs
.out_op
[j
])
123 m
.d
.sync
+= self
.out_op
.stb
.eq(1)
124 # acks all default to zero
125 for i
in range(self
.num_rows
):
126 m
.d
.sync
+= self
.rs
[i
].ack
.eq(1)
132 for i
in range(self
.num_rows
):
134 res
+= inop
.in_op
+ [inop
.stb
]
135 return self
.out_op
.ports() + res
+ [self
.mid
]
139 def __init__(self
, width
):
140 self
.in_op
= FPOp(width
)
141 self
.out_op
= Signal(width
)
142 self
.out_decode
= Signal(reset_less
=True)
144 def elaborate(self
, platform
):
146 m
.d
.comb
+= self
.out_decode
.eq((self
.in_op
.ack
) & (self
.in_op
.stb
))
147 m
.submodules
.get_op_in
= self
.in_op
148 #m.submodules.get_op_out = self.out_op
149 with m
.If(self
.out_decode
):
151 self
.out_op
.eq(self
.in_op
.v
),
156 class FPGetOp(FPState
):
160 def __init__(self
, in_state
, out_state
, in_op
, width
):
161 FPState
.__init
__(self
, in_state
)
162 self
.out_state
= out_state
163 self
.mod
= FPGetOpMod(width
)
165 self
.out_op
= Signal(width
)
166 self
.out_decode
= Signal(reset_less
=True)
168 def setup(self
, m
, in_op
):
169 """ links module to inputs and outputs
171 setattr(m
.submodules
, self
.state_from
, self
.mod
)
172 m
.d
.comb
+= self
.mod
.in_op
.eq(in_op
)
173 #m.d.comb += self.out_op.eq(self.mod.out_op)
174 m
.d
.comb
+= self
.out_decode
.eq(self
.mod
.out_decode
)
177 with m
.If(self
.out_decode
):
178 m
.next
= self
.out_state
180 self
.in_op
.ack
.eq(0),
181 self
.out_op
.eq(self
.mod
.out_op
)
184 m
.d
.sync
+= self
.in_op
.ack
.eq(1)
187 class FPGet2OpMod(Trigger
):
188 def __init__(self
, width
, id_wid
):
189 Trigger
.__init
__(self
)
192 self
.in_op1
= Signal(width
, reset_less
=True)
193 self
.in_op2
= Signal(width
, reset_less
=True)
194 self
.o
= FPNumBase2Ops(width
, id_wid
)
197 return FPNumBase2Ops(self
.width
, self
.id_wid
)
199 def elaborate(self
, platform
):
200 m
= Trigger
.elaborate(self
, platform
)
201 #m.submodules.get_op_in = self.in_op
202 m
.submodules
.get_op1_out
= self
.o
.a
203 m
.submodules
.get_op2_out
= self
.o
.b
204 out_op1
= FPNumIn(None, self
.width
)
205 out_op2
= FPNumIn(None, self
.width
)
206 with m
.If(self
.trigger
):
208 out_op1
.decode(self
.in_op1
),
209 out_op2
.decode(self
.in_op2
),
210 self
.o
.a
.eq(out_op1
),
211 self
.o
.b
.eq(out_op2
),
216 class FPGet2Op(FPState
):
220 def __init__(self
, in_state
, out_state
, in_op1
, in_op2
, width
, id_wid
):
221 FPState
.__init
__(self
, in_state
)
222 self
.out_state
= out_state
223 self
.mod
= FPGet2OpMod(width
, id_wid
)
226 self
.o
= self
.mod
.ospec()
227 self
.in_stb
= Signal(reset_less
=True)
228 self
.out_ack
= Signal(reset_less
=True)
229 self
.out_decode
= Signal(reset_less
=True)
231 def setup(self
, m
, in_op1
, in_op2
, in_stb
, in_ack
):
232 """ links module to inputs and outputs
234 m
.submodules
.get_ops
= self
.mod
235 m
.d
.comb
+= self
.mod
.in_op1
.eq(in_op1
)
236 m
.d
.comb
+= self
.mod
.in_op2
.eq(in_op2
)
237 m
.d
.comb
+= self
.mod
.stb
.eq(in_stb
)
238 m
.d
.comb
+= self
.out_ack
.eq(self
.mod
.ack
)
239 m
.d
.comb
+= self
.out_decode
.eq(self
.mod
.trigger
)
240 m
.d
.comb
+= in_ack
.eq(self
.mod
.ack
)
243 with m
.If(self
.out_decode
):
244 m
.next
= self
.out_state
247 self
.o
.eq(self
.mod
.o
),
250 m
.d
.sync
+= self
.mod
.ack
.eq(1)
255 def __init__(self
, width
, id_wid
, m_extra
=True):
256 self
.a
= FPNumBase(width
, m_extra
)
257 self
.b
= FPNumBase(width
, m_extra
)
258 self
.mid
= Signal(id_wid
, reset_less
=True)
261 return [self
.a
.eq(i
.a
), self
.b
.eq(i
.b
), self
.mid
.eq(i
.mid
)]
264 class FPAddSpecialCasesMod
:
265 """ special cases: NaNs, infs, zeros, denormalised
266 NOTE: some of these are unique to add. see "Special Operations"
267 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
270 def __init__(self
, width
, id_wid
):
273 self
.i
= self
.ispec()
274 self
.o
= self
.ospec()
275 self
.out_do_z
= Signal(reset_less
=True)
278 return FPNumBase2Ops(self
.width
, self
.id_wid
)
281 return FPPackData(self
.width
, self
.id_wid
)
283 def setup(self
, m
, in_a
, in_b
, out_do_z
):
284 """ links module to inputs and outputs
286 m
.submodules
.specialcases
= self
287 m
.d
.comb
+= self
.i
.a
.eq(in_a
)
288 m
.d
.comb
+= self
.i
.b
.eq(in_b
)
289 m
.d
.comb
+= out_do_z
.eq(self
.out_do_z
)
291 def elaborate(self
, platform
):
294 m
.submodules
.sc_in_a
= self
.i
.a
295 m
.submodules
.sc_in_b
= self
.i
.b
296 m
.submodules
.sc_out_z
= self
.o
.z
299 m
.d
.comb
+= s_nomatch
.eq(self
.i
.a
.s
!= self
.i
.b
.s
)
302 m
.d
.comb
+= m_match
.eq(self
.i
.a
.m
== self
.i
.b
.m
)
304 # if a is NaN or b is NaN return NaN
305 with m
.If(self
.i
.a
.is_nan | self
.i
.b
.is_nan
):
306 m
.d
.comb
+= self
.out_do_z
.eq(1)
307 m
.d
.comb
+= self
.o
.z
.nan(0)
309 # XXX WEIRDNESS for FP16 non-canonical NaN handling
312 ## if a is zero and b is NaN return -b
313 #with m.If(a.is_zero & (a.s==0) & b.is_nan):
314 # m.d.comb += self.out_do_z.eq(1)
315 # m.d.comb += z.create(b.s, b.e, Cat(b.m[3:-2], ~b.m[0]))
317 ## if b is zero and a is NaN return -a
318 #with m.Elif(b.is_zero & (b.s==0) & a.is_nan):
319 # m.d.comb += self.out_do_z.eq(1)
320 # m.d.comb += z.create(a.s, a.e, Cat(a.m[3:-2], ~a.m[0]))
322 ## if a is -zero and b is NaN return -b
323 #with m.Elif(a.is_zero & (a.s==1) & b.is_nan):
324 # m.d.comb += self.out_do_z.eq(1)
325 # m.d.comb += z.create(a.s & b.s, b.e, Cat(b.m[3:-2], 1))
327 ## if b is -zero and a is NaN return -a
328 #with m.Elif(b.is_zero & (b.s==1) & a.is_nan):
329 # m.d.comb += self.out_do_z.eq(1)
330 # m.d.comb += z.create(a.s & b.s, a.e, Cat(a.m[3:-2], 1))
332 # if a is inf return inf (or NaN)
333 with m
.Elif(self
.i
.a
.is_inf
):
334 m
.d
.comb
+= self
.out_do_z
.eq(1)
335 m
.d
.comb
+= self
.o
.z
.inf(self
.i
.a
.s
)
336 # if a is inf and signs don't match return NaN
337 with m
.If(self
.i
.b
.exp_128
& s_nomatch
):
338 m
.d
.comb
+= self
.o
.z
.nan(0)
340 # if b is inf return inf
341 with m
.Elif(self
.i
.b
.is_inf
):
342 m
.d
.comb
+= self
.out_do_z
.eq(1)
343 m
.d
.comb
+= self
.o
.z
.inf(self
.i
.b
.s
)
345 # if a is zero and b zero return signed-a/b
346 with m
.Elif(self
.i
.a
.is_zero
& self
.i
.b
.is_zero
):
347 m
.d
.comb
+= self
.out_do_z
.eq(1)
348 m
.d
.comb
+= self
.o
.z
.create(self
.i
.a
.s
& self
.i
.b
.s
,
352 # if a is zero return b
353 with m
.Elif(self
.i
.a
.is_zero
):
354 m
.d
.comb
+= self
.out_do_z
.eq(1)
355 m
.d
.comb
+= self
.o
.z
.create(self
.i
.b
.s
, self
.i
.b
.e
,
358 # if b is zero return a
359 with m
.Elif(self
.i
.b
.is_zero
):
360 m
.d
.comb
+= self
.out_do_z
.eq(1)
361 m
.d
.comb
+= self
.o
.z
.create(self
.i
.a
.s
, self
.i
.a
.e
,
364 # if a equal to -b return zero (+ve zero)
365 with m
.Elif(s_nomatch
& m_match
& (self
.i
.a
.e
== self
.i
.b
.e
)):
366 m
.d
.comb
+= self
.out_do_z
.eq(1)
367 m
.d
.comb
+= self
.o
.z
.zero(0)
369 # Denormalised Number checks
371 m
.d
.comb
+= self
.out_do_z
.eq(0)
377 def __init__(self
, id_wid
):
380 self
.in_mid
= Signal(id_wid
, reset_less
=True)
381 self
.out_mid
= Signal(id_wid
, reset_less
=True)
387 if self
.id_wid
is not None:
388 m
.d
.sync
+= self
.out_mid
.eq(self
.in_mid
)
391 class FPAddSpecialCases(FPState
, FPID
):
392 """ special cases: NaNs, infs, zeros, denormalised
393 NOTE: some of these are unique to add. see "Special Operations"
394 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
397 def __init__(self
, width
, id_wid
):
398 FPState
.__init
__(self
, "special_cases")
399 FPID
.__init
__(self
, id_wid
)
400 self
.mod
= FPAddSpecialCasesMod(width
)
401 self
.out_z
= self
.mod
.ospec()
402 self
.out_do_z
= Signal(reset_less
=True)
404 def setup(self
, m
, in_a
, in_b
, in_mid
):
405 """ links module to inputs and outputs
407 self
.mod
.setup(m
, in_a
, in_b
, self
.out_do_z
)
408 if self
.in_mid
is not None:
409 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
413 with m
.If(self
.out_do_z
):
414 m
.d
.sync
+= self
.out_z
.v
.eq(self
.mod
.out_z
.v
) # only take the output
417 m
.next
= "denormalise"
420 class FPAddSpecialCasesDeNorm(FPState
, FPID
):
421 """ special cases: NaNs, infs, zeros, denormalised
422 NOTE: some of these are unique to add. see "Special Operations"
423 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
426 def __init__(self
, width
, id_wid
):
427 FPState
.__init
__(self
, "special_cases")
428 FPID
.__init
__(self
, id_wid
)
429 self
.smod
= FPAddSpecialCasesMod(width
, id_wid
)
430 self
.out_z
= self
.smod
.ospec()
431 self
.out_do_z
= Signal(reset_less
=True)
433 self
.dmod
= FPAddDeNormMod(width
, id_wid
)
434 self
.o
= self
.dmod
.ospec()
436 def setup(self
, m
, in_a
, in_b
, in_mid
):
437 """ links module to inputs and outputs
439 self
.smod
.setup(m
, in_a
, in_b
, self
.out_do_z
)
440 self
.dmod
.setup(m
, in_a
, in_b
)
441 if self
.in_mid
is not None:
442 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
446 with m
.If(self
.out_do_z
):
447 m
.d
.sync
+= self
.out_z
.z
.v
.eq(self
.smod
.o
.z
.v
) # only take output
451 m
.d
.sync
+= self
.o
.a
.eq(self
.dmod
.o
.a
)
452 m
.d
.sync
+= self
.o
.b
.eq(self
.dmod
.o
.b
)
455 class FPAddDeNormMod(FPState
):
457 def __init__(self
, width
, id_wid
):
460 self
.i
= self
.ispec()
461 self
.o
= self
.ospec()
464 return FPNumBase2Ops(self
.width
, self
.id_wid
)
467 return FPNumBase2Ops(self
.width
, self
.id_wid
)
469 def setup(self
, m
, in_a
, in_b
):
470 """ links module to inputs and outputs
472 m
.submodules
.denormalise
= self
473 m
.d
.comb
+= self
.i
.a
.eq(in_a
)
474 m
.d
.comb
+= self
.i
.b
.eq(in_b
)
476 def elaborate(self
, platform
):
478 m
.submodules
.denorm_in_a
= self
.i
.a
479 m
.submodules
.denorm_in_b
= self
.i
.b
480 m
.submodules
.denorm_out_a
= self
.o
.a
481 m
.submodules
.denorm_out_b
= self
.o
.b
482 # hmmm, don't like repeating identical code
483 m
.d
.comb
+= self
.o
.a
.eq(self
.i
.a
)
484 with m
.If(self
.i
.a
.exp_n127
):
485 m
.d
.comb
+= self
.o
.a
.e
.eq(self
.i
.a
.N126
) # limit a exponent
487 m
.d
.comb
+= self
.o
.a
.m
[-1].eq(1) # set top mantissa bit
489 m
.d
.comb
+= self
.o
.b
.eq(self
.i
.b
)
490 with m
.If(self
.i
.b
.exp_n127
):
491 m
.d
.comb
+= self
.o
.b
.e
.eq(self
.i
.b
.N126
) # limit a exponent
493 m
.d
.comb
+= self
.o
.b
.m
[-1].eq(1) # set top mantissa bit
498 class FPAddDeNorm(FPState
, FPID
):
500 def __init__(self
, width
, id_wid
):
501 FPState
.__init
__(self
, "denormalise")
502 FPID
.__init
__(self
, id_wid
)
503 self
.mod
= FPAddDeNormMod(width
)
504 self
.out_a
= FPNumBase(width
)
505 self
.out_b
= FPNumBase(width
)
507 def setup(self
, m
, in_a
, in_b
, in_mid
):
508 """ links module to inputs and outputs
510 self
.mod
.setup(m
, in_a
, in_b
)
511 if self
.in_mid
is not None:
512 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
516 # Denormalised Number checks
518 m
.d
.sync
+= self
.out_a
.eq(self
.mod
.out_a
)
519 m
.d
.sync
+= self
.out_b
.eq(self
.mod
.out_b
)
522 class FPAddAlignMultiMod(FPState
):
524 def __init__(self
, width
):
525 self
.in_a
= FPNumBase(width
)
526 self
.in_b
= FPNumBase(width
)
527 self
.out_a
= FPNumIn(None, width
)
528 self
.out_b
= FPNumIn(None, width
)
529 self
.exp_eq
= Signal(reset_less
=True)
531 def elaborate(self
, platform
):
532 # This one however (single-cycle) will do the shift
537 m
.submodules
.align_in_a
= self
.in_a
538 m
.submodules
.align_in_b
= self
.in_b
539 m
.submodules
.align_out_a
= self
.out_a
540 m
.submodules
.align_out_b
= self
.out_b
542 # NOTE: this does *not* do single-cycle multi-shifting,
543 # it *STAYS* in the align state until exponents match
545 # exponent of a greater than b: shift b down
546 m
.d
.comb
+= self
.exp_eq
.eq(0)
547 m
.d
.comb
+= self
.out_a
.eq(self
.in_a
)
548 m
.d
.comb
+= self
.out_b
.eq(self
.in_b
)
549 agtb
= Signal(reset_less
=True)
550 altb
= Signal(reset_less
=True)
551 m
.d
.comb
+= agtb
.eq(self
.in_a
.e
> self
.in_b
.e
)
552 m
.d
.comb
+= altb
.eq(self
.in_a
.e
< self
.in_b
.e
)
554 m
.d
.comb
+= self
.out_b
.shift_down(self
.in_b
)
555 # exponent of b greater than a: shift a down
557 m
.d
.comb
+= self
.out_a
.shift_down(self
.in_a
)
558 # exponents equal: move to next stage.
560 m
.d
.comb
+= self
.exp_eq
.eq(1)
564 class FPAddAlignMulti(FPState
, FPID
):
566 def __init__(self
, width
, id_wid
):
567 FPID
.__init
__(self
, id_wid
)
568 FPState
.__init
__(self
, "align")
569 self
.mod
= FPAddAlignMultiMod(width
)
570 self
.out_a
= FPNumIn(None, width
)
571 self
.out_b
= FPNumIn(None, width
)
572 self
.exp_eq
= Signal(reset_less
=True)
574 def setup(self
, m
, in_a
, in_b
, in_mid
):
575 """ links module to inputs and outputs
577 m
.submodules
.align
= self
.mod
578 m
.d
.comb
+= self
.mod
.in_a
.eq(in_a
)
579 m
.d
.comb
+= self
.mod
.in_b
.eq(in_b
)
580 #m.d.comb += self.out_a.eq(self.mod.out_a)
581 #m.d.comb += self.out_b.eq(self.mod.out_b)
582 m
.d
.comb
+= self
.exp_eq
.eq(self
.mod
.exp_eq
)
583 if self
.in_mid
is not None:
584 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
588 m
.d
.sync
+= self
.out_a
.eq(self
.mod
.out_a
)
589 m
.d
.sync
+= self
.out_b
.eq(self
.mod
.out_b
)
590 with m
.If(self
.exp_eq
):
596 def __init__(self
, width
, id_wid
):
597 self
.a
= FPNumIn(None, width
)
598 self
.b
= FPNumIn(None, width
)
599 self
.mid
= Signal(id_wid
, reset_less
=True)
602 return [self
.a
.eq(i
.a
), self
.b
.eq(i
.b
), self
.mid
.eq(i
.mid
)]
605 class FPAddAlignSingleMod
:
607 def __init__(self
, width
, id_wid
):
610 self
.i
= self
.ispec()
611 self
.o
= self
.ospec()
614 return FPNumBase2Ops(self
.width
, self
.id_wid
)
617 return FPNumIn2Ops(self
.width
, self
.id_wid
)
619 def setup(self
, m
, in_a
, in_b
):
620 """ links module to inputs and outputs
622 m
.submodules
.align
= self
623 m
.d
.comb
+= self
.i
.a
.eq(in_a
)
624 m
.d
.comb
+= self
.i
.b
.eq(in_b
)
626 def elaborate(self
, platform
):
627 """ Aligns A against B or B against A, depending on which has the
628 greater exponent. This is done in a *single* cycle using
629 variable-width bit-shift
631 the shifter used here is quite expensive in terms of gates.
632 Mux A or B in (and out) into temporaries, as only one of them
633 needs to be aligned against the other
637 m
.submodules
.align_in_a
= self
.i
.a
638 m
.submodules
.align_in_b
= self
.i
.b
639 m
.submodules
.align_out_a
= self
.o
.a
640 m
.submodules
.align_out_b
= self
.o
.b
642 # temporary (muxed) input and output to be shifted
643 t_inp
= FPNumBase(self
.width
)
644 t_out
= FPNumIn(None, self
.width
)
645 espec
= (len(self
.i
.a
.e
), True)
646 msr
= MultiShiftRMerge(self
.i
.a
.m_width
, espec
)
647 m
.submodules
.align_t_in
= t_inp
648 m
.submodules
.align_t_out
= t_out
649 m
.submodules
.multishift_r
= msr
651 ediff
= Signal(espec
, reset_less
=True)
652 ediffr
= Signal(espec
, reset_less
=True)
653 tdiff
= Signal(espec
, reset_less
=True)
654 elz
= Signal(reset_less
=True)
655 egz
= Signal(reset_less
=True)
657 # connect multi-shifter to t_inp/out mantissa (and tdiff)
658 m
.d
.comb
+= msr
.inp
.eq(t_inp
.m
)
659 m
.d
.comb
+= msr
.diff
.eq(tdiff
)
660 m
.d
.comb
+= t_out
.m
.eq(msr
.m
)
661 m
.d
.comb
+= t_out
.e
.eq(t_inp
.e
+ tdiff
)
662 m
.d
.comb
+= t_out
.s
.eq(t_inp
.s
)
664 m
.d
.comb
+= ediff
.eq(self
.i
.a
.e
- self
.i
.b
.e
)
665 m
.d
.comb
+= ediffr
.eq(self
.i
.b
.e
- self
.i
.a
.e
)
666 m
.d
.comb
+= elz
.eq(self
.i
.a
.e
< self
.i
.b
.e
)
667 m
.d
.comb
+= egz
.eq(self
.i
.a
.e
> self
.i
.b
.e
)
669 # default: A-exp == B-exp, A and B untouched (fall through)
670 m
.d
.comb
+= self
.o
.a
.eq(self
.i
.a
)
671 m
.d
.comb
+= self
.o
.b
.eq(self
.i
.b
)
672 # only one shifter (muxed)
673 #m.d.comb += t_out.shift_down_multi(tdiff, t_inp)
674 # exponent of a greater than b: shift b down
676 m
.d
.comb
+= [t_inp
.eq(self
.i
.b
),
679 self
.o
.b
.s
.eq(self
.i
.b
.s
), # whoops forgot sign
681 # exponent of b greater than a: shift a down
683 m
.d
.comb
+= [t_inp
.eq(self
.i
.a
),
686 self
.o
.a
.s
.eq(self
.i
.a
.s
), # whoops forgot sign
691 class FPAddAlignSingle(FPState
, FPID
):
693 def __init__(self
, width
, id_wid
):
694 FPState
.__init
__(self
, "align")
695 FPID
.__init
__(self
, id_wid
)
696 self
.mod
= FPAddAlignSingleMod(width
, id_wid
)
697 self
.out_a
= FPNumIn(None, width
)
698 self
.out_b
= FPNumIn(None, width
)
700 def setup(self
, m
, in_a
, in_b
, in_mid
):
701 """ links module to inputs and outputs
703 self
.mod
.setup(m
, in_a
, in_b
)
704 if self
.in_mid
is not None:
705 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
709 # NOTE: could be done as comb
710 m
.d
.sync
+= self
.out_a
.eq(self
.mod
.out_a
)
711 m
.d
.sync
+= self
.out_b
.eq(self
.mod
.out_b
)
715 class FPAddAlignSingleAdd(FPState
, FPID
):
717 def __init__(self
, width
, id_wid
):
718 FPState
.__init
__(self
, "align")
719 FPID
.__init
__(self
, id_wid
)
720 self
.mod
= FPAddAlignSingleMod(width
, id_wid
)
721 self
.o
= self
.mod
.ospec()
723 self
.a0mod
= FPAddStage0Mod(width
, id_wid
)
724 self
.a0o
= self
.a0mod
.ospec()
726 self
.a1mod
= FPAddStage1Mod(width
, id_wid
)
727 self
.a1o
= self
.a1mod
.ospec()
729 def setup(self
, m
, in_a
, in_b
, in_mid
):
730 """ links module to inputs and outputs
732 self
.mod
.setup(m
, in_a
, in_b
)
733 m
.d
.comb
+= self
.o
.eq(self
.mod
.o
)
735 self
.a0mod
.setup(m
, self
.o
.a
, self
.o
.b
)
736 m
.d
.comb
+= self
.a0o
.eq(self
.a0mod
.o
)
738 self
.a1mod
.setup(m
, self
.a0o
.tot
, self
.a0o
.z
)
740 if self
.in_mid
is not None:
741 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
745 m
.d
.sync
+= self
.a1o
.eq(self
.a1mod
.o
)
746 m
.next
= "normalise_1"
749 class FPAddStage0Data
:
751 def __init__(self
, width
, id_wid
):
752 self
.z
= FPNumBase(width
, False)
753 self
.tot
= Signal(self
.z
.m_width
+ 4, reset_less
=True)
754 self
.mid
= Signal(id_wid
, reset_less
=True)
757 return [self
.z
.eq(i
.z
), self
.tot
.eq(i
.tot
), self
.mid
.eq(i
.mid
)]
760 class FPAddStage0Mod
:
762 def __init__(self
, width
, id_wid
):
765 self
.i
= self
.ispec()
766 self
.o
= self
.ospec()
769 return FPNumBase2Ops(self
.width
, self
.id_wid
)
772 return FPAddStage0Data(self
.width
, self
.id_wid
)
774 def setup(self
, m
, in_a
, in_b
):
775 """ links module to inputs and outputs
777 m
.submodules
.add0
= self
778 m
.d
.comb
+= self
.i
.a
.eq(in_a
)
779 m
.d
.comb
+= self
.i
.b
.eq(in_b
)
781 def elaborate(self
, platform
):
783 m
.submodules
.add0_in_a
= self
.i
.a
784 m
.submodules
.add0_in_b
= self
.i
.b
785 m
.submodules
.add0_out_z
= self
.o
.z
787 m
.d
.comb
+= self
.o
.z
.e
.eq(self
.i
.a
.e
)
789 # store intermediate tests (and zero-extended mantissas)
790 seq
= Signal(reset_less
=True)
791 mge
= Signal(reset_less
=True)
792 am0
= Signal(len(self
.i
.a
.m
)+1, reset_less
=True)
793 bm0
= Signal(len(self
.i
.b
.m
)+1, reset_less
=True)
794 m
.d
.comb
+= [seq
.eq(self
.i
.a
.s
== self
.i
.b
.s
),
795 mge
.eq(self
.i
.a
.m
>= self
.i
.b
.m
),
796 am0
.eq(Cat(self
.i
.a
.m
, 0)),
797 bm0
.eq(Cat(self
.i
.b
.m
, 0))
799 # same-sign (both negative or both positive) add mantissas
802 self
.o
.tot
.eq(am0
+ bm0
),
803 self
.o
.z
.s
.eq(self
.i
.a
.s
)
805 # a mantissa greater than b, use a
808 self
.o
.tot
.eq(am0
- bm0
),
809 self
.o
.z
.s
.eq(self
.i
.a
.s
)
811 # b mantissa greater than a, use b
814 self
.o
.tot
.eq(bm0
- am0
),
815 self
.o
.z
.s
.eq(self
.i
.b
.s
)
820 class FPAddStage0(FPState
, FPID
):
821 """ First stage of add. covers same-sign (add) and subtract
822 special-casing when mantissas are greater or equal, to
823 give greatest accuracy.
826 def __init__(self
, width
, id_wid
):
827 FPState
.__init
__(self
, "add_0")
828 FPID
.__init
__(self
, id_wid
)
829 self
.mod
= FPAddStage0Mod(width
)
830 self
.o
= self
.mod
.ospec()
832 def setup(self
, m
, in_a
, in_b
, in_mid
):
833 """ links module to inputs and outputs
835 self
.mod
.setup(m
, in_a
, in_b
)
836 if self
.in_mid
is not None:
837 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
841 # NOTE: these could be done as combinatorial (merge add0+add1)
842 m
.d
.sync
+= self
.o
.eq(self
.mod
.o
)
846 class FPAddStage1Data
:
848 def __init__(self
, width
, id_wid
):
849 self
.z
= FPNumBase(width
, False)
851 self
.mid
= Signal(id_wid
, reset_less
=True)
854 return [self
.z
.eq(i
.z
), self
.of
.eq(i
.of
), self
.mid
.eq(i
.mid
)]
858 class FPAddStage1Mod(FPState
):
859 """ Second stage of add: preparation for normalisation.
860 detects when tot sum is too big (tot[27] is kinda a carry bit)
863 def __init__(self
, width
, id_wid
):
866 self
.i
= self
.ispec()
867 self
.o
= self
.ospec()
870 return FPAddStage0Data(self
.width
, self
.id_wid
)
873 return FPAddStage1Data(self
.width
, self
.id_wid
)
875 def setup(self
, m
, in_tot
, in_z
):
876 """ links module to inputs and outputs
878 m
.submodules
.add1
= self
879 m
.submodules
.add1_out_overflow
= self
.o
.of
881 m
.d
.comb
+= self
.i
.z
.eq(in_z
)
882 m
.d
.comb
+= self
.i
.tot
.eq(in_tot
)
884 def elaborate(self
, platform
):
886 #m.submodules.norm1_in_overflow = self.in_of
887 #m.submodules.norm1_out_overflow = self.out_of
888 #m.submodules.norm1_in_z = self.in_z
889 #m.submodules.norm1_out_z = self.out_z
890 m
.d
.comb
+= self
.o
.z
.eq(self
.i
.z
)
891 # tot[-1] (MSB) gets set when the sum overflows. shift result down
892 with m
.If(self
.i
.tot
[-1]):
894 self
.o
.z
.m
.eq(self
.i
.tot
[4:]),
895 self
.o
.of
.m0
.eq(self
.i
.tot
[4]),
896 self
.o
.of
.guard
.eq(self
.i
.tot
[3]),
897 self
.o
.of
.round_bit
.eq(self
.i
.tot
[2]),
898 self
.o
.of
.sticky
.eq(self
.i
.tot
[1] | self
.i
.tot
[0]),
899 self
.o
.z
.e
.eq(self
.i
.z
.e
+ 1)
901 # tot[-1] (MSB) zero case
904 self
.o
.z
.m
.eq(self
.i
.tot
[3:]),
905 self
.o
.of
.m0
.eq(self
.i
.tot
[3]),
906 self
.o
.of
.guard
.eq(self
.i
.tot
[2]),
907 self
.o
.of
.round_bit
.eq(self
.i
.tot
[1]),
908 self
.o
.of
.sticky
.eq(self
.i
.tot
[0])
913 class FPAddStage1(FPState
, FPID
):
915 def __init__(self
, width
, id_wid
):
916 FPState
.__init
__(self
, "add_1")
917 FPID
.__init
__(self
, id_wid
)
918 self
.mod
= FPAddStage1Mod(width
)
919 self
.out_z
= FPNumBase(width
, False)
920 self
.out_of
= Overflow()
921 self
.norm_stb
= Signal()
923 def setup(self
, m
, in_tot
, in_z
, in_mid
):
924 """ links module to inputs and outputs
926 self
.mod
.setup(m
, in_tot
, in_z
)
928 m
.d
.sync
+= self
.norm_stb
.eq(0) # sets to zero when not in add1 state
930 if self
.in_mid
is not None:
931 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
935 m
.d
.sync
+= self
.out_of
.eq(self
.mod
.out_of
)
936 m
.d
.sync
+= self
.out_z
.eq(self
.mod
.out_z
)
937 m
.d
.sync
+= self
.norm_stb
.eq(1)
938 m
.next
= "normalise_1"
941 class FPNormaliseModSingle
:
943 def __init__(self
, width
):
945 self
.in_z
= self
.ispec()
946 self
.out_z
= self
.ospec()
949 return FPNumBase(self
.width
, False)
952 return FPNumBase(self
.width
, False)
954 def setup(self
, m
, in_z
, out_z
):
955 """ links module to inputs and outputs
957 m
.submodules
.normalise
= self
958 m
.d
.comb
+= self
.in_z
.eq(in_z
)
959 m
.d
.comb
+= out_z
.eq(self
.out_z
)
961 def elaborate(self
, platform
):
964 mwid
= self
.out_z
.m_width
+2
965 pe
= PriorityEncoder(mwid
)
966 m
.submodules
.norm_pe
= pe
968 m
.submodules
.norm1_out_z
= self
.out_z
969 m
.submodules
.norm1_in_z
= self
.in_z
971 in_z
= FPNumBase(self
.width
, False)
973 m
.submodules
.norm1_insel_z
= in_z
974 m
.submodules
.norm1_insel_overflow
= in_of
976 espec
= (len(in_z
.e
), True)
977 ediff_n126
= Signal(espec
, reset_less
=True)
978 msr
= MultiShiftRMerge(mwid
, espec
)
979 m
.submodules
.multishift_r
= msr
981 m
.d
.comb
+= in_z
.eq(self
.in_z
)
982 m
.d
.comb
+= in_of
.eq(self
.in_of
)
983 # initialise out from in (overridden below)
984 m
.d
.comb
+= self
.out_z
.eq(in_z
)
985 m
.d
.comb
+= self
.out_of
.eq(in_of
)
986 # normalisation decrease condition
987 decrease
= Signal(reset_less
=True)
988 m
.d
.comb
+= decrease
.eq(in_z
.m_msbzero
)
991 # *sigh* not entirely obvious: count leading zeros (clz)
992 # with a PriorityEncoder: to find from the MSB
993 # we reverse the order of the bits.
994 temp_m
= Signal(mwid
, reset_less
=True)
995 temp_s
= Signal(mwid
+1, reset_less
=True)
996 clz
= Signal((len(in_z
.e
), True), reset_less
=True)
998 # cat round and guard bits back into the mantissa
999 temp_m
.eq(Cat(in_of
.round_bit
, in_of
.guard
, in_z
.m
)),
1000 pe
.i
.eq(temp_m
[::-1]), # inverted
1001 clz
.eq(pe
.o
), # count zeros from MSB down
1002 temp_s
.eq(temp_m
<< clz
), # shift mantissa UP
1003 self
.out_z
.e
.eq(in_z
.e
- clz
), # DECREASE exponent
1004 self
.out_z
.m
.eq(temp_s
[2:]), # exclude bits 0&1
1011 def __init__(self
, width
, id_wid
):
1012 self
.roundz
= Signal(reset_less
=True)
1013 self
.z
= FPNumBase(width
, False)
1014 self
.mid
= Signal(id_wid
, reset_less
=True)
1017 return [self
.z
.eq(i
.z
), self
.roundz
.eq(i
.roundz
), self
.mid
.eq(i
.mid
)]
1020 class FPNorm1ModSingle
:
1022 def __init__(self
, width
, id_wid
):
1024 self
.id_wid
= id_wid
1025 self
.i
= self
.ispec()
1026 self
.o
= self
.ospec()
1029 return FPAddStage1Data(self
.width
, self
.id_wid
)
1032 return FPNorm1Data(self
.width
, self
.id_wid
)
1034 def setup(self
, m
, in_z
, in_of
, out_z
):
1035 """ links module to inputs and outputs
1037 m
.submodules
.normalise_1
= self
1039 m
.d
.comb
+= self
.i
.z
.eq(in_z
)
1040 m
.d
.comb
+= self
.i
.of
.eq(in_of
)
1042 m
.d
.comb
+= out_z
.eq(self
.o
.z
)
1044 def elaborate(self
, platform
):
1047 mwid
= self
.o
.z
.m_width
+2
1048 pe
= PriorityEncoder(mwid
)
1049 m
.submodules
.norm_pe
= pe
1052 m
.d
.comb
+= self
.o
.roundz
.eq(of
.roundz
)
1054 m
.submodules
.norm1_out_z
= self
.o
.z
1055 m
.submodules
.norm1_out_overflow
= of
1056 m
.submodules
.norm1_in_z
= self
.i
.z
1057 m
.submodules
.norm1_in_overflow
= self
.i
.of
1060 m
.submodules
.norm1_insel_z
= i
.z
1061 m
.submodules
.norm1_insel_overflow
= i
.of
1063 espec
= (len(i
.z
.e
), True)
1064 ediff_n126
= Signal(espec
, reset_less
=True)
1065 msr
= MultiShiftRMerge(mwid
, espec
)
1066 m
.submodules
.multishift_r
= msr
1068 m
.d
.comb
+= i
.eq(self
.i
)
1069 # initialise out from in (overridden below)
1070 m
.d
.comb
+= self
.o
.z
.eq(i
.z
)
1071 m
.d
.comb
+= of
.eq(i
.of
)
1072 # normalisation increase/decrease conditions
1073 decrease
= Signal(reset_less
=True)
1074 increase
= Signal(reset_less
=True)
1075 m
.d
.comb
+= decrease
.eq(i
.z
.m_msbzero
& i
.z
.exp_gt_n126
)
1076 m
.d
.comb
+= increase
.eq(i
.z
.exp_lt_n126
)
1078 with m
.If(decrease
):
1079 # *sigh* not entirely obvious: count leading zeros (clz)
1080 # with a PriorityEncoder: to find from the MSB
1081 # we reverse the order of the bits.
1082 temp_m
= Signal(mwid
, reset_less
=True)
1083 temp_s
= Signal(mwid
+1, reset_less
=True)
1084 clz
= Signal((len(i
.z
.e
), True), reset_less
=True)
1085 # make sure that the amount to decrease by does NOT
1086 # go below the minimum non-INF/NaN exponent
1087 limclz
= Mux(i
.z
.exp_sub_n126
> pe
.o
, pe
.o
,
1090 # cat round and guard bits back into the mantissa
1091 temp_m
.eq(Cat(i
.of
.round_bit
, i
.of
.guard
, i
.z
.m
)),
1092 pe
.i
.eq(temp_m
[::-1]), # inverted
1093 clz
.eq(limclz
), # count zeros from MSB down
1094 temp_s
.eq(temp_m
<< clz
), # shift mantissa UP
1095 self
.o
.z
.e
.eq(i
.z
.e
- clz
), # DECREASE exponent
1096 self
.o
.z
.m
.eq(temp_s
[2:]), # exclude bits 0&1
1097 of
.m0
.eq(temp_s
[2]), # copy of mantissa[0]
1098 # overflow in bits 0..1: got shifted too (leave sticky)
1099 of
.guard
.eq(temp_s
[1]), # guard
1100 of
.round_bit
.eq(temp_s
[0]), # round
1103 with m
.Elif(increase
):
1104 temp_m
= Signal(mwid
+1, reset_less
=True)
1106 temp_m
.eq(Cat(i
.of
.sticky
, i
.of
.round_bit
, i
.of
.guard
,
1108 ediff_n126
.eq(i
.z
.N126
- i
.z
.e
),
1109 # connect multi-shifter to inp/out mantissa (and ediff)
1111 msr
.diff
.eq(ediff_n126
),
1112 self
.o
.z
.m
.eq(msr
.m
[3:]),
1113 of
.m0
.eq(temp_s
[3]), # copy of mantissa[0]
1114 # overflow in bits 0..1: got shifted too (leave sticky)
1115 of
.guard
.eq(temp_s
[2]), # guard
1116 of
.round_bit
.eq(temp_s
[1]), # round
1117 of
.sticky
.eq(temp_s
[0]), # sticky
1118 self
.o
.z
.e
.eq(i
.z
.e
+ ediff_n126
),
1124 class FPNorm1ModMulti
:
1126 def __init__(self
, width
, single_cycle
=True):
1128 self
.in_select
= Signal(reset_less
=True)
1129 self
.in_z
= FPNumBase(width
, False)
1130 self
.in_of
= Overflow()
1131 self
.temp_z
= FPNumBase(width
, False)
1132 self
.temp_of
= Overflow()
1133 self
.out_z
= FPNumBase(width
, False)
1134 self
.out_of
= Overflow()
1136 def elaborate(self
, platform
):
1139 m
.submodules
.norm1_out_z
= self
.out_z
1140 m
.submodules
.norm1_out_overflow
= self
.out_of
1141 m
.submodules
.norm1_temp_z
= self
.temp_z
1142 m
.submodules
.norm1_temp_of
= self
.temp_of
1143 m
.submodules
.norm1_in_z
= self
.in_z
1144 m
.submodules
.norm1_in_overflow
= self
.in_of
1146 in_z
= FPNumBase(self
.width
, False)
1148 m
.submodules
.norm1_insel_z
= in_z
1149 m
.submodules
.norm1_insel_overflow
= in_of
1151 # select which of temp or in z/of to use
1152 with m
.If(self
.in_select
):
1153 m
.d
.comb
+= in_z
.eq(self
.in_z
)
1154 m
.d
.comb
+= in_of
.eq(self
.in_of
)
1156 m
.d
.comb
+= in_z
.eq(self
.temp_z
)
1157 m
.d
.comb
+= in_of
.eq(self
.temp_of
)
1158 # initialise out from in (overridden below)
1159 m
.d
.comb
+= self
.out_z
.eq(in_z
)
1160 m
.d
.comb
+= self
.out_of
.eq(in_of
)
1161 # normalisation increase/decrease conditions
1162 decrease
= Signal(reset_less
=True)
1163 increase
= Signal(reset_less
=True)
1164 m
.d
.comb
+= decrease
.eq(in_z
.m_msbzero
& in_z
.exp_gt_n126
)
1165 m
.d
.comb
+= increase
.eq(in_z
.exp_lt_n126
)
1166 m
.d
.comb
+= self
.out_norm
.eq(decrease | increase
) # loop-end
1168 with m
.If(decrease
):
1170 self
.out_z
.e
.eq(in_z
.e
- 1), # DECREASE exponent
1171 self
.out_z
.m
.eq(in_z
.m
<< 1), # shift mantissa UP
1172 self
.out_z
.m
[0].eq(in_of
.guard
), # steal guard (was tot[2])
1173 self
.out_of
.guard
.eq(in_of
.round_bit
), # round (was tot[1])
1174 self
.out_of
.round_bit
.eq(0), # reset round bit
1175 self
.out_of
.m0
.eq(in_of
.guard
),
1178 with m
.Elif(increase
):
1180 self
.out_z
.e
.eq(in_z
.e
+ 1), # INCREASE exponent
1181 self
.out_z
.m
.eq(in_z
.m
>> 1), # shift mantissa DOWN
1182 self
.out_of
.guard
.eq(in_z
.m
[0]),
1183 self
.out_of
.m0
.eq(in_z
.m
[1]),
1184 self
.out_of
.round_bit
.eq(in_of
.guard
),
1185 self
.out_of
.sticky
.eq(in_of
.sticky | in_of
.round_bit
)
1191 class FPNorm1Single(FPState
, FPID
):
1193 def __init__(self
, width
, id_wid
, single_cycle
=True):
1194 FPID
.__init
__(self
, id_wid
)
1195 FPState
.__init
__(self
, "normalise_1")
1196 self
.mod
= FPNorm1ModSingle(width
)
1197 self
.out_z
= FPNumBase(width
, False)
1198 self
.out_roundz
= Signal(reset_less
=True)
1200 def setup(self
, m
, in_z
, in_of
, in_mid
):
1201 """ links module to inputs and outputs
1203 self
.mod
.setup(m
, in_z
, in_of
, self
.out_z
)
1205 if self
.in_mid
is not None:
1206 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
1208 def action(self
, m
):
1210 m
.d
.sync
+= self
.out_roundz
.eq(self
.mod
.out_of
.roundz
)
1214 class FPNorm1Multi(FPState
, FPID
):
1216 def __init__(self
, width
, id_wid
):
1217 FPID
.__init
__(self
, id_wid
)
1218 FPState
.__init
__(self
, "normalise_1")
1219 self
.mod
= FPNorm1ModMulti(width
)
1220 self
.stb
= Signal(reset_less
=True)
1221 self
.ack
= Signal(reset
=0, reset_less
=True)
1222 self
.out_norm
= Signal(reset_less
=True)
1223 self
.in_accept
= Signal(reset_less
=True)
1224 self
.temp_z
= FPNumBase(width
)
1225 self
.temp_of
= Overflow()
1226 self
.out_z
= FPNumBase(width
)
1227 self
.out_roundz
= Signal(reset_less
=True)
1229 def setup(self
, m
, in_z
, in_of
, norm_stb
, in_mid
):
1230 """ links module to inputs and outputs
1232 self
.mod
.setup(m
, in_z
, in_of
, norm_stb
,
1233 self
.in_accept
, self
.temp_z
, self
.temp_of
,
1234 self
.out_z
, self
.out_norm
)
1236 m
.d
.comb
+= self
.stb
.eq(norm_stb
)
1237 m
.d
.sync
+= self
.ack
.eq(0) # sets to zero when not in normalise_1 state
1239 if self
.in_mid
is not None:
1240 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
1242 def action(self
, m
):
1244 m
.d
.comb
+= self
.in_accept
.eq((~self
.ack
) & (self
.stb
))
1245 m
.d
.sync
+= self
.temp_of
.eq(self
.mod
.out_of
)
1246 m
.d
.sync
+= self
.temp_z
.eq(self
.out_z
)
1247 with m
.If(self
.out_norm
):
1248 with m
.If(self
.in_accept
):
1253 m
.d
.sync
+= self
.ack
.eq(0)
1255 # normalisation not required (or done).
1257 m
.d
.sync
+= self
.ack
.eq(1)
1258 m
.d
.sync
+= self
.out_roundz
.eq(self
.mod
.out_of
.roundz
)
1261 class FPNormToPack(FPState
, FPID
):
1263 def __init__(self
, width
, id_wid
):
1264 FPID
.__init
__(self
, id_wid
)
1265 FPState
.__init
__(self
, "normalise_1")
1268 def setup(self
, m
, in_z
, in_of
, in_mid
):
1269 """ links module to inputs and outputs
1272 # Normalisation (chained to input in_z+in_of)
1273 nmod
= FPNorm1ModSingle(self
.width
, self
.id_wid
)
1274 n_out
= nmod
.ospec()
1275 nmod
.setup(m
, in_z
, in_of
, n_out
.z
)
1276 m
.d
.comb
+= n_out
.roundz
.eq(nmod
.o
.roundz
)
1278 # Rounding (chained to normalisation)
1279 rmod
= FPRoundMod(self
.width
, self
.id_wid
)
1280 r_out_z
= rmod
.ospec()
1281 rmod
.setup(m
, n_out
.z
, n_out
.roundz
)
1282 m
.d
.comb
+= r_out_z
.eq(rmod
.out_z
)
1284 # Corrections (chained to rounding)
1285 cmod
= FPCorrectionsMod(self
.width
, self
.id_wid
)
1286 c_out_z
= cmod
.ospec()
1287 cmod
.setup(m
, r_out_z
)
1288 m
.d
.comb
+= c_out_z
.eq(cmod
.out_z
)
1290 # Pack (chained to corrections)
1291 self
.pmod
= FPPackMod(self
.width
, self
.id_wid
)
1292 self
.out_z
= self
.pmod
.ospec()
1293 self
.pmod
.setup(m
, c_out_z
)
1296 if self
.in_mid
is not None:
1297 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
1299 def action(self
, m
):
1300 self
.idsync(m
) # copies incoming ID to outgoing
1301 m
.d
.sync
+= self
.out_z
.z
.v
.eq(self
.pmod
.o
.z
.v
) # outputs packed result
1302 m
.next
= "pack_put_z"
1307 def __init__(self
, width
, id_wid
):
1308 self
.z
= FPNumBase(width
, False)
1309 self
.mid
= Signal(id_wid
, reset_less
=True)
1312 return [self
.z
.eq(i
.z
), self
.mid
.eq(i
.mid
)]
1317 def __init__(self
, width
, id_wid
):
1319 self
.id_wid
= id_wid
1320 self
.i
= self
.ispec()
1321 self
.out_z
= self
.ospec()
1324 return FPNorm1Data(self
.width
, self
.id_wid
)
1327 return FPRoundData(self
.width
, self
.id_wid
)
1329 def setup(self
, m
, in_z
, roundz
):
1330 m
.submodules
.roundz
= self
1332 m
.d
.comb
+= self
.i
.z
.eq(in_z
)
1333 m
.d
.comb
+= self
.i
.roundz
.eq(roundz
)
1335 def elaborate(self
, platform
):
1337 m
.d
.comb
+= self
.out_z
.eq(self
.i
)
1338 with m
.If(self
.i
.roundz
):
1339 m
.d
.comb
+= self
.out_z
.z
.m
.eq(self
.i
.z
.m
+ 1) # mantissa rounds up
1340 with m
.If(self
.i
.z
.m
== self
.i
.z
.m1s
): # all 1s
1341 m
.d
.comb
+= self
.out_z
.z
.e
.eq(self
.i
.z
.e
+ 1) # exponent up
1345 class FPRound(FPState
, FPID
):
1347 def __init__(self
, width
, id_wid
):
1348 FPState
.__init
__(self
, "round")
1349 FPID
.__init
__(self
, id_wid
)
1350 self
.mod
= FPRoundMod(width
)
1351 self
.out_z
= self
.mod
.ospec()
1353 def setup(self
, m
, in_z
, roundz
, in_mid
):
1354 """ links module to inputs and outputs
1356 self
.mod
.setup(m
, in_z
, roundz
)
1358 if self
.in_mid
is not None:
1359 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
1361 def action(self
, m
):
1363 m
.d
.sync
+= self
.out_z
.eq(self
.mod
.out_z
)
1364 m
.next
= "corrections"
1367 class FPCorrectionsMod
:
1369 def __init__(self
, width
, id_wid
):
1371 self
.id_wid
= id_wid
1372 self
.in_z
= self
.ispec()
1373 self
.out_z
= self
.ospec()
1376 return FPRoundData(self
.width
, self
.id_wid
)
1379 return FPRoundData(self
.width
, self
.id_wid
)
1381 def setup(self
, m
, in_z
):
1382 """ links module to inputs and outputs
1384 m
.submodules
.corrections
= self
1385 m
.d
.comb
+= self
.in_z
.eq(in_z
)
1387 def elaborate(self
, platform
):
1389 m
.submodules
.corr_in_z
= self
.in_z
.z
1390 m
.submodules
.corr_out_z
= self
.out_z
.z
1391 m
.d
.comb
+= self
.out_z
.eq(self
.in_z
)
1392 with m
.If(self
.in_z
.z
.is_denormalised
):
1393 m
.d
.comb
+= self
.out_z
.z
.e
.eq(self
.in_z
.z
.N127
)
1397 class FPCorrections(FPState
, FPID
):
1399 def __init__(self
, width
, id_wid
):
1400 FPState
.__init
__(self
, "corrections")
1401 FPID
.__init
__(self
, id_wid
)
1402 self
.mod
= FPCorrectionsMod(width
)
1403 self
.out_z
= self
.mod
.ospec()
1405 def setup(self
, m
, in_z
, in_mid
):
1406 """ links module to inputs and outputs
1408 self
.mod
.setup(m
, in_z
)
1409 if self
.in_mid
is not None:
1410 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
1412 def action(self
, m
):
1414 m
.d
.sync
+= self
.out_z
.eq(self
.mod
.out_z
)
1420 def __init__(self
, width
, id_wid
):
1421 self
.z
= FPNumOut(width
, False)
1422 self
.mid
= Signal(id_wid
, reset_less
=True)
1425 return [self
.z
.eq(i
.z
), self
.mid
.eq(i
.mid
)]
1430 def __init__(self
, width
, id_wid
):
1432 self
.id_wid
= id_wid
1433 self
.i
= self
.ispec()
1434 self
.o
= self
.ospec()
1437 return FPRoundData(self
.width
, self
.id_wid
)
1440 return FPPackData(self
.width
, self
.id_wid
)
1442 def setup(self
, m
, in_z
):
1443 """ links module to inputs and outputs
1445 m
.submodules
.pack
= self
1446 m
.d
.comb
+= self
.i
.eq(in_z
)
1448 def elaborate(self
, platform
):
1450 m
.submodules
.pack_in_z
= self
.i
.z
1451 with m
.If(self
.i
.z
.is_overflowed
):
1452 m
.d
.comb
+= self
.o
.z
.inf(self
.i
.z
.s
)
1454 m
.d
.comb
+= self
.o
.z
.create(self
.i
.z
.s
, self
.i
.z
.e
, self
.i
.z
.m
)
1459 def __init__(self
, width
, id_wid
):
1460 self
.z
= FPNumOut(width
, False)
1461 self
.mid
= Signal(id_wid
, reset_less
=True)
1464 return [self
.z
.eq(i
.z
), self
.mid
.eq(i
.mid
)]
1467 class FPPack(FPState
, FPID
):
1469 def __init__(self
, width
, id_wid
):
1470 FPState
.__init
__(self
, "pack")
1471 FPID
.__init
__(self
, id_wid
)
1472 self
.mod
= FPPackMod(width
)
1473 self
.out_z
= self
.ospec()
1476 return self
.mod
.ispec()
1479 return self
.mod
.ospec()
1481 def setup(self
, m
, in_z
, in_mid
):
1482 """ links module to inputs and outputs
1484 self
.mod
.setup(m
, in_z
)
1485 if self
.in_mid
is not None:
1486 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
1488 def action(self
, m
):
1490 m
.d
.sync
+= self
.out_z
.v
.eq(self
.mod
.out_z
.v
)
1491 m
.next
= "pack_put_z"
1494 class FPPutZ(FPState
):
1496 def __init__(self
, state
, in_z
, out_z
, in_mid
, out_mid
, to_state
=None):
1497 FPState
.__init
__(self
, state
)
1498 if to_state
is None:
1499 to_state
= "get_ops"
1500 self
.to_state
= to_state
1503 self
.in_mid
= in_mid
1504 self
.out_mid
= out_mid
1506 def action(self
, m
):
1507 if self
.in_mid
is not None:
1508 m
.d
.sync
+= self
.out_mid
.eq(self
.in_mid
)
1510 self
.out_z
.v
.eq(self
.in_z
.v
)
1512 with m
.If(self
.out_z
.stb
& self
.out_z
.ack
):
1513 m
.d
.sync
+= self
.out_z
.stb
.eq(0)
1514 m
.next
= self
.to_state
1516 m
.d
.sync
+= self
.out_z
.stb
.eq(1)
1519 class FPPutZIdx(FPState
):
1521 def __init__(self
, state
, in_z
, out_zs
, in_mid
, to_state
=None):
1522 FPState
.__init
__(self
, state
)
1523 if to_state
is None:
1524 to_state
= "get_ops"
1525 self
.to_state
= to_state
1527 self
.out_zs
= out_zs
1528 self
.in_mid
= in_mid
1530 def action(self
, m
):
1531 outz_stb
= Signal(reset_less
=True)
1532 outz_ack
= Signal(reset_less
=True)
1533 m
.d
.comb
+= [outz_stb
.eq(self
.out_zs
[self
.in_mid
].stb
),
1534 outz_ack
.eq(self
.out_zs
[self
.in_mid
].ack
),
1537 self
.out_zs
[self
.in_mid
].v
.eq(self
.in_z
.v
)
1539 with m
.If(outz_stb
& outz_ack
):
1540 m
.d
.sync
+= self
.out_zs
[self
.in_mid
].stb
.eq(0)
1541 m
.next
= self
.to_state
1543 m
.d
.sync
+= self
.out_zs
[self
.in_mid
].stb
.eq(1)
1546 class FPADDBaseMod(FPID
):
1548 def __init__(self
, width
, id_wid
=None, single_cycle
=False, compact
=True):
1551 * width: bit-width of IEEE754. supported: 16, 32, 64
1552 * id_wid: an identifier that is sync-connected to the input
1553 * single_cycle: True indicates each stage to complete in 1 clock
1554 * compact: True indicates a reduced number of stages
1556 FPID
.__init
__(self
, id_wid
)
1558 self
.single_cycle
= single_cycle
1559 self
.compact
= compact
1561 self
.in_t
= Trigger()
1562 self
.in_a
= Signal(width
)
1563 self
.in_b
= Signal(width
)
1564 self
.out_z
= FPOp(width
)
1568 def add_state(self
, state
):
1569 self
.states
.append(state
)
1572 def get_fragment(self
, platform
=None):
1573 """ creates the HDL code-fragment for FPAdd
1576 m
.submodules
.out_z
= self
.out_z
1577 m
.submodules
.in_t
= self
.in_t
1579 self
.get_compact_fragment(m
, platform
)
1581 self
.get_longer_fragment(m
, platform
)
1583 with m
.FSM() as fsm
:
1585 for state
in self
.states
:
1586 with m
.State(state
.state_from
):
1591 def get_longer_fragment(self
, m
, platform
=None):
1593 get
= self
.add_state(FPGet2Op("get_ops", "special_cases",
1594 self
.in_a
, self
.in_b
, self
.width
))
1595 get
.setup(m
, self
.in_a
, self
.in_b
, self
.in_t
.stb
, self
.in_t
.ack
)
1599 sc
= self
.add_state(FPAddSpecialCases(self
.width
, self
.id_wid
))
1600 sc
.setup(m
, a
, b
, self
.in_mid
)
1602 dn
= self
.add_state(FPAddDeNorm(self
.width
, self
.id_wid
))
1603 dn
.setup(m
, a
, b
, sc
.in_mid
)
1605 if self
.single_cycle
:
1606 alm
= self
.add_state(FPAddAlignSingle(self
.width
, self
.id_wid
))
1607 alm
.setup(m
, dn
.out_a
, dn
.out_b
, dn
.in_mid
)
1609 alm
= self
.add_state(FPAddAlignMulti(self
.width
, self
.id_wid
))
1610 alm
.setup(m
, dn
.out_a
, dn
.out_b
, dn
.in_mid
)
1612 add0
= self
.add_state(FPAddStage0(self
.width
, self
.id_wid
))
1613 add0
.setup(m
, alm
.out_a
, alm
.out_b
, alm
.in_mid
)
1615 add1
= self
.add_state(FPAddStage1(self
.width
, self
.id_wid
))
1616 add1
.setup(m
, add0
.out_tot
, add0
.out_z
, add0
.in_mid
)
1618 if self
.single_cycle
:
1619 n1
= self
.add_state(FPNorm1Single(self
.width
, self
.id_wid
))
1620 n1
.setup(m
, add1
.out_z
, add1
.out_of
, add0
.in_mid
)
1622 n1
= self
.add_state(FPNorm1Multi(self
.width
, self
.id_wid
))
1623 n1
.setup(m
, add1
.out_z
, add1
.out_of
, add1
.norm_stb
, add0
.in_mid
)
1625 rn
= self
.add_state(FPRound(self
.width
, self
.id_wid
))
1626 rn
.setup(m
, n1
.out_z
, n1
.out_roundz
, n1
.in_mid
)
1628 cor
= self
.add_state(FPCorrections(self
.width
, self
.id_wid
))
1629 cor
.setup(m
, rn
.out_z
, rn
.in_mid
)
1631 pa
= self
.add_state(FPPack(self
.width
, self
.id_wid
))
1632 pa
.setup(m
, cor
.out_z
, rn
.in_mid
)
1634 ppz
= self
.add_state(FPPutZ("pack_put_z", pa
.out_z
, self
.out_z
,
1635 pa
.in_mid
, self
.out_mid
))
1637 pz
= self
.add_state(FPPutZ("put_z", sc
.out_z
, self
.out_z
,
1638 pa
.in_mid
, self
.out_mid
))
1640 def get_compact_fragment(self
, m
, platform
=None):
1642 get
= self
.add_state(FPGet2Op("get_ops", "special_cases",
1643 self
.in_a
, self
.in_b
,
1644 self
.width
, self
.id_wid
))
1645 get
.setup(m
, self
.in_a
, self
.in_b
, self
.in_t
.stb
, self
.in_t
.ack
)
1649 sc
= self
.add_state(FPAddSpecialCasesDeNorm(self
.width
, self
.id_wid
))
1650 sc
.setup(m
, a
, b
, self
.in_mid
)
1652 alm
= self
.add_state(FPAddAlignSingleAdd(self
.width
, self
.id_wid
))
1653 alm
.setup(m
, sc
.o
.a
, sc
.o
.b
, sc
.in_mid
)
1655 n1
= self
.add_state(FPNormToPack(self
.width
, self
.id_wid
))
1656 n1
.setup(m
, alm
.a1o
.z
, alm
.a1o
.of
, alm
.in_mid
)
1658 ppz
= self
.add_state(FPPutZ("pack_put_z", n1
.out_z
.z
, self
.out_z
,
1659 n1
.in_mid
, self
.out_mid
))
1661 pz
= self
.add_state(FPPutZ("put_z", sc
.out_z
.z
, self
.out_z
,
1662 sc
.in_mid
, self
.out_mid
))
1665 class FPADDBase(FPState
, FPID
):
1667 def __init__(self
, width
, id_wid
=None, single_cycle
=False):
1670 * width: bit-width of IEEE754. supported: 16, 32, 64
1671 * id_wid: an identifier that is sync-connected to the input
1672 * single_cycle: True indicates each stage to complete in 1 clock
1674 FPID
.__init
__(self
, id_wid
)
1675 FPState
.__init
__(self
, "fpadd")
1677 self
.single_cycle
= single_cycle
1678 self
.mod
= FPADDBaseMod(width
, id_wid
, single_cycle
)
1680 self
.in_t
= Trigger()
1681 self
.in_a
= Signal(width
)
1682 self
.in_b
= Signal(width
)
1683 #self.out_z = FPOp(width)
1685 self
.z_done
= Signal(reset_less
=True) # connects to out_z Strobe
1686 self
.in_accept
= Signal(reset_less
=True)
1687 self
.add_stb
= Signal(reset_less
=True)
1688 self
.add_ack
= Signal(reset
=0, reset_less
=True)
1690 def setup(self
, m
, a
, b
, add_stb
, in_mid
, out_z
, out_mid
):
1692 self
.out_mid
= out_mid
1693 m
.d
.comb
+= [self
.in_a
.eq(a
),
1695 self
.mod
.in_a
.eq(self
.in_a
),
1696 self
.mod
.in_b
.eq(self
.in_b
),
1697 self
.in_mid
.eq(in_mid
),
1698 self
.mod
.in_mid
.eq(self
.in_mid
),
1699 self
.z_done
.eq(self
.mod
.out_z
.trigger
),
1700 #self.add_stb.eq(add_stb),
1701 self
.mod
.in_t
.stb
.eq(self
.in_t
.stb
),
1702 self
.in_t
.ack
.eq(self
.mod
.in_t
.ack
),
1703 self
.out_mid
.eq(self
.mod
.out_mid
),
1704 self
.out_z
.v
.eq(self
.mod
.out_z
.v
),
1705 self
.out_z
.stb
.eq(self
.mod
.out_z
.stb
),
1706 self
.mod
.out_z
.ack
.eq(self
.out_z
.ack
),
1709 m
.d
.sync
+= self
.add_stb
.eq(add_stb
)
1710 m
.d
.sync
+= self
.add_ack
.eq(0) # sets to zero when not in active state
1711 m
.d
.sync
+= self
.out_z
.ack
.eq(0) # likewise
1712 #m.d.sync += self.in_t.stb.eq(0)
1714 m
.submodules
.fpadd
= self
.mod
1716 def action(self
, m
):
1718 # in_accept is set on incoming strobe HIGH and ack LOW.
1719 m
.d
.comb
+= self
.in_accept
.eq((~self
.add_ack
) & (self
.add_stb
))
1721 #with m.If(self.in_t.ack):
1722 # m.d.sync += self.in_t.stb.eq(0)
1723 with m
.If(~self
.z_done
):
1724 # not done: test for accepting an incoming operand pair
1725 with m
.If(self
.in_accept
):
1727 self
.add_ack
.eq(1), # acknowledge receipt...
1728 self
.in_t
.stb
.eq(1), # initiate add
1731 m
.d
.sync
+= [self
.add_ack
.eq(0),
1732 self
.in_t
.stb
.eq(0),
1733 self
.out_z
.ack
.eq(1),
1736 # done: acknowledge, and write out id and value
1737 m
.d
.sync
+= [self
.add_ack
.eq(1),
1744 if self
.in_mid
is not None:
1745 m
.d
.sync
+= self
.out_mid
.eq(self
.mod
.out_mid
)
1748 self
.out_z
.v
.eq(self
.mod
.out_z
.v
)
1750 # move to output state on detecting z ack
1751 with m
.If(self
.out_z
.trigger
):
1752 m
.d
.sync
+= self
.out_z
.stb
.eq(0)
1755 m
.d
.sync
+= self
.out_z
.stb
.eq(1)
1758 def __init__(self
, width
, id_wid
):
1760 self
.id_wid
= id_wid
1762 for i
in range(rs_sz
):
1764 out_z
.name
= "out_z_%d" % i
1766 self
.res
= Array(res
)
1767 self
.in_z
= FPOp(width
)
1768 self
.in_mid
= Signal(self
.id_wid
, reset_less
=True)
1770 def setup(self
, m
, in_z
, in_mid
):
1771 m
.d
.comb
+= [self
.in_z
.eq(in_z
),
1772 self
.in_mid
.eq(in_mid
)]
1774 def get_fragment(self
, platform
=None):
1775 """ creates the HDL code-fragment for FPAdd
1778 m
.submodules
.res_in_z
= self
.in_z
1779 m
.submodules
+= self
.res
1791 """ FPADD: stages as follows:
1797 FPAddBase---> FPAddBaseMod
1799 PutZ GetOps->Specials->Align->Add1/2->Norm->Round/Pack->PutZ
1801 FPAddBase is tricky: it is both a stage and *has* stages.
1802 Connection to FPAddBaseMod therefore requires an in stb/ack
1803 and an out stb/ack. Just as with Add1-Norm1 interaction, FPGetOp
1804 needs to be the thing that raises the incoming stb.
1807 def __init__(self
, width
, id_wid
=None, single_cycle
=False, rs_sz
=2):
1810 * width: bit-width of IEEE754. supported: 16, 32, 64
1811 * id_wid: an identifier that is sync-connected to the input
1812 * single_cycle: True indicates each stage to complete in 1 clock
1815 self
.id_wid
= id_wid
1816 self
.single_cycle
= single_cycle
1818 #self.out_z = FPOp(width)
1819 self
.ids
= FPID(id_wid
)
1822 for i
in range(rs_sz
):
1825 in_a
.name
= "in_a_%d" % i
1826 in_b
.name
= "in_b_%d" % i
1827 rs
.append((in_a
, in_b
))
1831 for i
in range(rs_sz
):
1833 out_z
.name
= "out_z_%d" % i
1835 self
.res
= Array(res
)
1839 def add_state(self
, state
):
1840 self
.states
.append(state
)
1843 def get_fragment(self
, platform
=None):
1844 """ creates the HDL code-fragment for FPAdd
1847 m
.submodules
+= self
.rs
1849 in_a
= self
.rs
[0][0]
1850 in_b
= self
.rs
[0][1]
1852 out_z
= FPOp(self
.width
)
1853 out_mid
= Signal(self
.id_wid
, reset_less
=True)
1854 m
.submodules
.out_z
= out_z
1856 geta
= self
.add_state(FPGetOp("get_a", "get_b",
1861 getb
= self
.add_state(FPGetOp("get_b", "fpadd",
1866 ab
= FPADDBase(self
.width
, self
.id_wid
, self
.single_cycle
)
1867 ab
= self
.add_state(ab
)
1868 ab
.setup(m
, a
, b
, getb
.out_decode
, self
.ids
.in_mid
,
1871 pz
= self
.add_state(FPPutZIdx("put_z", ab
.out_z
, self
.res
,
1874 with m
.FSM() as fsm
:
1876 for state
in self
.states
:
1877 with m
.State(state
.state_from
):
1883 if __name__
== "__main__":
1885 alu
= FPADD(width
=32, id_wid
=5, single_cycle
=True)
1886 main(alu
, ports
=alu
.rs
[0][0].ports() + \
1887 alu
.rs
[0][1].ports() + \
1888 alu
.res
[0].ports() + \
1889 [alu
.ids
.in_mid
, alu
.ids
.out_mid
])
1891 alu
= FPADDBase(width
=32, id_wid
=5, single_cycle
=True)
1892 main(alu
, ports
=[alu
.in_a
, alu
.in_b
] + \
1893 alu
.in_t
.ports() + \
1894 alu
.out_z
.ports() + \
1895 [alu
.in_mid
, alu
.out_mid
])
1898 # works... but don't use, just do "python fname.py convert -t v"
1899 #print (verilog.convert(alu, ports=[
1900 # ports=alu.in_a.ports() + \
1901 # alu.in_b.ports() + \
1902 # alu.out_z.ports())