divModImpl function

({Uint64 quotient, Uint64 remainder}) divModImpl(
  1. Uint64 a,
  2. Uint64 b
)

Implementation

({Uint64 quotient, Uint64 remainder}) divModImpl(Uint64 a, Uint64 b) {
  final n = _pack(a);
  final d = _pack(b);
  if (d == 0) throw IntegerError.divisionByZero;
  int q, r;
  if (d < 0) {
    if (_ucmp(n, d) < 0) {
      q = 0;
      r = n;
    } else {
      q = 1;
      r = n - d;
    }
  } else if (n >= 0) {
    q = n ~/ d;
    r = n - q * d;
  } else {
    q = ((n >>> 1) ~/ d) * 2;
    r = n - q * d;
    if (_ucmp(r, d) >= 0) {
      q += 1;
      r -= d;
    }
  }
  return (quotient: _unpack(q), remainder: _unpack(r));
}