Skip to content

Commit 13952a5

Browse files
committed
Add. Underflow check in posit round
1 parent 19a2afb commit 13952a5

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/main/scala/PositRound.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ class PositRound(val nbits: Int, val es: Int) extends Module with HasHardPositPa
3434
val fracOverflow = roundFrac(maxFractionBitsWithHiddenBit)
3535

3636
val roundExp = WireInit(SInt((maxExponentBits + 1).W), io.in.exponent +& fracOverflow.zext)
37-
val overflow = roundExp >= maxExponent.S
37+
val overflow = roundExp >= maxExponent.S
38+
val underflow = roundExp < minExponent.S
3839

3940
io.out.exponent :=
40-
Mux(io.in.isNaR || io.in.isZero,
41-
0.S, Mux(overflow,
42-
maxExponent.S, roundExp))
41+
Mux(io.in.isNaR || io.in.isZero, 0.S,
42+
Mux(overflow, maxExponent.S,
43+
Mux(underflow, minExponent.S,
44+
roundExp)))
4345

4446
io.out.fraction :=
45-
Mux(fracOverflow || overflow || io.in.isNaR || io.in.isZero,
47+
Mux(fracOverflow || overflow || underflow || io.in.isNaR || io.in.isZero,
4648
Cat(1.U, 0.U(maxFractionBits.W)),
4749
roundFrac)
4850
}

src/main/scala/common.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ trait HasHardPositParams {
6666

6767
def maxRegime: Int = nbits - 2
6868

69+
def minRegime: Int = -maxRegime
70+
6971
def maxExponent: Int = maxRegime * (1 << es)
7072

73+
def minExponent: Int = minRegime * (1 << es)
74+
7175
def maxSignedInteger(w: Int): Int = (1 << (w - 1)) - 1
7276

7377
def maxUnsignedInteger(w: Int): Int = (1 << w) - 1

src/test/scala/PositRoundSpec.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ class PositRoundSpec extends ChiselFlatSpec {
4848
assert(test(16, 1, false, 0x1C, 0x1FFF, false, false, 3, true, 0x1C, 0x1000))
4949
}
5050

51-
51+
it should "round to minpos if underflow" in {
52+
assert(test(16, 1, false, -0x1D, 0x1FFF, false, false, 0, false, -0x1C, 0x1000))
53+
}
5254
}

0 commit comments

Comments
 (0)