mirror of
https://github.com/golang/go
synced 2024-11-21 22:04:39 -07:00
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
This commit is contained in:
parent
dd0b8e7975
commit
cb51fdc007
@ -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<<flt.mantbits {
|
||||
@ -236,7 +236,7 @@ out:
|
||||
// Assemble bits.
|
||||
bits := mant & (uint64(1)<<flt.mantbits - 1)
|
||||
bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits
|
||||
if neg {
|
||||
if d.neg {
|
||||
bits |= 1 << flt.mantbits << flt.expbits
|
||||
}
|
||||
return bits, overflow
|
||||
@ -244,24 +244,24 @@ out:
|
||||
|
||||
// Compute exact floating-point integer from d's digits.
|
||||
// Caller is responsible for avoiding overflow.
|
||||
func decimalAtof64Int(neg bool, d *decimal) float64 {
|
||||
func (d *decimal) atof64int() float64 {
|
||||
f := 0.0
|
||||
for i := 0; i < d.nd; i++ {
|
||||
f = f*10 + float64(d.d[i]-'0')
|
||||
}
|
||||
if neg {
|
||||
f *= -1 // BUG work around 6g f = -f.
|
||||
if d.neg {
|
||||
f = -f
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func decimalAtof32Int(neg bool, d *decimal) float32 {
|
||||
func (d *decimal) atof32int() float32 {
|
||||
f := float32(0)
|
||||
for i := 0; i < d.nd; i++ {
|
||||
f = f*10 + float32(d.d[i]-'0')
|
||||
}
|
||||
if neg {
|
||||
f *= -1 // BUG work around 6g f = -f.
|
||||
if d.neg {
|
||||
f = -f
|
||||
}
|
||||
return f
|
||||
}
|
||||
@ -281,7 +281,7 @@ var float32pow10 = []float32{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1
|
||||
// value is exact integer * exact power of ten
|
||||
// value is exact integer / exact power of ten
|
||||
// These all produce potentially inexact but correctly rounded answers.
|
||||
func decimalAtof64(neg bool, d *decimal, trunc bool) (f float64, ok bool) {
|
||||
func (d *decimal) atof64() (f float64, ok bool) {
|
||||
// Exact integers are <= 10^15.
|
||||
// Exact powers of ten are <= 10^22.
|
||||
if d.nd > 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}
|
||||
|
@ -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},
|
||||
|
@ -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?
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user