1 from nmutil
.pipemodbase
import PipeModBaseChain
, PipeModBase
2 from ieee754
.fpcommon
.fpbase
import OverflowMod
3 from ieee754
.fpcommon
.postnormalise
import FPNorm1Data
4 from ieee754
.fpcommon
.roundz
import FPRoundMod
5 from ieee754
.fpcommon
.corrections
import FPCorrectionsMod
6 from ieee754
.fpcommon
.pack
import FPPackMod
7 from ieee754
.fpfma
.main_stage
import FPFMAPostCalcData
8 from nmigen
.hdl
.dsl
import Module
9 from nmigen
.hdl
.ast
import Signal
10 from ieee754
.fpfma
.util
import get_fpformat
11 from nmigen
.lib
.coding
import PriorityEncoder
14 class FPFMANorm(PipeModBase
):
15 def __init__(self
, pspec
):
16 super().__init
__(pspec
, "norm")
19 return FPFMAPostCalcData(self
.pspec
)
22 return FPNorm1Data(self
.pspec
)
24 def elaborate(self
, platform
):
26 fpf
= get_fpformat(self
.pspec
)
28 inp
: FPFMAPostCalcData
= self
.i
29 out
: FPNorm1Data
= self
.o
30 m
.submodules
.pri_enc
= pri_enc
= PriorityEncoder(inp
.mantissa
.width
)
31 m
.d
.comb
+= pri_enc
.i
.eq(inp
.mantissa
[::-1])
32 unrestricted_shift_amount
= Signal(range(inp
.mantissa
.width
))
33 shift_amount
= Signal(range(inp
.mantissa
.width
))
34 m
.d
.comb
+= unrestricted_shift_amount
.eq(pri_enc
.o
)
35 with m
.If(inp
.exponent
- (1 + fpf
.e_sub
) < unrestricted_shift_amount
):
36 m
.d
.comb
+= shift_amount
.eq(inp
.exponent
- (1 + fpf
.e_sub
))
38 m
.d
.comb
+= shift_amount
.eq(unrestricted_shift_amount
)
39 n_mantissa
= Signal(inp
.mantissa
.width
)
40 m
.d
.comb
+= n_mantissa
.eq(inp
.mantissa
<< shift_amount
)
42 m
.submodules
.of
= of
= OverflowMod()
44 pri_enc
.i
.eq(inp
.mantissa
[::-1]),
45 of
.guard
.eq(n_mantissa
[-(out
.z
.m
.width
+ 1)]),
46 of
.round_bit
.eq(n_mantissa
[-(out
.z
.m
.width
+ 2)]),
47 of
.sticky
.eq(n_mantissa
[:-(out
.z
.m
.width
+ 2)].bool()),
52 out
.roundz
.eq(of
.roundz_out
),
54 out
.z
.e
.eq(inp
.exponent
- shift_amount
),
55 out
.z
.m
.eq(n_mantissa
[-out
.z
.m
.width
:]),
56 out
.out_do_z
.eq(inp
.do_bypass
),
57 out
.oz
.eq(inp
.bypassed_z
),
64 class FPFMANormToPack(PipeModBaseChain
):
65 def __init__(self
, pspec
):
66 super().__init
__(pspec
)
69 """ gets chain of modules
71 nmod
= FPFMANorm(self
.pspec
)
72 rmod
= FPRoundMod(self
.pspec
)
73 cmod
= FPCorrectionsMod(self
.pspec
)
74 pmod
= FPPackMod(self
.pspec
)
75 return [nmod
, rmod
, cmod
, pmod
]