Skip to content

Commit c932774

Browse files
committed
apd: inline Context.round
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.
1 parent b26ba6f commit c932774

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,8 +1220,8 @@ func (c *Context) quantize(d, v *Decimal, exp int32) Condition {
12201220
// target eliminates this problem.
12211221

12221222
d.Exponent = -diff
1223-
// Avoid the c.Precision == 0 check.
1224-
res = nc.Rounding.Round(nc, d, d)
1223+
// Round even if nc.Precision == 0.
1224+
res = nc.Rounding.Round(nc, d, d, false /* disableIfPrecisionZero */)
12251225
// Adjust for 0.9 -> 1.0 rollover.
12261226
if d.Exponent > 0 {
12271227
d.Coeff.Mul(&d.Coeff, bigTen)

round.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@ func (c *Context) Round(d, x *Decimal) (Condition, error) {
2121
return c.goError(c.round(d, x))
2222
}
2323

24+
//gcassert:inline
2425
func (c *Context) round(d, x *Decimal) Condition {
25-
if c.Precision == 0 {
26-
d.Set(x)
27-
return d.setExponent(c, unknownNumDigits, 0, int64(d.Exponent))
28-
}
29-
res := c.Rounding.Round(c, d, x)
30-
return res
26+
return c.Rounding.Round(c, d, x, true /* disableIfPrecisionZero */)
3127
}
3228

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

6561
// Round sets d to rounded x.
66-
func (r Rounder) Round(c *Context, d, x *Decimal) Condition {
62+
func (r Rounder) Round(c *Context, d, x *Decimal, disableIfPrecisionZero bool) Condition {
6763
d.Set(x)
6864
nd := x.NumDigits()
6965
xs := x.Sign()
7066
var res Condition
7167

68+
if disableIfPrecisionZero && c.Precision == 0 {
69+
// Rounding has been disabled.
70+
return d.setExponent(c, nd, res, int64(d.Exponent))
71+
}
72+
7273
// adj is the adjusted exponent: exponent + clength - 1
7374
if adj := int64(x.Exponent) + nd - 1; xs != 0 && adj < int64(c.MinExponent) {
7475
// Subnormal is defined before rounding.

0 commit comments

Comments
 (0)