@@ -16,77 +16,83 @@ class PositFMA(val totalBits: Int, val es: Int) extends Module with HasHardPosit
16
16
val out = Output (UInt (totalBits.W ))
17
17
})
18
18
19
- private val num1Extractor = Module (new PositExtractor (totalBits, es))
20
- private val num2Extractor = Module (new PositExtractor (totalBits, es))
21
- private val num3Extractor = Module (new PositExtractor (totalBits, es))
19
+ val num1Extractor = Module (new PositExtractor (totalBits, es))
20
+ val num2Extractor = Module (new PositExtractor (totalBits, es))
21
+ val num3Extractor = Module (new PositExtractor (totalBits, es))
22
22
23
23
num2Extractor.io.in := io.num2
24
24
num1Extractor.io.in := io.num1
25
25
num3Extractor.io.in := io.num3
26
26
27
- private val num1 = num1Extractor.io.out
28
- private val num2 = num2Extractor.io.out
29
- private val num3 = num3Extractor.io.out
27
+ val num1 = num1Extractor.io.out
28
+ val num2 = num2Extractor.io.out
29
+ val num3 = num3Extractor.io.out
30
30
31
- io.isNaR : = num1.isNaR || num2.isNaR || num3.isNaR
32
- io.isZero := (num1.isZero || num2.isZero) && num3.isZero
31
+ val productSign = num1.sign ^ num2.sign ^ io.negate
32
+ val addendSign = num3.sign ^ io.negate ^ io.sub
33
33
34
- private val productSign = num1.sign ^ num2.sign ^ io.negate
35
- private val addendSign = num3.sign ^ io.negate ^ io.sub
34
+ val productExponent = num1.exponent + num2.exponent
35
+ val productFraction =
36
+ WireInit (UInt (maxProductFractionBits.W ), num1.fraction * num2.fraction)
36
37
37
- private val productExponent = num1.exponent + num2.exponent
38
- private val productFraction = WireInit (UInt (maxProductFractionBits.W ), num1.fraction * num2.fraction)
38
+ val prodOverflow = productFraction(maxProductFractionBits - 1 )
39
+ val normProductFraction = (productFraction >> prodOverflow.asUInt()).asUInt()
40
+ val normProductExponent = productExponent + Mux (prodOverflow, 1 .S , 0 .S )
41
+ val prodStickyBit = Mux (prodOverflow, productFraction(0 ), false .B )
39
42
40
- private val prodOverflow = productFraction(maxProductFractionBits - 1 )
41
- private val normProductFraction = (productFraction >> prodOverflow.asUInt()).asUInt()
42
- private val normProductExponent = productExponent + Mux (prodOverflow, 1 .S , 0 .S )
43
- private val prodStickyBit = Mux (prodOverflow, productFraction(0 ), false .B )
43
+ val addendFraction = (num3.fraction << maxFractionBits).asUInt
44
+ val addendExponent = num3.exponent
44
45
45
- private val addendFraction = (num3.fraction << maxFractionBits).asUInt
46
- private val addendExponent = num3.exponent
46
+ val isAddendLargerThanProduct =
47
+ (addendExponent > normProductExponent) |
48
+ (addendExponent === normProductExponent &&
49
+ (addendFraction > normProductFraction))
47
50
48
- private val isAddendLargerThanProduct = (addendExponent > normProductExponent) | (addendExponent === normProductExponent && (addendFraction > normProductFraction))
51
+ val largeExp = Mux (isAddendLargerThanProduct, addendExponent, normProductExponent)
52
+ val largeFrac = Mux (isAddendLargerThanProduct, addendFraction, normProductFraction)
53
+ val largeSign = Mux (isAddendLargerThanProduct, addendSign, productSign)
49
54
50
- private val largerExponent = Mux (isAddendLargerThanProduct, addendExponent, normProductExponent )
51
- private val largerFraction = Mux (isAddendLargerThanProduct, addendFraction, normProductFraction )
52
- private val largerSign = Mux (isAddendLargerThanProduct, addendSign, productSign )
55
+ val smallExp = Mux (isAddendLargerThanProduct, normProductExponent, addendExponent )
56
+ val smallFrac = Mux (isAddendLargerThanProduct, normProductFraction, addendFraction )
57
+ val smallSign = Mux (isAddendLargerThanProduct, productSign, addendSign )
53
58
54
- private val smallerExponent = Mux (isAddendLargerThanProduct, normProductExponent, addendExponent)
55
- private val smallerFraction = Mux (isAddendLargerThanProduct, normProductFraction, addendFraction)
56
- private val smallerSign = Mux (isAddendLargerThanProduct, productSign, addendSign)
59
+ val expDiff = (largeExp - smallExp).asUInt()
60
+ val shiftedSmallFrac =
61
+ Mux (expDiff < maxProductFractionBits.U , smallFrac >> expDiff, 0 .U )
62
+ val smallFracStickyBit = (smallFrac & ((1 .U << expDiff) - 1 .U )).orR()
57
63
58
- private val exponentDifference = (largerExponent - smallerExponent).asUInt()
59
- private val shiftedSmallerFraction = (smallerFraction >> exponentDifference).asUInt()
60
- private val smallFractionStickyBit = (smallerFraction & ((1 .U << exponentDifference) - 1 .U )).orR()
64
+ val isAddition = ~ (largeSign ^ smallSign)
65
+ val signedSmallerFraction =
66
+ Mux (isAddition, shiftedSmallFrac, ~ shiftedSmallFrac + 1 .U )
67
+ val fmaFraction =
68
+ WireInit (UInt (maxProductFractionBits.W ), largeFrac +& signedSmallerFraction)
61
69
62
- private val isAddition = ~ (largerSign ^ smallerSign)
63
- private val signedSmallerFraction = Mux (isAddition, shiftedSmallerFraction, ~ shiftedSmallerFraction + 1 .U )
64
- private val fmaFraction = WireInit (UInt (maxProductFractionBits.W ), largerFraction +& signedSmallerFraction)
70
+ val sumOverflow = fmaFraction(maxProductFractionBits - 1 )
71
+ val adjFmaFraction =
72
+ Mux (isAddition, fmaFraction >> sumOverflow.asUInt(), fmaFraction(maxProductFractionBits - 2 , 0 ))
73
+ val adjFmaExponent = largeExp + Mux (isAddition & sumOverflow, 1 .S , 0 .S )
74
+ val sumStickyBit = Mux (isAddition & sumOverflow, fmaFraction(0 ), false .B )
65
75
66
- private val sumOverflow = fmaFraction(maxProductFractionBits - 1 )
67
- private val adjFmaFraction = Mux (isAddition, fmaFraction >> sumOverflow.asUInt(), fmaFraction(maxProductFractionBits - 2 , 0 ))
68
- private val adjFmaExponent = largerExponent + Mux (isAddition & sumOverflow, 1 .S , 0 .S )
69
- private val sumStickyBit = Mux (isAddition & sumOverflow, fmaFraction(0 ), false .B )
70
-
71
- private val normalizationFactor = MuxCase (0 .S , Array .range(0 , maxProductFractionBits - 2 ).map(index => {
76
+ val normalizationFactor = MuxCase (0 .S , Array .range(0 , maxProductFractionBits - 2 ).map(index => {
72
77
(adjFmaFraction(maxProductFractionBits - 2 , maxProductFractionBits - index - 2 ) === 1 .U ) -> index.S
73
78
}))
74
79
75
- private val normFmaExponent = adjFmaExponent - normalizationFactor
76
- private val normFmaFraction = adjFmaFraction << normalizationFactor.asUInt()
80
+ val normFmaExponent = adjFmaExponent - normalizationFactor
81
+ val normFmaFraction = adjFmaFraction << normalizationFactor.asUInt()
77
82
78
- private val result = Wire (new unpackedPosit(totalBits, es))
79
- result.isNaR := num1.isNaR || num2.isNaR || num3.isNaR
80
- result.isZero := (num1.isZero || num2.isZero) && num3.isZero
81
- result.sign := largerSign
83
+ val result = Wire (new unpackedPosit(totalBits, es))
84
+ result.isNaR := num1.isNaR || num2.isNaR || num3.isNaR
85
+ result.isZero := (num1.isZero || num2.isZero) && num3.isZero
86
+ result.sign := largeSign
82
87
result.exponent := normFmaExponent
83
88
result.fraction := (normFmaFraction >> maxFractionBits).asUInt()
84
- result.stickyBit := prodStickyBit | sumStickyBit | smallFractionStickyBit | normFmaFraction(maxFractionBits - 1 , 0 ).orR()
85
89
86
- private val positGenerator = Module (new PositGenerator (totalBits, es))
90
+ val positGenerator = Module (new PositGenerator (totalBits, es))
87
91
positGenerator.io.in <> result
92
+ positGenerator.io.trailingBits := normFmaFraction(maxFractionBits - 1 , maxFractionBits - trailingBitCount)
93
+ positGenerator.io.stickyBit := prodStickyBit | sumStickyBit | smallFracStickyBit | normFmaFraction(maxFractionBits - trailingBitCount - 1 , 0 ).orR()
88
94
89
- io.isNaR := result.isNaR || (positGenerator.io.out === NaR )
95
+ io.isNaR := result.isNaR || (positGenerator.io.out === NaR )
90
96
io.isZero := result.isZero || (positGenerator.io.out === 0 .U )
91
- io.out := Mux (result.isNaR, NaR , positGenerator.io.out)
97
+ io.out := Mux (result.isNaR, NaR , positGenerator.io.out)
92
98
}
0 commit comments