diff options
| author | Chick Markley | 2020-01-31 13:22:05 -0800 |
|---|---|---|
| committer | GitHub | 2020-01-31 13:22:05 -0800 |
| commit | efc40252631869531e79f4d8490113d18e75cc1d (patch) | |
| tree | b1377a66921f953458523b54b531298f56beeb69 /src | |
| parent | 86e92931dd1c83a863e14b382e9f094e8b18bc5c (diff) | |
| parent | f1c4395bd608234fef5a60d8851036d1acb2382f (diff) | |
Merge branch 'master' into add-asbool-to-clock
Diffstat (limited to 'src')
| -rw-r--r-- | src/test/scala/chiselTests/FixedPointSpec.scala | 15 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/IntervalSpec.scala | 17 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/LiteralExtractorSpec.scala | 43 |
3 files changed, 73 insertions, 2 deletions
diff --git a/src/test/scala/chiselTests/FixedPointSpec.scala b/src/test/scala/chiselTests/FixedPointSpec.scala index a928f08e..e97c6be7 100644 --- a/src/test/scala/chiselTests/FixedPointSpec.scala +++ b/src/test/scala/chiselTests/FixedPointSpec.scala @@ -19,6 +19,21 @@ class FixedPointLiteralSpec extends FlatSpec with Matchers { initialDouble should be(finalDouble) } + + they should "have their literals support to double and to BigDecimal" in { + val d = -7.125 + val lit1 = d.F(3.BP) + lit1.litToDouble should be (d) + + val d2 = BigDecimal("1232123213131123.125") + val lit2 = d2.F(3.BP) + lit2.litToBigDecimal should be (d2) + + // Numbers that are too big will throw exception + intercept[ChiselException] { + lit2.litToDouble + } + } } //noinspection TypeAnnotation,EmptyParenMethodAccessedAsParameterless diff --git a/src/test/scala/chiselTests/IntervalSpec.scala b/src/test/scala/chiselTests/IntervalSpec.scala index 1e56d8a3..ee704c83 100644 --- a/src/test/scala/chiselTests/IntervalSpec.scala +++ b/src/test/scala/chiselTests/IntervalSpec.scala @@ -456,12 +456,27 @@ class IntervalSpec extends FreeSpec with Matchers with ChiselRunners { () => new BasicTester { val x = 5.I(range"[0,4]") - } + } ).elaborate } } } + "Interval literals support to double and to BigDecimal" in { + val d = -7.125 + val lit1 = d.I(3.BP) + lit1.litToDouble should be (d) + + val d2 = BigDecimal("1232123213131123.125") + val lit2 = d2.I(3.BP) + lit2.litToBigDecimal should be (d2) + + // Numbers that are too big will throw exception + intercept[ChiselException] { + lit2.litToDouble + } + } + "Interval literals creation handles edge cases" - { "value at closed boundaries works" in { val inputRange = range"[-6, 6].2" diff --git a/src/test/scala/chiselTests/LiteralExtractorSpec.scala b/src/test/scala/chiselTests/LiteralExtractorSpec.scala index b56672af..145c1ef2 100644 --- a/src/test/scala/chiselTests/LiteralExtractorSpec.scala +++ b/src/test/scala/chiselTests/LiteralExtractorSpec.scala @@ -3,7 +3,7 @@ package chiselTests import chisel3._ -import chisel3.experimental.FixedPoint +import chisel3.experimental._ import chisel3.experimental.BundleLiterals._ import chisel3.testers.BasicTester @@ -55,6 +55,47 @@ class LiteralExtractorSpec extends ChiselFlatSpec { }} } + "doubles and big decimals" should "round trip properly" in { + intercept[ChiselException] { + Num.toBigDecimal(BigInt("1" * 109, 2), 0.BP) // this only works if number takes less than 109 bits + } + + intercept[ChiselException] { + Num.toDouble(BigInt("1" * 54, 2), 0.BP) // this only works if number takes less than 54 bits + } + + val bigInt108 = BigInt("1" * 108, 2) + val bigDecimal = Num.toBigDecimal(bigInt108, 2) + + val bigIntFromBigDecimal = Num.toBigInt(bigDecimal, 2) + + bigIntFromBigDecimal should be (bigInt108) + + val bigInt53 = BigInt("1" * 53, 2) + + val double = Num.toDouble(bigInt53, 2) + + val bigIntFromDouble = Num.toBigInt(double, 2) + + bigIntFromDouble should be (bigInt53) + } + + "encoding and decoding of Intervals" should "round trip" in { + val rangeMin = BigDecimal(-31.5) + val rangeMax = BigDecimal(32.5) + val range = range"($rangeMin, $rangeMax).2" + for(value <- (rangeMin - 4) to (rangeMax + 4) by 2.25) { + if (value < rangeMin || value > rangeMax) { + intercept[ChiselException] { + val literal = value.I(range) + } + } else { + val literal = value.I(range) + literal.isLit() should be(true) + literal.litValue().toDouble / 4.0 should be(value) + } + } + } "literals declared outside a builder context" should "compare with those inside builder context" in { class InsideBundle extends Bundle { |
