function bnpDivRemTo()

in public/amazon-cognito-identity.js [3782:3845]


function bnpDivRemTo(m, q, r) {
	var pm = m.abs();
	if (pm.t <= 0) return;
	var pt = this.abs();
	if (pt.t < pm.t) {
		if (q != null) q.fromInt(0);
		if (r != null) this.copyTo(r);
		return;
	}
	if (r == null) r = nbi();
	var y = nbi(),
	    ts = this.s,
	    ms = m.s;
	var nsh = this.DB - nbits(pm[pm.t - 1]);
	// normalize modulus
	if (nsh > 0) {
		pm.lShiftTo(nsh, y);
		pt.lShiftTo(nsh, r);
	} else {
		pm.copyTo(y);
		pt.copyTo(r);
	}
	var ys = y.t;
	var y0 = y[ys - 1];
	if (y0 == 0) return;
	var yt = y0 * (1 << this.F1) + (ys > 1 ? y[ys - 2] >> this.F2 : 0);
	var d1 = this.FV / yt,
	    d2 = (1 << this.F1) / yt,
	    e = 1 << this.F2;
	var i = r.t,
	    j = i - ys,
	    t = q == null ? nbi() : q;
	y.dlShiftTo(j, t);
	if (r.compareTo(t) >= 0) {
		r[r.t++] = 1;
		r.subTo(t, r);
	}
	BigInteger.ONE.dlShiftTo(ys, t);
	t.subTo(y, y);
	// "negative" y so we can replace sub with am later
	while (y.t < ys) {
		y[y.t++] = 0;
	}while (--j >= 0) {
		// Estimate quotient digit
		var qd = r[--i] == y0 ? this.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
		if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) {
			// Try it out
			y.dlShiftTo(j, t);
			r.subTo(t, r);
			while (r[i] < --qd) {
				r.subTo(t, r);
			}
		}
	}
	if (q != null) {
		r.drShiftTo(ys, q);
		if (ts != ms) BigInteger.ZERO.subTo(q, q);
	}
	r.t = ys;
	r.clamp();
	if (nsh > 0) r.rShiftTo(nsh, r);
	// Denormalize remainder
	if (ts < 0) BigInteger.ZERO.subTo(r, r);
}