Skip to content

Commit

Permalink
apd: inline Context.round
Browse files Browse the repository at this point in the history
This commit reworks the call to `Context.round` to allow for mid-stack
function inlining. To do this, we remove the conditional call to one
of two functions, which is considered too complex to inline.
  • Loading branch information
nvanbenschoten committed Jan 29, 2022
1 parent b26ba6f commit c932774
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1220,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

0 comments on commit c932774

Please sign in to comment.