Skip to content

Commit

Permalink
apd: add inline fast-pathis to BigInt.{IsUint64,IsInt64,Uint64,Int64}
Browse files Browse the repository at this point in the history
Not important, but easy enough to do.
  • Loading branch information
nvanbenschoten committed Jan 9, 2022
1 parent a0a9338 commit b0077f8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions bigint.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,18 +571,39 @@ func (z *BigInt) GobDecode(buf []byte) error {

// Int64 calls (big.Int).Int64.
func (z *BigInt) Int64() int64 {
if bits.UintSize == 64 {
if zVal, zNeg, ok := z.innerAsUint(); ok {
zi := int64(zVal)
if zNeg {
zi = -zi
}
return zi
}
}
var tmp1 big.Int
return z.inner(&tmp1).Int64()
}

// IsInt64 calls (big.Int).IsInt64.
func (z *BigInt) IsInt64() bool {
if bits.UintSize == 64 {
if zVal, zNeg, ok := z.innerAsUint(); ok {
// From (big.Int).IsInt64.
zi := int64(zVal)
return zi >= 0 || zNeg && zi == -zi
}
}
var tmp1 big.Int
return z.inner(&tmp1).IsInt64()
}

// IsUint64 calls (big.Int).IsUint64.
func (z *BigInt) IsUint64() bool {
if bits.UintSize == 64 {
if _, zNeg, ok := z.innerAsUint(); ok {
return !zNeg
}
}
var tmp1 big.Int
return z.inner(&tmp1).IsUint64()
}
Expand Down Expand Up @@ -943,6 +964,11 @@ func (z *BigInt) TrailingZeroBits() uint {

// Uint64 calls (big.Int).Uint64.
func (z *BigInt) Uint64() uint64 {
if bits.UintSize == 64 {
if zVal, _, ok := z.innerAsUint(); ok {
return uint64(zVal)
}
}
var tmp1 big.Int
return z.inner(&tmp1).Uint64()
}
Expand Down

0 comments on commit b0077f8

Please sign in to comment.