math/big: reduce x1,x2 via subtraction

This avoids a mulWW to recalculate the same thing.

Also remove prevRhat, as we can detect overflow by comparision
of rhat against vn1.

Change-Id: I35f9c4fc96ccc271d4c68f41b744bd71b1756585
Reviewed-on: https://go-review.googlesource.com/c/go/+/467556
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Joel Sing <joel@sing.id.au>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Joel Sing 2023-02-08 18:52:36 +11:00 committed by Gopher Robot
parent 69a99fdcbb
commit 250d0eb6ee

View file

@ -676,16 +676,19 @@ func (q nat) divBasic(stk *stack, u, v nat) {
ujn2 := u[j+n-2]
for greaterThan(x1, x2, rhat, ujn2) { // x1x2 > r̂ u[j+n-2]
qhat--
prevRhat := rhat
rhat += vn1
// If r̂ overflows, then
// r̂ u[j+n-2]v[n-1] is now definitely > x1 x2.
if rhat < prevRhat {
if rhat < vn1 {
break
}
// TODO(rsc): No need for a full mulWW.
// x2 += vn2; if x2 overflows, x1++
x1, x2 = mulWW(qhat, vn2)
// Maintain (x1, x2) = qhat * vn2.
// Since we did qhat-- we need to do (x1, x2) -= vn2.
if vn2 > x2 {
x1--
}
x2 -= vn2
}
}