summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/scala/chisel3/util/Math.scala36
-rw-r--r--src/test/scala/chiselTests/Math.scala50
2 files changed, 84 insertions, 2 deletions
diff --git a/src/main/scala/chisel3/util/Math.scala b/src/main/scala/chisel3/util/Math.scala
index f9278c7d..105b4161 100644
--- a/src/main/scala/chisel3/util/Math.scala
+++ b/src/main/scala/chisel3/util/Math.scala
@@ -5,8 +5,7 @@
package chisel3.util
-import chisel3._
-import chisel3.internal.chiselRuntimeDeprecated
+import chisel3.internal
/** Compute the log2 of a Scala integer, rounded up, with min value of 1.
* Useful for getting the number of bits needed to represent some number of states (in - 1),
@@ -99,3 +98,36 @@ object isPow2 {
def apply(in: BigInt): Boolean = in > 0 && ((in & (in-1)) == 0)
def apply(in: Int): Boolean = apply(BigInt(in))
}
+
+
+object unsignedBitLength {
+ /** Return the number of bits required to encode a specific value, assuming no sign bit is required.
+ *
+ * Basically, `n.bitLength`. NOTE: This will return 0 for a value of 0.
+ * This reflects the Chisel assumption that a zero width wire has a value of 0.
+ * @param in - the number to be encoded.
+ * @return - an Int representing the number of bits to encode.
+ */
+ def apply(in: BigInt): Int = {
+ require(in >= 0)
+ in.bitLength
+ }
+}
+
+object signedBitLength {
+ /** Return the number of bits required to encode a specific value, assuming a sign bit is required.
+ *
+ * Basically, 0 for 0, 1 for -1, and `n.bitLength` + 1 for everything else.
+ * This reflects the Chisel assumption that a zero width wire has a value of 0.
+ * @param in - the number to be encoded.
+ * @return - an Int representing the number of bits to encode.
+ */
+ def apply(in: BigInt): Int = {
+ in.toInt match {
+ case 0 => 0
+ case -1 => 1
+ case _ => in.bitLength + 1
+ }
+
+ }
+}
diff --git a/src/test/scala/chiselTests/Math.scala b/src/test/scala/chiselTests/Math.scala
new file mode 100644
index 00000000..aa32a828
--- /dev/null
+++ b/src/test/scala/chiselTests/Math.scala
@@ -0,0 +1,50 @@
+// See LICENSE for license details.
+
+package chiselTests
+
+import org.scalacheck.Shrink
+
+class Math extends ChiselPropSpec {
+ import chisel3.util._
+ // Disable shrinking on error.
+ implicit val noShrinkListVal = Shrink[List[Int]](_ => Stream.empty)
+ implicit val noShrinkInt = Shrink[Int](_ => Stream.empty)
+
+ property ("unsignedBitLength is computed correctly") {
+ forAll(safeUIntWidth) { case (width: Int) =>
+ for ( offset <- List(-1, 0, 1)) {
+ val n = (1 << width) + offset
+ if (n >= 0) {
+ val d = unsignedBitLength(n)
+ val t = if (n == 0) 0 else if (offset < 0) width else width + 1
+ d shouldEqual (t)
+ }
+ }
+ }
+ }
+
+ property ("signedBitLength is computed correctly") {
+ forAll(safeUIntWidth) { case (width: Int) =>
+ for ( offset <- List(-1, 0, 1)) {
+ for ( mult <- List(-1, +1)) {
+ val n = ((1 << (width - 1)) + offset) * mult
+ val d = signedBitLength(n)
+ val t = n match {
+ case -2 => 2
+ case -1 => 1
+ case 0 => 0
+ case 1 => 2
+ case 2 => 3
+ case _ =>
+ if (n > 0) {
+ if (offset < 0) width else width + 1
+ } else {
+ if (offset > 0) width + 1 else width
+ }
+ }
+ d shouldEqual (t)
+ }
+ }
+ }
+ }
+}