src/test/scala/chipsldpc/sort/NeighbourSorterSpec.scala

Source from the repository checkout used to build these docs. Line numbers link to individual source lines.

  1package chipsldpc.sort
  2
  3import chisel3._
  4import chisel3.simulator.scalatest.ChiselSim
  5import chipsldpc.RelayDefaults
  6import org.scalatest.freespec.AnyFreeSpec
  7import scala.util.Random
  8
  9final class NeighbourSorterSpec extends AnyFreeSpec with ChiselSim {
 10  private val soft = Vector(-16, 2, -1, -4, 0, -3, 8, -2)
 11
 12  private def expected(
 13      values: Vector[Int],
 14      config: NeighbourSorterConfig,
 15      scope: Option[Set[Int]],
 16  ): Vector[(Int, Int)] =
 17    values.zipWithIndex
 18      .collect {
 19        case (value, index)
 20            if (config.order match {
 21              case MagnitudeAscending => value.abs < config.threshold
 22              case SignedAscending => value < config.threshold
 23            }) && scope.forall(_.contains(index)) =>
 24          value -> index
 25      }
 26      .sortBy { case (value, index) =>
 27        (if (config.order == MagnitudeAscending) value.abs else value, index)
 28      }
 29
 30  private def run(
 31      dut: NeighbourSorter,
 32      config: NeighbourSorterConfig,
 33      scope: Option[Set[Int]],
 34      values: Vector[Int] = soft,
 35      stalls: Int = 0,
 36  ): Int = {
 37    require(values.size == config.size)
 38    val want = expected(values, config, scope)
 39    dut.io.in.valid.poke(false.B)
 40    dut.io.out.ready.poke(true.B)
 41    values.zipWithIndex.foreach { case (value, index) =>
 42      dut.io.in.bits.soft(index).poke(value.S)
 43      dut.io.in.bits.inScope(index).poke(scope.exists(_.contains(index)).B)
 44    }
 45    dut.io.in.bits.inScopeValid.poke(scope.nonEmpty.B)
 46    dut.io.in.valid.poke(true.B)
 47    dut.io.in.ready.expect(true.B)
 48    dut.clock.step()
 49    dut.io.in.valid.poke(false.B)
 50
 51    var cycles = 1
 52    for (_ <- 1 until config.size) {
 53      dut.io.out.valid.expect(false.B)
 54      dut.io.done.expect(false.B)
 55      dut.clock.step()
 56      cycles += 1
 57    }
 58
 59    if (want.isEmpty) {
 60      dut.io.done.expect(true.B)
 61    } else {
 62      dut.io.out.ready.poke(false.B)
 63      for (_ <- 0 until stalls) {
 64        dut.io.out.valid.expect(true.B)
 65        dut.io.out.bits.soft.expect(want.head._1.S)
 66        dut.io.out.bits.index.expect(want.head._2.U)
 67        dut.clock.step()
 68        cycles += 1
 69      }
 70      dut.io.out.ready.poke(true.B)
 71      want.zipWithIndex.foreach { case ((value, index), position) =>
 72        dut.io.out.valid.expect(true.B)
 73        dut.io.out.bits.soft.expect(value.S)
 74        dut.io.out.bits.index.expect(index.U)
 75        dut.io.out.bits.last.expect((position == want.size - 1).B)
 76        dut.clock.step()
 77        cycles += 1
 78      }
 79      dut.io.done.expect(true.B)
 80    }
 81
 82    dut.io.in.ready.expect(true.B)
 83    dut.clock.step()
 84    dut.io.done.expect(false.B)
 85    assert(cycles == config.size + want.size + stalls)
 86    cycles
 87  }
 88
 89  "the threshold uses BP-format signed soft values and scope defaults to all" in {
 90    val config = NeighbourSorterConfig(soft.size, RelayDefaults.q.accumulatorBits, threshold = 4)
 91    simulate(new NeighbourSorter(config)) { dut =>
 92      assert(run(dut, config, scope = None) == config.size + 5)
 93      assert(run(dut, config, scope = Some(Set(2, 5, 7)), stalls = 2) == config.size + 3 + 2)
 94      assert(run(dut, config, scope = Some(Set.empty)) == config.size)
 95
 96      val random = new Random(0x5eedL)
 97      for (_ <- 0 until 32) {
 98        val values = Vector.fill(config.size)(random.nextInt(32) - 16)
 99        val scope = Some((0 until config.size).filter(_ => random.nextBoolean()).toSet)
100        run(dut, config, scope, values)
101      }
102    }
103  }
104
105  "sorting all N values takes exactly 2N active cycles" in {
106    val config = NeighbourSorterConfig(soft.size, RelayDefaults.q.accumulatorBits, threshold = 17)
107    simulate(new NeighbourSorter(config)) { dut =>
108      assert(run(dut, config, scope = None) == 2 * config.size)
109    }
110  }
111
112  "signed mode implements the strict Maurya threshold and stable LLR order" in {
113    val values = Vector(-16, 2, -1, 1, 0, -3, 8, -1)
114    val config = NeighbourSorterConfig(
115      values.size,
116      RelayDefaults.q.accumulatorBits,
117      threshold = 1,
118      order = SignedAscending,
119    )
120    simulate(new NeighbourSorter(config)) { dut =>
121      assert(run(dut, config, scope = None, values = values) == config.size + 5)
122      assert(run(dut, config, scope = Some(Set(2, 4, 7)), values = values) == config.size + 3)
123    }
124  }
125
126  "an odd N uses one unused slot without changing the 2N contract" in {
127    val values = Vector(-16, 4, -1, 0, 7)
128    val config = NeighbourSorterConfig(values.size, RelayDefaults.q.accumulatorBits, threshold = 17)
129    simulate(new NeighbourSorter(config)) { dut =>
130      assert(run(dut, config, scope = None, values = values) == 2 * config.size)
131    }
132
133    val one = Vector(-2)
134    val oneConfig = NeighbourSorterConfig(1, RelayDefaults.q.accumulatorBits, threshold = 17)
135    simulate(new NeighbourSorter(oneConfig)) { dut =>
136      assert(run(dut, oneConfig, scope = None, values = one) == 2)
137      assert(run(dut, oneConfig, scope = Some(Set.empty), values = one) == 1)
138    }
139  }
140}