InferenceNumericFormat constructor

InferenceNumericFormat({
  1. required InferenceElementKind elementKind,
  2. required int elementWidth,
  3. required int productWidth,
  4. required int accumulatorWidth,
  5. required bool signed,
  6. InferenceScaleKind scaleKind = InferenceScaleKind.none,
  7. int scaleWidth = 0,
  8. int groupSize = 1,
  9. int? integerWidth,
  10. int? fractionWidth,
  11. int? exponentWidth,
  12. int? mantissaWidth,
  13. NumericRoundingMode roundingMode = NumericRoundingMode.nearestEven,
  14. NumericOverflowMode overflowMode = NumericOverflowMode.saturate,
})

Creates a validated numeric format.

Implementation

InferenceNumericFormat({
  required this.elementKind,
  required this.elementWidth,
  required this.productWidth,
  required this.accumulatorWidth,
  required this.signed,
  this.scaleKind = InferenceScaleKind.none,
  this.scaleWidth = 0,
  this.groupSize = 1,
  this.integerWidth,
  this.fractionWidth,
  this.exponentWidth,
  this.mantissaWidth,
  this.roundingMode = NumericRoundingMode.nearestEven,
  this.overflowMode = NumericOverflowMode.saturate,
}) {
  if (elementWidth < 1 ||
      productWidth < 1 ||
      accumulatorWidth < productWidth) {
    throw ArgumentError(
      'Element and product widths must be positive and the accumulator '
      'must be at least as wide as one product.',
    );
  }
  if (scaleKind == InferenceScaleKind.none) {
    if (scaleWidth != 0 || groupSize != 1) {
      throw ArgumentError(
        'An unscaled format must use scaleWidth 0 and groupSize 1.',
      );
    }
  } else if (scaleWidth < 1 || groupSize < 1) {
    throw ArgumentError(
      'A scaled format requires positive scale width and group size.',
    );
  }
  switch (elementKind) {
    case InferenceElementKind.unsignedInteger:
      if (signed) {
        throw ArgumentError('Unsigned integer formats cannot be signed.');
      }
      if (integerWidth != null ||
          fractionWidth != null ||
          exponentWidth != null ||
          mantissaWidth != null) {
        throw ArgumentError(
          'Integer formats do not accept fixed or floating field widths.',
        );
      }
    case InferenceElementKind.signedInteger:
      if (!signed) {
        throw ArgumentError('Signed integer formats must be signed.');
      }
      if (integerWidth != null ||
          fractionWidth != null ||
          exponentWidth != null ||
          mantissaWidth != null) {
        throw ArgumentError(
          'Integer formats do not accept fixed or floating field widths.',
        );
      }
    case InferenceElementKind.fixedPoint:
      if (integerWidth == null ||
          fractionWidth == null ||
          integerWidth! < 0 ||
          fractionWidth! < 0 ||
          elementWidth != integerWidth! + fractionWidth! + (signed ? 1 : 0)) {
        throw ArgumentError(
          'Fixed-point width must equal integer + fraction + sign widths.',
        );
      }
    case InferenceElementKind.floatingPoint:
      if (!signed ||
          exponentWidth == null ||
          mantissaWidth == null ||
          exponentWidth! < 1 ||
          mantissaWidth! < 1 ||
          elementWidth != exponentWidth! + mantissaWidth! + 1) {
        throw ArgumentError(
          'Floating-point width must equal sign + exponent + mantissa.',
        );
      }
    case InferenceElementKind.blockScaled:
      if (scaleKind == InferenceScaleKind.none) {
        throw ArgumentError('Block-scaled elements require scale metadata.');
      }
  }
}