From: Robert Griesemer Date: Fri, 8 Jun 2012 20:00:49 +0000 (-0700) Subject: math/big: added nat.trailingZeroBits, simplified some code X-Git-Tag: go1.1rc2~2950 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=014d036d84494f6613174ebe1bf118469a216b52;p=gostls13.git math/big: added nat.trailingZeroBits, simplified some code Will simplify implementation of binaryGCD. R=rsc, cswenson CC=golang-dev https://golang.org/cl/6299064 --- diff --git a/src/pkg/math/big/nat.go b/src/pkg/math/big/nat.go index eaa6ff0666..10026c8b16 100644 --- a/src/pkg/math/big/nat.go +++ b/src/pkg/math/big/nat.go @@ -740,7 +740,7 @@ func (x nat) string(charset string) string { // convert power of two and non power of two bases separately if b == b&-b { // shift is base-b digit size in bits - shift := uint(trailingZeroBits(b)) // shift > 0 because b >= 2 + shift := trailingZeroBits(b) // shift > 0 because b >= 2 mask := Word(1)<>27]) + return uint(deBruijn32Lookup[((x&-x)*deBruijn32)>>27]) case 64: - return int(deBruijn64Lookup[((x&-x)*(deBruijn64&_M))>>58]) + return uint(deBruijn64Lookup[((x&-x)*(deBruijn64&_M))>>58]) default: panic("Unknown word size") } @@ -1017,6 +1017,20 @@ func trailingZeroBits(x Word) int { return 0 } +// trailingZeroBits returns the number of consecutive least significant zero +// bits of x. +func (x nat) trailingZeroBits() uint { + if len(x) == 0 { + return 0 + } + var i uint + for x[i] == 0 { + i++ + } + // x[i] != 0 + return i*_W + trailingZeroBits(x[i]) +} + // z = x << s func (z nat) shl(x nat, s uint) nat { m := len(x) @@ -1169,29 +1183,6 @@ func (x nat) modW(d Word) (r Word) { return divWVW(q, 0, x, d) } -// powersOfTwoDecompose finds q and k with x = q * 1<