expandWidth method

LogicValue expandWidth({
  1. required bool sign,
  2. int m = 0,
  3. int n = 0,
})

Expands the bit width of integer and fractional parts.

Implementation

LogicValue expandWidth({required bool sign, int m = 0, int n = 0}) {
  if ((m < 0) | (n < 0)) {
    throw RohdHclException('Input width must be non-negative.');
  }
  if ((m > 0) & (m < this.m)) {
    throw RohdHclException('Integer width is larger than input.');
  }
  if ((n > 0) & (n < this.n)) {
    throw RohdHclException('Fraction width is larger than input.');
  }
  var newValue = value;
  if (m >= this.m) {
    if (signed) {
      newValue = newValue.signExtend(newValue.width + m - this.m);
    } else {
      newValue = newValue.zeroExtend(newValue.width + m - this.m);
      if (sign) {
        newValue = newValue.zeroExtend(newValue.width + 1);
      }
    }
  }
  if (n > this.n) {
    newValue =
        newValue.reversed.zeroExtend(newValue.width + n - this.n).reversed;
  }
  return newValue;
}