Skip to content

Commit

Permalink
Merge pull request #112 from nvanbenschoten/nvanbenschoten/inlineGoError
Browse files Browse the repository at this point in the history
apd: avoid function calls on hot paths
  • Loading branch information
nvanbenschoten authored Jan 29, 2022
2 parents a3aa8bd + c932774 commit 86b4932
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions bigint.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func NewBigInt(x int64) *BigInt {
var negSentinel = new(big.Int)

// isInline returns whether the BigInt stores its value in its _inline array.
//gcassert:inline
func (z *BigInt) isInline() bool {
return z._inner == nil || z._inner == negSentinel
}
Expand Down
7 changes: 5 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func (c *Context) WithPrecision(p uint32) *Context {
// goError converts flags into an error based on c.Traps.
//gcassert:inline
func (c *Context) goError(flags Condition) (Condition, error) {
if flags == 0 {
return flags, nil
}
return flags.GoError(c.Traps)
}

Expand Down Expand Up @@ -1217,8 +1220,8 @@ func (c *Context) quantize(d, v *Decimal, exp int32) Condition {
// target eliminates this problem.

d.Exponent = -diff
// Avoid the c.Precision == 0 check.
res = nc.Rounding.Round(nc, d, d)
// Round even if nc.Precision == 0.
res = nc.Rounding.Round(nc, d, d, false /* disableIfPrecisionZero */)
// Adjust for 0.9 -> 1.0 rollover.
if d.Exponent > 0 {
d.Coeff.Mul(&d.Coeff, bigTen)
Expand Down
15 changes: 8 additions & 7 deletions round.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ func (c *Context) Round(d, x *Decimal) (Condition, error) {
return c.goError(c.round(d, x))
}

//gcassert:inline
func (c *Context) round(d, x *Decimal) Condition {
if c.Precision == 0 {
d.Set(x)
return d.setExponent(c, unknownNumDigits, 0, int64(d.Exponent))
}
res := c.Rounding.Round(c, d, x)
return res
return c.Rounding.Round(c, d, x, true /* disableIfPrecisionZero */)
}

// Rounder specifies the behavior of rounding.
Expand Down Expand Up @@ -63,12 +59,17 @@ func (r Rounder) ShouldAddOne(result *BigInt, neg bool, half int) bool {
}

// Round sets d to rounded x.
func (r Rounder) Round(c *Context, d, x *Decimal) Condition {
func (r Rounder) Round(c *Context, d, x *Decimal, disableIfPrecisionZero bool) Condition {
d.Set(x)
nd := x.NumDigits()
xs := x.Sign()
var res Condition

if disableIfPrecisionZero && c.Precision == 0 {
// Rounding has been disabled.
return d.setExponent(c, nd, res, int64(d.Exponent))
}

// adj is the adjusted exponent: exponent + clength - 1
if adj := int64(x.Exponent) + nd - 1; xs != 0 && adj < int64(c.MinExponent) {
// Subnormal is defined before rounding.
Expand Down
1 change: 1 addition & 0 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func init() {
}

// NumDigits returns the number of decimal digits of d.Coeff.
//gcassert:inline
func (d *Decimal) NumDigits() int64 {
return NumDigits(&d.Coeff)
}
Expand Down

0 comments on commit 86b4932

Please sign in to comment.