Skip to content

Commit 6a63353

Browse files
TruthTable improvements: structural equality and delete sort (backport #2935) (#2936)
* TruthTable improvements: structural equality and delete sort (#2935) We had implemented equals, but not hashCode. This commit also changes the implemental of equals to just use the underlying values instead of wasting the compute calling .toString. Delete TruthTable.sort, it is unused. (cherry picked from commit b5d9c08) * Restore and deprecate TruthTable.sort Co-authored-by: Jack Koenig <koenig@sifive.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent d4570fb commit 6a63353

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/main/scala/chisel3/util/experimental/decode/TruthTable.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,35 @@
33
package chisel3.util.experimental.decode
44

55
import chisel3.util.BitPat
6+
import scala.util.hashing.MurmurHash3
67
import scala.collection.mutable
78

8-
sealed class TruthTable private (val table: Seq[(BitPat, BitPat)], val default: BitPat, val sort: Boolean) {
9+
sealed class TruthTable private (val table: Seq[(BitPat, BitPat)], val default: BitPat, _sort: Boolean) {
910
def inputWidth = table.head._1.getWidth
1011

1112
def outputWidth = table.head._2.getWidth
1213

14+
@deprecated("This field is unused and will be removed.", "Chisel 3.5")
15+
def sort: Boolean = _sort
16+
1317
override def toString: String = {
1418
def writeRow(map: (BitPat, BitPat)): String =
1519
s"${map._1.rawString}->${map._2.rawString}"
1620

1721
(table.map(writeRow) ++ Seq(s"${" " * (inputWidth + 2)}${default.rawString}")).mkString("\n")
1822
}
1923

20-
def copy(table: Seq[(BitPat, BitPat)] = this.table, default: BitPat = this.default, sort: Boolean = this.sort) =
21-
TruthTable(table, default, sort)
24+
def copy(table: Seq[(BitPat, BitPat)] = this.table, default: BitPat = this.default, sort: Boolean = _sort) =
25+
TruthTable(table, default)
2226

2327
override def equals(y: Any): Boolean = {
2428
y match {
25-
case y: TruthTable => toString == y.toString
29+
case that: TruthTable => this.table == that.table && this.default == that.default
2630
case _ => false
2731
}
2832
}
33+
34+
override lazy val hashCode: Int = MurmurHash3.productHash((table, default))
2935
}
3036

3137
object TruthTable {

src/test/scala/chiselTests/util/experimental/TruthTableSpec.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ class TruthTableSpec extends AnyFlatSpec {
3535
assert(table.toString contains " 0")
3636
}
3737
"TruthTable" should "deserialize" in {
38-
assert(TruthTable.fromString(str) == table)
38+
val table2 = TruthTable.fromString(str)
39+
assert(table2 === table)
40+
assert(table2.hashCode === table.hashCode)
3941
}
4042
"TruthTable" should "merge same key" in {
4143
assert(

0 commit comments

Comments
 (0)