src/main/scala/chipsldpc/RelayBias.scala
Source from the repository checkout used to build these docs. Line numbers link to individual source lines.
1package chipsldpc
2
3import chisel3._
4
5/** Paper-compatible Relay-BP bias arithmetic from Fig. 3 and Table 2. */
6private[chipsldpc] object RelayBias {
7 /** Truncate every magnitude partial product before summing, then restore sign. */
8 def reducedMultiply(value: SInt, beta: UInt, fractionalBits: Int): SInt = {
9 require(fractionalBits >= 0)
10 val valueBits = value.getWidth
11 val workBits = valueBits + beta.getWidth + 1
12 val widened = value.pad(valueBits + 1)
13 val negative = widened < 0.S
14 val magnitudeWide = Mux(
15 negative,
16 (0.S((valueBits + 1).W) -% widened).asUInt,
17 widened.asUInt,
18 )
19 val magnitude = magnitudeWide(valueBits - 1, 0)
20 val terms = (0 until valueBits).map { bit =>
21 val scaled =
22 if (bit >= fractionalBits) beta << (bit - fractionalBits)
23 else beta >> (fractionalBits - bit)
24 Mux(magnitude(bit), scaled.pad(workBits), 0.U(workBits.W)).asSInt
25 }
26 val positive = Arithmetic.sum(terms, workBits)
27 Mux(negative, 0.S(workBits.W) -% positive, positive)
28 }
29
30 def apply(
31 prior: SInt,
32 previous: SInt,
33 beta: UInt,
34 q: Quantization,
35 format: RelayFormat,
36 ): SInt = {
37 require(prior.getWidth == q.accumulatorBits)
38 require(previous.getWidth == q.accumulatorBits)
39 require(beta.getWidth == format.betaBits)
40 val scaledPrior = reducedMultiply(prior, beta, format.fractionalBits)
41 val scaledPrevious = reducedMultiply(previous, beta, format.fractionalBits)
42 val workBits = scaledPrior.getWidth.max(scaledPrevious.getWidth).max(previous.getWidth) + 2
43 val mixed = (scaledPrior.pad(workBits) +% previous.pad(workBits)) -%
44 scaledPrevious.pad(workBits)
45 Arithmetic.clipSigned(mixed, q.accumulatorBits)
46 }
47}