]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: optimize common case of Int.Bit(0)
authorRobert Griesemer <gri@golang.org>
Tue, 12 Jun 2012 16:36:35 +0000 (09:36 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 12 Jun 2012 16:36:35 +0000 (09:36 -0700)
R=golang-dev, dsymonds, rsc
CC=golang-dev
https://golang.org/cl/6306069

src/pkg/math/big/int.go

index ce308bd24ffb64a599f562bcd36caef3bf8b5bc3..276f56708ab600719746f334c9f80d4739bc9703 100644 (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
 // returns (x>>i)&1. The bit index i must be >= 0.
 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 {
                panic("negative bit index")
        }