mirror of
https://github.com/golang/go
synced 2024-11-25 08:17:58 -07:00
big: cleanup and removal of redundant functionality
R=rsc CC=golang-dev https://golang.org/cl/1048041
This commit is contained in:
parent
6361f52fc4
commit
26078c395a
@ -211,19 +211,32 @@ func divStep(x1, x0, y Word) (q, r Word) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Number of leading zeros in x.
|
// Length of x in bits.
|
||||||
func leadingZeros(x Word) (n uint) {
|
func bitLen(x Word) (n int) {
|
||||||
if x == 0 {
|
for ; x >= 0x100; x >>= 8 {
|
||||||
return _W
|
n += 8
|
||||||
}
|
}
|
||||||
for x&(1<<(_W-1)) == 0 {
|
for ; x > 0; x >>= 1 {
|
||||||
n++
|
n++
|
||||||
x <<= 1
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// log2 computes the integer binary logarithm of x.
|
||||||
|
// The result is the integer n for which 2^n <= x < 2^(n+1).
|
||||||
|
// If x == 0, the result is -1.
|
||||||
|
func log2(x Word) int {
|
||||||
|
return bitLen(x) - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Number of leading zeros in x.
|
||||||
|
func leadingZeros(x Word) uint {
|
||||||
|
return uint(_W - bitLen(x))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// q = (x1<<_W + x0 - r)/y
|
// q = (x1<<_W + x0 - r)/y
|
||||||
func divWW_g(x1, x0, y Word) (q, r Word) {
|
func divWW_g(x1, x0, y Word) (q, r Word) {
|
||||||
if x1 == 0 {
|
if x1 == 0 {
|
||||||
|
@ -165,9 +165,8 @@ func (z nat) sub(x, y nat) nat {
|
|||||||
if c != 0 {
|
if c != 0 {
|
||||||
panic("underflow")
|
panic("underflow")
|
||||||
}
|
}
|
||||||
z = z.norm()
|
|
||||||
|
|
||||||
return z
|
return z.norm()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -495,7 +494,7 @@ func (z nat) divW(x nat, y Word) (q nat, r Word) {
|
|||||||
q = z.set(x) // result is x
|
q = z.set(x) // result is x
|
||||||
return
|
return
|
||||||
case m == 0:
|
case m == 0:
|
||||||
q = z.set(nil) // result is 0
|
q = z.make(0) // result is 0
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// m > 0
|
// m > 0
|
||||||
@ -553,10 +552,10 @@ func (z nat) divLarge(z2, uIn, v nat) (q, r nat) {
|
|||||||
q = z.make(m + 1)
|
q = z.make(m + 1)
|
||||||
|
|
||||||
// D1.
|
// D1.
|
||||||
shift := uint(leadingZeroBits(v[n-1]))
|
shift := leadingZeros(v[n-1])
|
||||||
v.shiftLeftDeprecated(v, shift)
|
v.shiftLeftDeprecated(v, shift)
|
||||||
u.shiftLeftDeprecated(uIn, shift)
|
u.shiftLeftDeprecated(uIn, shift)
|
||||||
u[len(uIn)] = uIn[len(uIn)-1] >> (_W - uint(shift))
|
u[len(uIn)] = uIn[len(uIn)-1] >> (_W - shift)
|
||||||
|
|
||||||
// D2.
|
// D2.
|
||||||
for j := m; j >= 0; j-- {
|
for j := m; j >= 0; j-- {
|
||||||
@ -605,26 +604,12 @@ func (z nat) divLarge(z2, uIn, v nat) (q, r nat) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// log2 computes the integer binary logarithm of x.
|
// Length of x in bits. x must be normalized.
|
||||||
// The result is the integer n for which 2^n <= x < 2^(n+1).
|
func (x nat) bitLen() int {
|
||||||
// If x == 0, the result is -1.
|
|
||||||
func log2(x Word) int {
|
|
||||||
n := -1
|
|
||||||
for ; x > 0; x >>= 1 {
|
|
||||||
n++
|
|
||||||
}
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// log2 computes the integer binary logarithm of x.
|
|
||||||
// The result is the integer n for which 2^n <= x < 2^(n+1).
|
|
||||||
// If x == 0, the result is -1.
|
|
||||||
func (x nat) log2() int {
|
|
||||||
if i := len(x) - 1; i >= 0 {
|
if i := len(x) - 1; i >= 0 {
|
||||||
return i*_W + log2(x[i])
|
return i*_W + bitLen(x[i])
|
||||||
}
|
}
|
||||||
return -1
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -703,7 +688,7 @@ func (x nat) string(base int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// allocate buffer for conversion
|
// allocate buffer for conversion
|
||||||
i := (x.log2()+1)/log2(Word(base)) + 1 // +1: round up
|
i := x.bitLen()/log2(Word(base)) + 1 // +1: round up
|
||||||
s := make([]byte, i)
|
s := make([]byte, i)
|
||||||
|
|
||||||
// don't destroy x
|
// don't destroy x
|
||||||
@ -721,24 +706,6 @@ func (x nat) string(base int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// leadingZeroBits returns the number of leading zero bits in x.
|
|
||||||
func leadingZeroBits(x Word) int {
|
|
||||||
c := 0
|
|
||||||
if x < 1<<(_W/2) {
|
|
||||||
x <<= _W / 2
|
|
||||||
c = _W / 2
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; x != 0; i++ {
|
|
||||||
if x&(1<<(_W-1)) != 0 {
|
|
||||||
return i + c
|
|
||||||
}
|
|
||||||
x <<= 1
|
|
||||||
}
|
|
||||||
|
|
||||||
return _W
|
|
||||||
}
|
|
||||||
|
|
||||||
const deBruijn32 = 0x077CB531
|
const deBruijn32 = 0x077CB531
|
||||||
|
|
||||||
var deBruijn32Lookup = []byte{
|
var deBruijn32Lookup = []byte{
|
||||||
@ -997,16 +964,6 @@ func (z nat) expNN(x, y, m nat) nat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// len returns the bit length of z.
|
|
||||||
func (z nat) len() int {
|
|
||||||
if len(z) == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return (len(z)-1)*_W + (_W - leadingZeroBits(z[len(z)-1]))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// probablyPrime performs reps Miller-Rabin tests to check whether n is prime.
|
// probablyPrime performs reps Miller-Rabin tests to check whether n is prime.
|
||||||
// If it returns true, n is prime with probability 1 - 1/4^reps.
|
// If it returns true, n is prime with probability 1 - 1/4^reps.
|
||||||
// If it returns false, n is not prime.
|
// If it returns false, n is not prime.
|
||||||
@ -1063,7 +1020,7 @@ func (n nat) probablyPrime(reps int) bool {
|
|||||||
rand := rand.New(rand.NewSource(int64(n[0])))
|
rand := rand.New(rand.NewSource(int64(n[0])))
|
||||||
|
|
||||||
var x, y, quotient nat
|
var x, y, quotient nat
|
||||||
nm3Len := nm3.len()
|
nm3Len := nm3.bitLen()
|
||||||
|
|
||||||
NextRandom:
|
NextRandom:
|
||||||
for i := 0; i < reps; i++ {
|
for i := 0; i < reps; i++ {
|
||||||
|
@ -209,11 +209,11 @@ func TestString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestLeadingZeroBits(t *testing.T) {
|
func TestLeadingZeros(t *testing.T) {
|
||||||
var x Word = 1 << (_W - 1)
|
var x Word = _B >> 1
|
||||||
for i := 0; i <= _W; i++ {
|
for i := 0; i <= _W; i++ {
|
||||||
if leadingZeroBits(x) != i {
|
if int(leadingZeros(x)) != i {
|
||||||
t.Errorf("failed at %x: got %d want %d", x, leadingZeroBits(x), i)
|
t.Errorf("failed at %x: got %d want %d", x, leadingZeros(x), i)
|
||||||
}
|
}
|
||||||
x >>= 1
|
x >>= 1
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user