Sum constructor

Sum(
  1. List<SumInterface> interfaces, {
  2. dynamic initialValue = 0,
  3. dynamic maxValue,
  4. dynamic minValue = 0,
  5. int? width,
  6. bool saturates = false,
  7. String name = 'sum',
  8. bool reserveName = false,
  9. bool reserveDefinitionName = false,
  10. String? definitionName,
})

Computes a sum across the provided interfaces.

The width can be either explicitly provided or inferred from other values such as a maxValue, minValue, or initialValue that contain width information (e.g. a LogicValue), or by making it large enough to fit maxValue, or by inspecting widths of interfaces. There must be enough information provided to determine the width.

If no maxValue is provided, one will be inferred by the maximum that can fit inside of the width.

It is expected that maxValue is at least minValue, or else results may be unpredictable.

If saturates is true, then it will saturate at the maxValue and minValue. If false, will wrap around (overflow/underflow) at the maxValue and minValue. The equalsMax, equalsMin, overflowed, and underflowed outputs can be used to determine if the sum is at the maximum, minimum, (would have) overflowed, or (would have) underflowed, respectively. initialValue, maxValue, and minValue can be provided as StaticOrRuntimeControls when a named runtime input or explicit static/runtime configuration is useful.

Implementation

Sum(super.interfaces,
    {dynamic initialValue = 0,
    super.maxValue,
    super.minValue,
    super.width,
    super.saturates,
    super.name = 'sum',
    super.reserveName,
    super.reserveDefinitionName,
    String? definitionName})
    : super(
          definitionName:
              definitionName ?? 'Sum_${interfaces.length}_W$width',
          initialValue: initialValue) {
  addOutput('sum', width: width);
  final configuredInitialValue = SummationBase.configuredValue(initialValue);

  var maxPosMagnitude = SummationBase.biggestVal(width);
  var maxNegMagnitude = BigInt.zero;
  for (final intf in interfaces) {
    final maxMagnitude = intf.fixedAmount != null
        ? intf.amount.value.toBigInt()
        : SummationBase.biggestVal(intf.width);

    if (intf.increments) {
      maxPosMagnitude += maxMagnitude;
    } else {
      maxNegMagnitude += maxMagnitude;
    }
  }

  // also consider that initialValue may be less than min or more than max
  final initialValueMagnitude = configuredInitialValue == null
      ? BigInt.zero
      : configuredInitialValue is Logic
          ? SummationBase.biggestVal(configuredInitialValue.width)
          : LogicValue.ofInferWidth(configuredInitialValue).toBigInt().abs();
  final maxInitialValueMagnitude = initialValueMagnitude;
  maxPosMagnitude += maxInitialValueMagnitude;
  maxNegMagnitude += maxInitialValueMagnitude;

  // calculate the largest number that we could have in intermediate
  final internalWidth = max(
      (maxPosMagnitude + maxNegMagnitude + BigInt.one).bitLength, width + 1);

  final initialValueLogicExt = initialValueLogic.zeroExtend(internalWidth);
  final minValueLogicExt =
      minValueLogic.zeroExtend(internalWidth).named('minValueExt');
  final maxValueLogicExt =
      maxValueLogic.zeroExtend(internalWidth).named('maxValueExt');

  final possibleRange =
      _possibleSumRange(configuredInitialValue, interfaces, width);
  canOverflow = constantMaxValue == null ||
      constantMaxValue!.isNegative ||
      possibleRange.maximum > constantMaxValue!;
  canUnderflow = constantMinValue == null ||
      constantMinValue!.isNegative ||
      possibleRange.minimum < constantMinValue!;

  // lazy range so that it's not generated if not necessary
  late final range = Logic(name: 'range', width: internalWidth)
    ..gets(maxValueLogicExt - minValueLogicExt + 1);

  final zeroPoint = Logic(name: 'zeroPoint', width: internalWidth)
    ..gets(Const(maxNegMagnitude, width: internalWidth));

  final upperSaturation = Logic(name: 'upperSaturation', width: internalWidth)
    ..gets(maxValueLogicExt + zeroPoint);
  final lowerSaturation = Logic(name: 'lowerSaturation', width: internalWidth)
    ..gets(minValueLogicExt + zeroPoint);

  final internalValue = Logic(name: 'internalValue', width: internalWidth);
  sum <=
      (internalValue - zeroPoint)
          .named('internalValueOverZeroPoint')
          .getRange(0, width);

  final preAdjustmentValue =
      Logic(name: 'preAdjustmentValue', width: internalWidth);

  var rawValue = initialValueLogicExt + zeroPoint;
  for (final (index, intf) in interfaces.indexed) {
    final amount = intf.amount.zeroExtend(internalWidth);
    final adjusted = intf.increments ? rawValue + amount : rawValue - amount;
    if (intf.hasEnable) {
      final next = Logic(name: 'adjusted_$index', width: internalWidth);
      Combinational([
        next < rawValue,
        If(intf.enable!, then: [next < adjusted]),
      ]);
      rawValue = next;
    } else {
      rawValue = adjusted;
    }
  }
  preAdjustmentValue <= rawValue;

  overflowed <=
      (canOverflow ? preAdjustmentValue.gt(upperSaturation) : Const(0));
  underflowed <=
      (canUnderflow ? preAdjustmentValue.lt(lowerSaturation) : Const(0));

  final overflowUnderflowAdjustments = <Iff>[];
  if (canOverflow) {
    overflowUnderflowAdjustments.add(Iff.s(
        overflowed,
        internalValue <
            (saturates
                ? upperSaturation
                : ((preAdjustmentValue - upperSaturation - 1) % range +
                    lowerSaturation))));
  }
  if (canUnderflow) {
    final underflowAdjustment = internalValue <
        (saturates
            ? lowerSaturation
            : (upperSaturation -
                ((lowerSaturation - preAdjustmentValue - 1) % range)));
    overflowUnderflowAdjustments.add(canOverflow
        ? ElseIf.s(underflowed, underflowAdjustment)
        : Iff.s(underflowed, underflowAdjustment));
  }
  Combinational([
    internalValue < preAdjustmentValue,
    if (overflowUnderflowAdjustments.isNotEmpty)
      If.block(overflowUnderflowAdjustments),
  ]);

  equalsMax <= internalValue.eq(upperSaturation);
  equalsMin <= internalValue.eq(lowerSaturation);
}