From: Russ Cox Date: Thu, 25 Aug 2011 21:54:14 +0000 (-0400) Subject: strconv: put decimal on stack X-Git-Tag: weekly.2011-09-01~81 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=cb51fdc00756ca2960769eca0da57e0c6e244a1b;p=gostls13.git strconv: put decimal on stack This makes decimal a good test case for the escape analysis. With escape analysis: benchmark old ns/op new ns/op delta BenchmarkAtof64Decimal 1954 243 -87.56% BenchmarkAtof64Float 2008 293 -85.41% BenchmarkAtof64FloatExp 10106 8814 -12.78% BenchmarkAtof64Big 5113 3486 -31.82% R=golang-dev, gri CC=golang-dev https://golang.org/cl/4861042 --- diff --git a/src/pkg/strconv/atof.go b/src/pkg/strconv/atof.go index 38b38053ce..86c56f7fd7 100644 --- a/src/pkg/strconv/atof.go +++ b/src/pkg/strconv/atof.go @@ -56,8 +56,9 @@ func special(s string) (f float64, ok bool) { } // TODO(rsc): Better truncation handling. -func stringToDecimal(s string) (neg bool, d *decimal, trunc bool, ok bool) { +func (b *decimal) set(s string) (ok bool) { i := 0 + b.neg = false // optional sign if i >= len(s) { @@ -67,12 +68,11 @@ func stringToDecimal(s string) (neg bool, d *decimal, trunc bool, ok bool) { case s[i] == '+': i++ case s[i] == '-': - neg = true + b.neg = true i++ } // digits - b := new(decimal) sawdot := false sawdigits := false for ; i < len(s); i++ { @@ -137,7 +137,6 @@ func stringToDecimal(s string) (neg bool, d *decimal, trunc bool, ok bool) { return } - d = b ok = true return } @@ -145,7 +144,7 @@ func stringToDecimal(s string) (neg bool, d *decimal, trunc bool, ok bool) { // decimal power of ten to binary power of two. var powtab = []int{1, 3, 6, 9, 13, 16, 19, 23, 26} -func decimalToFloatBits(neg bool, d *decimal, trunc bool, flt *floatInfo) (b uint64, overflow bool) { +func (d *decimal) floatBits(flt *floatInfo) (b uint64, overflow bool) { var exp int var mant uint64 @@ -209,7 +208,8 @@ func decimalToFloatBits(neg bool, d *decimal, trunc bool, flt *floatInfo) (b uin } // Extract 1+flt.mantbits bits. - mant = d.Shift(int(1 + flt.mantbits)).RoundedInteger() + d.Shift(int(1 + flt.mantbits)) + mant = d.RoundedInteger() // Rounding might have added a bit; shift down. if mant == 2< 15 { @@ -289,11 +289,11 @@ func decimalAtof64(neg bool, d *decimal, trunc bool) (f float64, ok bool) { } switch { case d.dp == d.nd: // int - f := decimalAtof64Int(neg, d) + f := d.atof64int() return f, true case d.dp > d.nd && d.dp <= 15+22: // int * 10^k - f := decimalAtof64Int(neg, d) + f := d.atof64int() k := d.dp - d.nd // If exponent is big but number of digits is not, // can move a few zeros into the integer part. @@ -304,7 +304,7 @@ func decimalAtof64(neg bool, d *decimal, trunc bool) (f float64, ok bool) { return f * float64pow10[k], true case d.dp < d.nd && d.nd-d.dp <= 22: // int / 10^k - f := decimalAtof64Int(neg, d) + f := d.atof64int() return f / float64pow10[d.nd-d.dp], true } return @@ -312,7 +312,7 @@ func decimalAtof64(neg bool, d *decimal, trunc bool) (f float64, ok bool) { // If possible to convert decimal d to 32-bit float f exactly, // entirely in floating-point math, do so, avoiding the machinery above. -func decimalAtof32(neg bool, d *decimal, trunc bool) (f float32, ok bool) { +func (d *decimal) atof32() (f float32, ok bool) { // Exact integers are <= 10^7. // Exact powers of ten are <= 10^10. if d.nd > 7 { @@ -320,11 +320,11 @@ func decimalAtof32(neg bool, d *decimal, trunc bool) (f float32, ok bool) { } switch { case d.dp == d.nd: // int - f := decimalAtof32Int(neg, d) + f := d.atof32int() return f, true case d.dp > d.nd && d.dp <= 7+10: // int * 10^k - f := decimalAtof32Int(neg, d) + f := d.atof32int() k := d.dp - d.nd // If exponent is big but number of digits is not, // can move a few zeros into the integer part. @@ -335,7 +335,7 @@ func decimalAtof32(neg bool, d *decimal, trunc bool) (f float32, ok bool) { return f * float32pow10[k], true case d.dp < d.nd && d.nd-d.dp <= 10: // int / 10^k - f := decimalAtof32Int(neg, d) + f := d.atof32int() return f / float32pow10[d.nd-d.dp], true } return @@ -360,16 +360,16 @@ func Atof32(s string) (f float32, err os.Error) { return float32(val), nil } - neg, d, trunc, ok := stringToDecimal(s) - if !ok { + var d decimal + if !d.set(s) { return 0, &NumError{s, os.EINVAL} } if optimize { - if f, ok := decimalAtof32(neg, d, trunc); ok { + if f, ok := d.atof32(); ok { return f, nil } } - b, ovf := decimalToFloatBits(neg, d, trunc, &float32info) + b, ovf := d.floatBits(&float32info) f = math.Float32frombits(uint32(b)) if ovf { err = &NumError{s, os.ERANGE} @@ -385,16 +385,16 @@ func Atof64(s string) (f float64, err os.Error) { return val, nil } - neg, d, trunc, ok := stringToDecimal(s) - if !ok { + var d decimal + if !d.set(s) { return 0, &NumError{s, os.EINVAL} } if optimize { - if f, ok := decimalAtof64(neg, d, trunc); ok { + if f, ok := d.atof64(); ok { return f, nil } } - b, ovf := decimalToFloatBits(neg, d, trunc, &float64info) + b, ovf := d.floatBits(&float64info) f = math.Float64frombits(b) if ovf { err = &NumError{s, os.ERANGE} diff --git a/src/pkg/strconv/atof_test.go b/src/pkg/strconv/atof_test.go index 0fdd0ea982..23aafc1e5d 100644 --- a/src/pkg/strconv/atof_test.go +++ b/src/pkg/strconv/atof_test.go @@ -34,6 +34,7 @@ var atoftests = []atofTest{ {"100000000000000016777215", "1.0000000000000001e+23", nil}, {"100000000000000016777216", "1.0000000000000003e+23", nil}, {"-1", "-1", nil}, + {"-0.1", "-0.1", nil}, {"-0", "-0", nil}, {"1e-20", "1e-20", nil}, {"625e-3", "0.625", nil}, diff --git a/src/pkg/strconv/decimal.go b/src/pkg/strconv/decimal.go index 783065bfbf..f572ea4a22 100644 --- a/src/pkg/strconv/decimal.go +++ b/src/pkg/strconv/decimal.go @@ -14,9 +14,10 @@ package strconv type decimal struct { // TODO(rsc): Can make d[] a bit smaller and add // truncated bool; - d [2000]byte // digits - nd int // number of digits used - dp int // decimal point + d [2000]byte // digits + nd int // number of digits used + dp int // decimal point + neg bool } func (a *decimal) String() string { @@ -266,8 +267,7 @@ func leftShift(a *decimal, k uint) { } // Binary shift left (k > 0) or right (k < 0). -// Returns receiver for convenience. -func (a *decimal) Shift(k int) *decimal { +func (a *decimal) Shift(k int) { switch { case a.nd == 0: // nothing to do: a == 0 @@ -284,7 +284,6 @@ func (a *decimal) Shift(k int) *decimal { } rightShift(a, uint(-k)) } - return a } // If we chop a at nd digits, should we round up? diff --git a/src/pkg/strconv/decimal_test.go b/src/pkg/strconv/decimal_test.go index 9b79035167..deb2e02f61 100644 --- a/src/pkg/strconv/decimal_test.go +++ b/src/pkg/strconv/decimal_test.go @@ -32,7 +32,9 @@ var shifttests = []shiftTest{ func TestDecimalShift(t *testing.T) { for i := 0; i < len(shifttests); i++ { test := &shifttests[i] - s := NewDecimal(test.i).Shift(test.shift).String() + d := NewDecimal(test.i) + d.Shift(test.shift) + s := d.String() if s != test.out { t.Errorf("Decimal %v << %v = %v, want %v", test.i, test.shift, s, test.out) @@ -108,7 +110,9 @@ var roundinttests = []roundIntTest{ func TestDecimalRoundedInteger(t *testing.T) { for i := 0; i < len(roundinttests); i++ { test := roundinttests[i] - int := NewDecimal(test.i).Shift(test.shift).RoundedInteger() + d := NewDecimal(test.i) + d.Shift(test.shift) + int := d.RoundedInteger() if int != test.int { t.Errorf("Decimal %v >> %v RoundedInteger = %v, want %v", test.i, test.shift, int, test.int) diff --git a/src/pkg/strconv/ftoa.go b/src/pkg/strconv/ftoa.go index b6049c5458..07fe806b97 100644 --- a/src/pkg/strconv/ftoa.go +++ b/src/pkg/strconv/ftoa.go @@ -98,7 +98,8 @@ func genericFtoa(bits uint64, fmt byte, prec int, flt *floatInfo) string { // The shift is exp - flt.mantbits because mant is a 1-bit integer // followed by a flt.mantbits fraction, and we are treating it as // a 1+flt.mantbits-bit integer. - d := newDecimal(mant).Shift(exp - int(flt.mantbits)) + d := newDecimal(mant) + d.Shift(exp - int(flt.mantbits)) // Round appropriately. // Negative precision means "only as much as needed to be exact." @@ -183,7 +184,8 @@ func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo) { // d = mant << (exp - mantbits) // Next highest floating point number is mant+1 << exp-mantbits. // Our upper bound is halfway inbetween, mant*2+1 << exp-mantbits-1. - upper := newDecimal(mant*2 + 1).Shift(exp - int(flt.mantbits) - 1) + upper := newDecimal(mant*2 + 1) + upper.Shift(exp - int(flt.mantbits) - 1) // d = mant << (exp - mantbits) // Next lowest floating point number is mant-1 << exp-mantbits, @@ -201,7 +203,8 @@ func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo) { mantlo = mant*2 - 1 explo = exp - 1 } - lower := newDecimal(mantlo*2 + 1).Shift(explo - int(flt.mantbits) - 1) + lower := newDecimal(mantlo*2 + 1) + lower.Shift(explo - int(flt.mantbits) - 1) // The upper and lower bounds are possible outputs only if // the original mantissa is even, so that IEEE round-to-even