DynamicNumericNarrowing constructor
- Logic value,
- Logic shift, {
- required int outputWidth,
- bool signed = false,
- NumericRoundingMode roundingMode = NumericRoundingMode.truncate,
- NumericOverflowMode overflowMode = NumericOverflowMode.wrap,
- String name = 'dynamic_numeric_narrowing',
- bool reserveName = false,
- bool reserveDefinitionName = false,
- String? definitionName,
Creates combinational narrowing with an unsigned runtime shift.
Implementation
DynamicNumericNarrowing(
Logic value,
Logic shift, {
required int outputWidth,
bool signed = false,
NumericRoundingMode roundingMode = NumericRoundingMode.truncate,
NumericOverflowMode overflowMode = NumericOverflowMode.wrap,
super.name = 'dynamic_numeric_narrowing',
super.reserveName,
super.reserveDefinitionName,
String? definitionName,
}) : super(
definitionName: definitionName ??
'DynamicNumericNarrowing_I${value.width}_O${outputWidth}_'
'S${signed ? 1 : 0}_R${shift.width}_'
'${roundingMode.name}_${overflowMode.name}',
) {
if (outputWidth <= 0) {
throw RohdHclException(
'Numeric narrowing output width must be positive.');
}
value = addInput('value', value, width: value.width);
shift = addInput('shift', shift, width: shift.width);
final limit = value.width + 1;
final maxShift =
shift.width >= limit.bitLength ? limit : (1 << shift.width) - 1;
final boundedShift = shift.width >= limit.bitLength
? mux(shift.gt(limit), Const(limit, width: shift.width), shift)
: shift;
final extended = signed
? value.signExtend(value.width + maxShift)
: value.zeroExtend(value.width + maxShift);
final aligned =
extended << (Const(maxShift, width: shift.width) - boundedShift);
final narrowed = NumericNarrowing(
aligned,
outputWidth: outputWidth,
signed: signed,
shiftRight: maxShift,
roundingMode: roundingMode,
overflowMode: overflowMode,
name: '${name}_narrow',
);
addOutput('result', width: outputWidth) <= narrowed.result;
addOutput('overflowed') <= narrowed.overflowed;
addOutput('underflowed') <= narrowed.underflowed;
addOutput('inexact') <= narrowed.inexact;
}