diff options
| author | Andrew Waterman | 2019-03-15 16:38:46 -0700 |
|---|---|---|
| committer | GitHub | 2019-03-15 16:38:46 -0700 |
| commit | cfb2f08db9d9df121a82f138dd71297dbcea66cc (patch) | |
| tree | 08bf92d133f5e05e155878fcbd64e41cee892a6d /src/test/scala/chiselTests/PopCount.scala | |
| parent | ab68f5d76f645b721614f0efa8d55ab7d1e08a3d (diff) | |
| parent | d3280685892bbf0fd9b3c0f3d9e6750a30d25593 (diff) | |
Merge pull request #1033 from freechipsproject/popcount
Tighten inferred width for PopCount
Diffstat (limited to 'src/test/scala/chiselTests/PopCount.scala')
| -rw-r--r-- | src/test/scala/chiselTests/PopCount.scala | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/test/scala/chiselTests/PopCount.scala b/src/test/scala/chiselTests/PopCount.scala new file mode 100644 index 00000000..d9b3b837 --- /dev/null +++ b/src/test/scala/chiselTests/PopCount.scala @@ -0,0 +1,27 @@ +// See LICENSE for license details. + +package chiselTests + +import chisel3._ +import chisel3.util.PopCount +import org.scalatest._ +import org.scalatest.prop._ +import chisel3.testers.BasicTester + +class PopCountTester(n: Int) extends BasicTester { + val x = RegInit(0.U(n.W)) + x := x + 1.U + when (RegNext(x === ~0.U(n.W))) { stop() } + + val result = PopCount(x.asBools) + val expected = x.asBools.foldLeft(0.U)(_ +& _) + assert(result === expected) + + require(result.getWidth == BigInt(n).bitLength) +} + +class PopCountSpec extends ChiselPropSpec { + property("Mul lookup table should return the correct result") { + forAll(smallPosInts) { (n: Int) => assertTesterPasses { new PopCountTester(n) } } + } +} |
