aboutsummaryrefslogtreecommitdiff
path: root/src/math
diff options
context:
space:
mode:
authorBrian Kessler <brian.m.kessler@gmail.com>2017-08-09 00:09:07 -0700
committerRobert Griesemer <gri@golang.org>2017-08-11 09:52:30 +0000
commit9c7bf0807a56429a92c4518f90d3418609688cbb (patch)
tree613fb5a126ee3d0f9ff98661144f4e29c3c7415a /src/math
parente9348ab4e9c8c189036ef405d73528ca50a6f785 (diff)
downloadgo-9c7bf0807a56429a92c4518f90d3418609688cbb.tar.xz
math/big: avoid unneeded sticky bit calculations
As noted in the TODO comment, the sticky bit is only used when the rounding bit is zero or the rounding mode is ToNearestEven. This change makes that check explicit and will eliminate half the sticky bit calculations on average when rounding mode is not ToNearestEven. Change-Id: Ia4709f08f46e682bf97dabe5eb2a10e8e3d7af43 Reviewed-on: https://go-review.googlesource.com/54111 Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/math')
-rw-r--r--src/math/big/float.go5
1 files changed, 3 insertions, 2 deletions
diff --git a/src/math/big/float.go b/src/math/big/float.go
index 7e11f1aff5..afbed306f3 100644
--- a/src/math/big/float.go
+++ b/src/math/big/float.go
@@ -415,8 +415,9 @@ func (z *Float) round(sbit uint) {
// bits > z.prec: mantissa too large => round
r := uint(bits - z.prec - 1) // rounding bit position; r >= 0
rbit := z.mant.bit(r) & 1 // rounding bit; be safe and ensure it's a single bit
- if sbit == 0 {
- // TODO(gri) if rbit != 0 we don't need to compute sbit for some rounding modes (optimization)
+ // The sticky bit is only needed for rounding ToNearestEven
+ // or when the rounding bit is zero. Avoid computation otherwise.
+ if sbit == 0 && (rbit == 0 || z.mode == ToNearestEven) {
sbit = z.mant.sticky(r)
}
sbit &= 1 // be safe and ensure it's a single bit