From 014d036d84494f6613174ebe1bf118469a216b52 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Fri, 8 Jun 2012 13:00:49 -0700 Subject: [PATCH] math/big: added nat.trailingZeroBits, simplified some code Will simplify implementation of binaryGCD. R=rsc, cswenson CC=golang-dev https://golang.org/cl/6299064 --- src/pkg/math/big/nat.go | 58 ++++++++++++++++-------------------- src/pkg/math/big/nat_test.go | 19 ++++++++---- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/pkg/math/big/nat.go b/src/pkg/math/big/nat.go index eaa6ff0666c..10026c8b161 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<