Skip to content

Commit

Permalink
Decimal: slight performance boost to pow2
Browse files Browse the repository at this point in the history
  • Loading branch information
db47h committed May 11, 2020
1 parent b8b6944 commit a4ac945
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions decimal_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ func (z *Decimal) scan(r io.ByteScanner, base int) (f *Decimal, b int, err error
return
}

// pow5 sets z to 5**n and returns z.
// pow2 sets z to 2**n and returns z.
// n must not be negative.
func (z *Decimal) pow2(n uint64) *Decimal {
const m = _DW * 100000 / 30103 // maximum exponent such that 2**m < _BD
if n <= m {
const m = _DWb - 1 // maximum exponent such that 2**m < _BD
if n < _W {
return z.SetUint64(1 << n)
}
// n > m
Expand All @@ -159,6 +159,9 @@ func (z *Decimal) pow2(n uint64) *Decimal {
for n > 0 {
if n&1 != 0 {
z.Mul(z, f)
if n == 1 {
break
}
}
f.Mul(f, f)
n >>= 1
Expand Down

0 comments on commit a4ac945

Please sign in to comment.