InferenceNumericFormat constructor
- required InferenceElementKind elementKind,
- required int elementWidth,
- required int productWidth,
- required int accumulatorWidth,
- required bool signed,
- InferenceScaleKind scaleKind = InferenceScaleKind.none,
- int scaleWidth = 0,
- int groupSize = 1,
- int? integerWidth,
- int? fractionWidth,
- int? exponentWidth,
- int? mantissaWidth,
- NumericRoundingMode roundingMode = NumericRoundingMode.nearestEven,
- 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.');
}
}
}