From 5367d696f7a610b0e3826914d2633888e66b9f0b Mon Sep 17 00:00:00 2001 From: Joel Sing Date: Tue, 7 Feb 2023 23:15:07 +1100 Subject: [PATCH] math/big: simplify divBasic ujn assignment Rather than conditionally assigning ujn, initialise ujn above the loop to invent the leading 0 for u, then unconditionally load ujn at the bottom of the loop. This code operates on the basis that n >= 2, hence j+n-1 is always greater than zero. Change-Id: I1272ef30c787ed8707ae8421af2adcccc776d389 Reviewed-on: https://go-review.googlesource.com/c/go/+/467555 Auto-Submit: Robert Griesemer LUCI-TryBot-Result: Go LUCI Commit-Queue: Robert Griesemer Reviewed-by: Dmitri Shuralyov Reviewed-by: Robert Griesemer --- src/math/big/natdiv.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/math/big/natdiv.go b/src/math/big/natdiv.go index 96a41c0ace..084ac72658 100644 --- a/src/math/big/natdiv.go +++ b/src/math/big/natdiv.go @@ -642,15 +642,13 @@ func (q nat) divBasic(u, v nat) { vn1 := v[n-1] rec := reciprocalWord(vn1) + // Invent a leading 0 for u, for the first iteration. + ujn := Word(0) + // Compute each digit of quotient. for j := m; j >= 0; j-- { // Compute the 2-by-1 guess q̂. - // The first iteration must invent a leading 0 for u. qhat := Word(_M) - var ujn Word - if j+n < len(u) { - ujn = u[j+n] - } // ujn ≤ vn1, or else q̂ would be more than one digit. // For ujn == vn1, we set q̂ to the max digit M above. @@ -699,6 +697,8 @@ func (q nat) divBasic(u, v nat) { qhat-- } + ujn = u[j+n-1] + // Save quotient digit. // Caller may know the top digit is zero and not leave room for it. if j == m && m == len(q) && qhat == 0 {