mulChecked method

Int128 mulChecked(
  1. Int128 other
)

Detects overflow via round-trip: wrap-multiply then divide back out. Detects overflow via round-trip: wrap-multiply then divide back out.

Int128.min * Int128.minusOne needs an explicit guard — see the matching comment on Int32.mulChecked for why.

Implementation

Int128 mulChecked(Int128 other) {
  if ((this == Int128.min && other == Int128.minusOne) ||
      (this == Int128.minusOne && other == Int128.min)) {
    throw IntegerError.overflow;
  }
  if (isZero || other.isZero) return Int128.zero;
  final r = this * other;
  if (r ~/ other != this) throw IntegerError.overflow;
  return r;
}