summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel3/util/BitPat.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/chisel3/util/BitPat.scala')
-rw-r--r--src/main/scala/chisel3/util/BitPat.scala35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/main/scala/chisel3/util/BitPat.scala b/src/main/scala/chisel3/util/BitPat.scala
index d27fee14..7cb80e54 100644
--- a/src/main/scala/chisel3/util/BitPat.scala
+++ b/src/main/scala/chisel3/util/BitPat.scala
@@ -5,6 +5,8 @@ package chisel3.util
import scala.language.experimental.macros
import chisel3._
import chisel3.internal.sourceinfo.{SourceInfo, SourceInfoTransform}
+import scala.collection.mutable
+import scala.util.hashing.MurmurHash3
object BitPat {
@@ -253,6 +255,9 @@ sealed class BitPat(val value: BigInt, val mask: BigInt, val width: Int)
def =/=(that: UInt): Bool = macro SourceInfoTransform.thatArg
def ##(that: BitPat): BitPat = macro SourceInfoTransform.thatArg
+ override def hashCode: Int =
+ MurmurHash3.seqHash(Seq(this.value, this.mask, this.width))
+
/** @group SourceInfoTransformMacro */
def do_apply(x: Int)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): BitPat = {
do_apply(x, x)
@@ -348,15 +353,29 @@ sealed class BitPat(val value: BigInt, val mask: BigInt, val width: Int)
override def isEmpty: Boolean = false
/** Generate raw string of a [[BitPat]]. */
- def rawString: String = Seq
- .tabulate(width) { i =>
- (value.testBit(width - i - 1), mask.testBit(width - i - 1)) match {
- case (true, true) => "1"
- case (false, true) => "0"
- case (_, false) => "?"
- }
+ def rawString: String = _rawString
+
+ // This is micro-optimized and memoized because it is used for lots of BitPat operations
+ private lazy val _rawString: String = {
+ val sb = new StringBuilder(width)
+ var i = 0
+ while (i < width) {
+ val bitIdx = width - i - 1
+ val char =
+ if (mask.testBit(bitIdx)) {
+ if (value.testBit(bitIdx)) {
+ '1'
+ } else {
+ '0'
+ }
+ } else {
+ '?'
+ }
+ sb += char
+ i += 1
}
- .mkString
+ sb.result()
+ }
override def toString = s"BitPat($rawString)"
}