ofScaledBigInt method

FxvType ofScaledBigInt(
  1. BigInt significand,
  2. int exponent, {
  3. FloatingPointRoundingMode roundingMode = FloatingPointRoundingMode.truncate,
})

Constructs a value equal to significand times two to exponent.

The exact dyadic value is rounded once into this fixed-point format.

Implementation

FxvType ofScaledBigInt(BigInt significand, int exponent,
    {FloatingPointRoundingMode roundingMode =
        FloatingPointRoundingMode.truncate}) {
  if (!signed && significand.isNegative) {
    throw RohdHclException('Negative input not allowed with unsigned');
  }

  final shift = exponent + fractionWidth;
  final scaled = shift >= 0
      ? significand << shift
      : roundBigIntByPowerOfTwo(significand, -shift,
          roundingMode: roundingMode);
  final width = integerWidth + fractionWidth + (signed ? 1 : 0);
  if (width < 1) {
    throw RohdHclException('FixedPointValue width must be positive.');
  }
  final minValue = signed ? -(BigInt.one << (width - 1)) : BigInt.zero;
  final maxValue = signed
      ? (BigInt.one << (width - 1)) - BigInt.one
      : (BigInt.one << width) - BigInt.one;
  if (scaled < minValue || scaled > maxValue) {
    throw RohdHclException('Value cannot be represented by FixedPointValue '
        'with integerWidth=$integerWidth, fractionWidth=$fractionWidth, '
        'signed=$signed.');
  }

  return ofLogicValue(LogicValue.ofBigInt(scaled, width));
}