mulChecked method

Int32 mulChecked(
  1. Int32 other
)

Detects overflow via round-trip: wrap-multiply then divide back out — same technique every sibling mulChecked uses, BigInt-free.

Int32.min * Int32.minusOne needs an explicit guard: ~/ itself special-cases min ~/ minusOne to wrap back to min (matching real hardware behavior), which would otherwise make the round-trip check below look "clean" even though the true product (2^31) overflows.

Implementation

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