encode method

RadixEncode encode(
  1. Logic multiplierSlice,
  2. int row
)

Encode a multiplier slice into the Booth encoded value

Implementation

RadixEncode encode(Logic multiplierSlice, int row) {
  if (multiplierSlice.width != log2Ceil(radix) + 1) {
    throw RohdHclException('multiplier slice width ${multiplierSlice.width}'
        'must be same length as log(radix)+1=${log2Ceil(radix) + 1}');
  }
  final width = log2Ceil(radix) + 1;
  final inputXor = Logic(width: width);
  inputXor <=
      (multiplierSlice ^ (multiplierSlice >>> 1))
          .slice(multiplierSlice.width - 1, 0)
          .named('${multiplierSlice.name}_xor', naming: Naming.mergeable);

  final multiples = <Logic>[];
  for (var i = 2; i < radix + 1; i += 2) {
    final variantA = LogicValue.ofInt(i - 1, width);
    final xorA = variantA ^ (variantA >>> 1);
    final variantB = LogicValue.ofInt(i, width);
    final xorB = variantB ^ (variantB >>> 1);
    // Multiples don't agree on a bit position so we will skip that position
    final multiplesDisagree = xorA ^ xorB;
    // Where multiples agree, we need the sense or direction (1 or 0)
    final senseMultiples = xorA & xorB;

    multiples.add([
      for (var j = 0; j < width - 1; j++)
        if (multiplesDisagree[j].isZero)
          if (senseMultiples[j].isZero) ~inputXor[j] else inputXor[j]
    ].swizzle().and().named('multiple${i}_of_${multiplierSlice.name}',
        naming: Naming.mergeable));
  }

  final multiplesR = multiples
      .rswizzle()
      .named('multiples_reversed_r$row', naming: Naming.mergeable);

  return RadixEncode._(multiplesR,
      multiplesR.or() & multiplierSlice[multiplierSlice.width - 1], row);
}