reductionTreeRecurse method

({int depth, int flopDepth, Logic value}) reductionTreeRecurse(
  1. List<Logic> seq
)

Recursively construct the computation tree

Implementation

({Logic value, int depth, int flopDepth}) reductionTreeRecurse(
    List<Logic> seq) {
  if (seq.length < radix) {
    return (value: operation(seq), depth: 0, flopDepth: 0);
  } else {
    final results = <({Logic value, int depth, int flopDepth})>[];
    final segment = seq.length ~/ radix;
    var pos = 0;
    for (var i = 0; i < radix; i++) {
      final c = reductionTreeRecurse(seq
          .getRange(pos, (i < radix - 1) ? pos + segment : seq.length)
          .toList());
      results.add(c);
      pos += segment;
    }
    final flopDepth = results.map((c) => c.flopDepth).reduce(max);
    final treeDepth = results.map((c) => c.depth).reduce(max);

    final alignedResults = results
        .map((c) => localFlop(c.value, doFlop: c.flopDepth < flopDepth));

    final depthFlop = (depthToFlop != null) &&
        (treeDepth > 0) & (treeDepth % depthToFlop! == 0);
    final resultsFlop =
        alignedResults.map((r) => localFlop(r, doFlop: depthFlop));

    final alignWidth = results.map((c) => c.value.width).reduce(max);
    final resultsExtend = resultsFlop.map((r) =>
        signExtend ? r.signExtend(alignWidth) : r.zeroExtend(alignWidth));

    final computed = operation(resultsExtend.toList(),
        name: 'reduce_d${(treeDepth + 1) + flopDepth * (depthToFlop ?? 0)}');
    return (
      value: computed,
      depth: depthFlop ? 0 : treeDepth + 1,
      flopDepth: flopDepth + (depthFlop ? 1 : 0)
    );
  }
}