abs method

LogicValue abs()

Calculates the absolute value, assuming that the number is a two's complement.

Implementation

LogicValue abs() {
  if (width == 0) {
    return this;
  }
  if (!this[-1].isValid) {
    return LogicValue.filled(width, LogicValue.x);
  }
  return this[-1] == LogicValue.one
      ? ~this + LogicValue.ofInt(1, width)
      : this;
}