From: Josh Bleecher Snyder Date: Fri, 10 Mar 2017 23:09:05 +0000 (-0800) Subject: math/big: make nat.setUint64 vet-friendly X-Git-Tag: go1.9beta1~1203 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=2de773d45f202d38c981a433880e867a7b5d0745;p=gostls13.git math/big: make nat.setUint64 vet-friendly nat.setUint64 is nicely generic. By assuming 32- or 64-bit words, however, we can write simpler code, and eliminate some shifts in dead code that vet complains about. Generated code for 64 bit systems is unaltered. Generated code for 32 bit systems is much better. For 386, the routine length drops from 325 bytes of code to 271 bytes of code, with fewer loops. Change-Id: I1bc14c06272dee37a7fcb48d33dd1e621eba945d Reviewed-on: https://go-review.googlesource.com/38070 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot Reviewed-by: Robert Griesemer --- diff --git a/src/cmd/vet/all/whitelist/64bit.txt b/src/cmd/vet/all/whitelist/64bit.txt index b2c8e05b59..39855f7d70 100644 --- a/src/cmd/vet/all/whitelist/64bit.txt +++ b/src/cmd/vet/all/whitelist/64bit.txt @@ -22,7 +22,5 @@ math/big/arith.go: (xi&yi | (xi|yi)&^zi) might be too small for shift of 63 math/big/arith.go: (yi&^xi | (yi|^xi)&zi) might be too small for shift of 63 math/big/arith.go: xi &^ zi might be too small for shift of 63 math/big/arith.go: (zi &^ xi) might be too small for shift of 63 -math/big/nat.go: t too small for shift of 64 -math/big/nat.go: x too small for shift of 64 math/big/nat.go: yi might be too small for shift of 60 math/big/nat.go: yi might be too small for shift of 60 diff --git a/src/math/big/nat.go b/src/math/big/nat.go index 1e6f7ae8a8..67176553b0 100644 --- a/src/math/big/nat.go +++ b/src/math/big/nat.go @@ -68,24 +68,14 @@ func (z nat) setWord(x Word) nat { } func (z nat) setUint64(x uint64) nat { - // single-digit values + // single-word value if w := Word(x); uint64(w) == x { return z.setWord(w) } - - // compute number of words n required to represent x - n := 0 - for t := x; t > 0; t >>= _W { - n++ - } - - // split x into n words - z = z.make(n) - for i := range z { - z[i] = Word(x & _M) - x >>= _W - } - + // 2-word value + z = z.make(2) + z[1] = Word(x >> 32) + z[0] = Word(x) return z }