diff options
| author | Jack Koenig | 2019-09-16 15:54:31 -0700 |
|---|---|---|
| committer | Jim Lawson | 2019-09-16 15:54:31 -0700 |
| commit | e939181aebb5a0131562609a5782e1f1df88699d (patch) | |
| tree | cb01c1e3438ae123061f2147600fd961ce509f1e /src | |
| parent | a83647a91326bcd2ac0e3c664056b5ced212cc67 (diff) | |
Da steve101 tree reduce (#485)
* Add a tree reduce function to Vec
* Change function names of reduce operation function in Vec
* Change reference to single layer operation in Vec.reduce
* Commint name change for pair macro
* Remove pair, call not necessary and can just be used from grouped(2) and map
* Changed to reduceTree, added default identity function for single reduce.
* Change style of Vec.reduceTree and tests to chisel3 and canonical Scala style
* Cleanup Vec initialization, implicitCompileOptions
Diffstat (limited to 'src')
| -rw-r--r-- | src/test/scala/chiselTests/AdderTree.scala | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/test/scala/chiselTests/AdderTree.scala b/src/test/scala/chiselTests/AdderTree.scala new file mode 100644 index 00000000..4e7ad1a6 --- /dev/null +++ b/src/test/scala/chiselTests/AdderTree.scala @@ -0,0 +1,33 @@ +package chiselTests + +import chisel3._ +import chisel3.testers.BasicTester + +class AdderTree[T <: Bits with Num[T]](genType: T, vecSize: Int) extends Module { + val io = IO(new Bundle { + val numIn = Input(Vec(vecSize, genType)) + val numOut = Output(genType) + }) + io.numOut := io.numIn.reduceTree((a : T, b : T) => (a + b)) +} + +class AdderTreeTester(bitWidth: Int, numsToAdd: List[Int]) extends BasicTester { + val genType = UInt(bitWidth.W) + val dut = Module(new AdderTree(genType, numsToAdd.size)) + dut.io.numIn := VecInit(numsToAdd.map(x => x.asUInt(bitWidth.W))) + val sumCorrect = dut.io.numOut === (numsToAdd.reduce(_+_) % (1 << bitWidth)).asUInt(bitWidth.W) + assert(sumCorrect) + stop() +} + +class AdderTreeSpec extends ChiselPropSpec { + property("All numbers should be added correctly by an Adder Tree") { + forAll(safeUIntN(20)) { + case (w: Int, v: List[Int]) => { + whenever(v.size > 0 && w > 0) { + assertTesterPasses { new AdderTreeTester(w, v.map(x => math.abs(x) % ( 1 << w )).toList) } + } + } + } + } +} |
