operator / method

FixedPointValue operator /(
  1. FixedPointValue other
)

Division operation that returns a FixedPointValue. The result is signed if one of the operands is signed. The result integer width is the sum of dividend integer width and divisor fraction width. The result fraction width is the sum of dividend fraction width and divisor integer width.

Implementation

FixedPointValue operator /(FixedPointValue other) {
  if (!value.isValid | !other.value.isValid) {
    throw RohdHclException('Inputs must be valid.');
  }
  final s = signed | other.signed;
  // extend integer width for max negative number
  final m1 = s ? m + 1 : m;
  final m2 = s ? other.m + 1 : other.m;
  final mr = m1 + other.n;
  final nr = n + m2;
  final tr = mr + nr;
  var val1 = expandWidth(sign: s, m: m1, n: tr - m1);
  var val2 = other.expandWidth(sign: s, m: tr - other.n);
  // Convert to positive as needed
  if (s) {
    if (val1[-1] == LogicValue.one) {
      val1 = ~(val1 - 1);
    }
    if (val2[-1] == LogicValue.one) {
      val2 = ~(val2 - 1);
    }
  }
  var val = val1 / val2;
  // Convert to negative as needed
  if (isNegative() != other.isNegative()) {
    val = (~val) + 1;
  }
  return FixedPointValue(value: val, signed: s, m: mr, n: nr);
}