1
0
mirror of https://github.com/golang/go synced 2024-11-24 19:30:42 -07:00

math/big: slightly faster float->decimal conversion

Inspired by Alberto Donizetti's observations in
https://go-review.googlesource.com/#/c/30099/.

name                 old time/op  new time/op  delta
DecimalConversion-8   138µs ± 1%   136µs ± 2%  -1.85%  (p=0.000 n=10+10)

10 runs each, measured on a Mac Mini, 2.3 GHz Intel Core i7.

Performance improvements varied between -1.25% to -4.4%; -1.85% is
about in the middle of the observed improvement. The generated code
is slightly shorter in the inner loops of the conversion code.

Change-Id: I10fb3b2843da527691c39ad5e5e5bd37ed63e2fa
Reviewed-on: https://go-review.googlesource.com/31250
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Robert Griesemer 2016-10-14 17:09:54 -07:00
parent 984753b665
commit 92221fe8bc

View File

@ -125,11 +125,12 @@ func shr(x *decimal, s uint) {
// read a digit, write a digit // read a digit, write a digit
w := 0 // write index w := 0 // write index
mask := Word(1)<<s - 1
for r < len(x.mant) { for r < len(x.mant) {
ch := Word(x.mant[r]) ch := Word(x.mant[r])
r++ r++
d := n >> s d := n >> s
n -= d << s n &= mask // n -= d << s
x.mant[w] = byte(d + '0') x.mant[w] = byte(d + '0')
w++ w++
n = n*10 + ch - '0' n = n*10 + ch - '0'
@ -138,7 +139,7 @@ func shr(x *decimal, s uint) {
// write extra digits that still fit // write extra digits that still fit
for n > 0 && w < len(x.mant) { for n > 0 && w < len(x.mant) {
d := n >> s d := n >> s
n -= d << s n &= mask
x.mant[w] = byte(d + '0') x.mant[w] = byte(d + '0')
w++ w++
n = n * 10 n = n * 10
@ -148,7 +149,7 @@ func shr(x *decimal, s uint) {
// append additional digits that didn't fit // append additional digits that didn't fit
for n > 0 { for n > 0 {
d := n >> s d := n >> s
n -= d << s n &= mask
x.mant = append(x.mant, byte(d+'0')) x.mant = append(x.mant, byte(d+'0'))
n = n * 10 n = n * 10
} }