summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/scala/chisel3/util/Mux.scala19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/main/scala/chisel3/util/Mux.scala b/src/main/scala/chisel3/util/Mux.scala
index 342531bd..b9a5f576 100644
--- a/src/main/scala/chisel3/util/Mux.scala
+++ b/src/main/scala/chisel3/util/Mux.scala
@@ -63,10 +63,21 @@ object MuxLookup {
* @return the value found or the default if not
*/
def apply[S <: UInt, T <: Data] (key: S, default: T, mapping: Seq[(S, T)]): T = {
- var res = default
- for ((k, v) <- mapping.reverse)
- res = Mux(k === key, v, res)
- res
+ /* If the mapping is defined for all possible values of the key, then don't use the default value */
+ val (defaultx, mappingx) = key.widthOption match {
+ case Some(width) =>
+ val keySetSize = BigInt(1) << width
+ val keyMask = keySetSize - 1
+ val distinctLitKeys = mapping.flatMap(_._1.litOption).map(_ & keyMask).distinct
+ if (distinctLitKeys.size == keySetSize) {
+ (mapping.head._2, mapping.tail)
+ } else {
+ (default, mapping)
+ }
+ case None => (default, mapping)
+ }
+
+ mappingx.foldLeft(defaultx){ case (d, (k, v)) => Mux(k === key, v, d) }
}
}