Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
26078c39
Commit
26078c39
authored
May 01, 2010
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
big: cleanup and removal of redundant functionality
R=rsc CC=golang-dev
https://golang.org/cl/1048041
parent
6361f52f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
63 deletions
+33
-63
arith.go
src/pkg/big/arith.go
+19
-6
nat.go
src/pkg/big/nat.go
+10
-53
nat_test.go
src/pkg/big/nat_test.go
+4
-4
No files found.
src/pkg/big/arith.go
View file @
26078c39
...
...
@@ -211,19 +211,32 @@ func divStep(x1, x0, y Word) (q, r Word) {
}
//
Number of leading zeros in x
.
func
leadingZeros
(
x
Word
)
(
n
u
int
)
{
if
x
==
0
{
return
_W
//
Length of x in bits
.
func
bitLen
(
x
Word
)
(
n
int
)
{
for
;
x
>=
0x100
;
x
>>=
8
{
n
+=
8
}
for
x
&
(
1
<<
(
_W
-
1
))
==
0
{
for
;
x
>
0
;
x
>>=
1
{
n
++
x
<<=
1
}
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
func
divWW_g
(
x1
,
x0
,
y
Word
)
(
q
,
r
Word
)
{
if
x1
==
0
{
...
...
src/pkg/big/nat.go
View file @
26078c39
...
...
@@ -165,9 +165,8 @@ func (z nat) sub(x, y nat) nat {
if
c
!=
0
{
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
return
case
m
==
0
:
q
=
z
.
set
(
nil
)
// result is 0
q
=
z
.
make
(
0
)
// result is 0
return
}
// m > 0
...
...
@@ -553,10 +552,10 @@ func (z nat) divLarge(z2, uIn, v nat) (q, r nat) {
q
=
z
.
make
(
m
+
1
)
// D1.
shift
:=
uint
(
leadingZeroBits
(
v
[
n
-
1
])
)
shift
:=
leadingZeros
(
v
[
n
-
1
]
)
v
.
shiftLeftDeprecated
(
v
,
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.
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.
// 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
{
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
{
// Length of x in bits. x must be normalized.
func
(
x
nat
)
bitLen
()
int
{
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
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
)
// 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
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.
// If it returns true, n is prime with probability 1 - 1/4^reps.
// 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
])))
var
x
,
y
,
quotient
nat
nm3Len
:=
nm3
.
l
en
()
nm3Len
:=
nm3
.
bitL
en
()
NextRandom
:
for
i
:=
0
;
i
<
reps
;
i
++
{
...
...
src/pkg/big/nat_test.go
View file @
26078c39
...
...
@@ -209,11 +209,11 @@ func TestString(t *testing.T) {
}
func
TestLeadingZero
Bit
s
(
t
*
testing
.
T
)
{
var
x
Word
=
1
<<
(
_W
-
1
)
func
TestLeadingZeros
(
t
*
testing
.
T
)
{
var
x
Word
=
_B
>>
1
for
i
:=
0
;
i
<=
_W
;
i
++
{
if
leadingZeroBits
(
x
)
!=
i
{
t
.
Errorf
(
"failed at %x: got %d want %d"
,
x
,
leadingZero
Bit
s
(
x
),
i
)
if
int
(
leadingZeros
(
x
)
)
!=
i
{
t
.
Errorf
(
"failed at %x: got %d want %d"
,
x
,
leadingZeros
(
x
),
i
)
}
x
>>=
1
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment