mulChecked method

Int64 mulChecked(
  1. Int64 other
)

Detects overflow via round-trip: wrap-multiply then divide back out. Avoids ever forming a >64-bit intermediate. Detects overflow via round-trip: wrap-multiply then divide back out. Avoids ever forming a >64-bit intermediate.

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

Implementation

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