summaryrefslogtreecommitdiff
path: root/src/test/scala
diff options
context:
space:
mode:
authorchick2019-12-12 12:06:15 -0800
committerchick2019-12-18 13:57:50 -0800
commit0bcce65d5e3001b1b7098aa2c1ccd60fcc2a6628 (patch)
treeba737c4c886005552b0e95fb48f5ff5cbdc31ea9 /src/test/scala
parent954cc41e1349d0df6d2250d6270590340cd36e82 (diff)
- New trait HasBinaryPoint which provides literal values as double and big decimal
- made .F and .I work for creating fixed point and interval lits from big decimal - Added NumObject trait which provides new math conversions - Made a Num object that extends NumObject - Add this trait to FixedPoint and Interval for backward compatibility - Removed code that is now in NumObject, keeping things DRY - Add tests to FixedPointSpec to show lit conversion to double and big decimal - Add tests to IntervalSpec to show lit conversion to double and big decimal - Add tests to LiteralExtractorSpec to show general math conversions between BigInts with binary points and double and big decimal
Diffstat (limited to 'src/test/scala')
-rw-r--r--src/test/scala/chiselTests/FixedPointSpec.scala15
-rw-r--r--src/test/scala/chiselTests/IntervalSpec.scala59
-rw-r--r--src/test/scala/chiselTests/LiteralExtractorSpec.scala43
3 files changed, 115 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 863771a3..ae7fdabf 100644
--- a/src/test/scala/chiselTests/IntervalSpec.scala
+++ b/src/test/scala/chiselTests/IntervalSpec.scala
@@ -456,12 +456,69 @@ 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"
+ val in1 = (-6.0).I(inputRange)
+ val in2 = 6.0.I(inputRange)
+ BigDecimal(in1.litValue()) / (1 << inputRange.binaryPoint.get) should be (-6)
+ BigDecimal(in2.litValue()) / (1 << inputRange.binaryPoint.get) should be (6)
+ intercept[ChiselException] {
+ (-6.25).I(inputRange)
+ }
+ intercept[ChiselException] {
+ (6.25).I(inputRange)
+ }
+ }
+ "value at open boundaries works" in {
+ val inputRange = range"(-6, 6).2"
+ val in1 = (-5.75).I(inputRange)
+ val in2 = 5.75.I(inputRange)
+ BigDecimal(in1.litValue()) / (1 << inputRange.binaryPoint.get) should be (-5.75)
+ BigDecimal(in2.litValue()) / (1 << inputRange.binaryPoint.get) should be (5.75)
+ intercept[ChiselException] {
+ (-6.0).I(inputRange)
+ }
+ intercept[ChiselException] {
+ (6.0).I(inputRange)
+ }
+ }
+ "values not precisely at open boundaries works but are converted to nearest match" in {
+ val inputRange = range"(-6, 6).2"
+ val in1 = (-5.95).I(inputRange)
+ val in2 = 5.95.I(inputRange)
+ BigDecimal(in1.litValue()) / (1 << inputRange.binaryPoint.get) should be (-5.75)
+ BigDecimal(in2.litValue()) / (1 << inputRange.binaryPoint.get) should be (5.75)
+ intercept[ChiselException] {
+ (-6.1).I(inputRange)
+ }
+ intercept[ChiselException] {
+ (6.1).I(inputRange)
+ }
+ }
+ }
+
"Let's take a look at the results of squeeze over small range" in {
assertTesterPasses {
new ClipSqueezeWrapDemo(
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 {