]> Cypherpunks repositories - gostls13.git/commitdiff
big: eliminate redundant array lookups
authorEvan Shaw <chickencha@gmail.com>
Wed, 21 Apr 2010 04:41:58 +0000 (21:41 -0700)
committerRuss Cox <rsc@golang.org>
Wed, 21 Apr 2010 04:41:58 +0000 (21:41 -0700)
This gives about a 6% performance improvement to pidigits.
Thanks to Russ for the suggestion.

R=rsc, gri
CC=golang-dev
https://golang.org/cl/957041

src/pkg/big/nat.go

index 456952aa885b86a3b076a3b1769b78616a4ec887..6c7e6e722d9afb8d404c63a3858dc0874df65343 100644 (file)
@@ -544,13 +544,16 @@ func shiftLeft(dst, src []Word, n uint) {
        }
 
        ñ := _W - n
+       x := src[len(src)-1]
        if len(dst) > len(src) {
-               dst[len(src)] |= src[len(src)-1] >> ñ
+               dst[len(src)] = x >> ñ
        }
        for i := len(src) - 1; i >= 1; i-- {
-               dst[i] = src[i]<<n | src[i-1]>>ñ
+               y := src[i-1]
+               dst[i] = x<<n | y>>ñ
+               x = y
        }
-       dst[0] = src[0] << n
+       dst[0] = x << n
 }
 
 
@@ -560,10 +563,13 @@ func shiftRight(dst, src []Word, n uint) {
        }
 
        ñ := _W - n
+       x := src[0]
        for i := 0; i < len(src)-1; i++ {
-               dst[i] = src[i]>>n | src[i+1]<<ñ
+               y := src[i+1]
+               dst[i] = x>>n | y<<ñ
+               x = y
        }
-       dst[len(src)-1] = src[len(src)-1] >> n
+       dst[len(src)-1] = x >> n
 }