Skip to content

Commit 86b4932

Browse files
authored
Merge pull request #112 from nvanbenschoten/nvanbenschoten/inlineGoError
apd: avoid function calls on hot paths
2 parents a3aa8bd + c932774 commit 86b4932

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

bigint.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func NewBigInt(x int64) *BigInt {
7373
var negSentinel = new(big.Int)
7474

7575
// isInline returns whether the BigInt stores its value in its _inline array.
76+
//gcassert:inline
7677
func (z *BigInt) isInline() bool {
7778
return z._inner == nil || z._inner == negSentinel
7879
}

context.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ func (c *Context) WithPrecision(p uint32) *Context {
8181
// goError converts flags into an error based on c.Traps.
8282
//gcassert:inline
8383
func (c *Context) goError(flags Condition) (Condition, error) {
84+
if flags == 0 {
85+
return flags, nil
86+
}
8487
return flags.GoError(c.Traps)
8588
}
8689

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

12191222
d.Exponent = -diff
1220-
// Avoid the c.Precision == 0 check.
1221-
res = nc.Rounding.Round(nc, d, d)
1223+
// Round even if nc.Precision == 0.
1224+
res = nc.Rounding.Round(nc, d, d, false /* disableIfPrecisionZero */)
12221225
// Adjust for 0.9 -> 1.0 rollover.
12231226
if d.Exponent > 0 {
12241227
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.

table.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func init() {
5252
}
5353

5454
// NumDigits returns the number of decimal digits of d.Coeff.
55+
//gcassert:inline
5556
func (d *Decimal) NumDigits() int64 {
5657
return NumDigits(&d.Coeff)
5758
}

0 commit comments

Comments
 (0)