1
0
mirror of https://github.com/golang/go synced 2024-11-22 09:34:54 -07:00

math/big: optimize common case of Int.Bit(0)

R=golang-dev, dsymonds, rsc
CC=golang-dev
https://golang.org/cl/6306069
This commit is contained in:
Robert Griesemer 2012-06-12 09:36:35 -07:00
parent ea3c3bb3a8
commit 008c62b2cd

View File

@ -697,6 +697,13 @@ func (z *Int) Rsh(x *Int, n uint) *Int {
// Bit returns the value of the i'th bit of x. That is, it // Bit returns the value of the i'th bit of x. That is, it
// returns (x>>i)&1. The bit index i must be >= 0. // returns (x>>i)&1. The bit index i must be >= 0.
func (x *Int) Bit(i int) uint { func (x *Int) Bit(i int) uint {
if i == 0 {
// optimization for common case: odd/even test of x
if len(x.abs) > 0 {
return uint(x.abs[0] & 1) // bit 0 is same for -x
}
return 0
}
if i < 0 { if i < 0 {
panic("negative bit index") panic("negative bit index")
} }