]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: simplify divBasic ujn assignment
authorJoel Sing <joel@sing.id.au>
Tue, 7 Feb 2023 12:15:07 +0000 (23:15 +1100)
committerGopher Robot <gobot@golang.org>
Mon, 16 Sep 2024 17:33:18 +0000 (17:33 +0000)
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 <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: Robert Griesemer <gri@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/math/big/natdiv.go

index 96a41c0ace0d96f49da8128d4e64d8dae15142d3..084ac726587fe3edd413dc218ff4f69d98db3244 100644 (file)
@@ -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 {