]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libgo/go/strconv/ftoa_test.go
libgo: Update to current sources.
[thirdparty/gcc.git] / libgo / go / strconv / ftoa_test.go
index ee7b7c431e7fa413e2c48fe0dea27c3b72a7dc52..39b861547eae437b255c6f8c4c47abf41ed85504 100644 (file)
@@ -163,6 +163,7 @@ func TestFtoaRandom(t *testing.T) {
        for i := 0; i < N; i++ {
                bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
                x := math.Float64frombits(bits)
+
                shortFast := FormatFloat(x, 'g', -1, 64)
                SetOptimize(false)
                shortSlow := FormatFloat(x, 'g', -1, 64)
@@ -170,30 +171,18 @@ func TestFtoaRandom(t *testing.T) {
                if shortSlow != shortFast {
                        t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
                }
-       }
-}
-
-/* This test relies on escape analysis which gccgo does not yet do.
 
-func TestAppendFloatDoesntAllocate(t *testing.T) {
-       n := numAllocations(func() {
-               var buf [64]byte
-               AppendFloat(buf[:0], 1.23, 'g', 5, 64)
-       })
-       want := 1 // TODO(bradfitz): this might be 0, once escape analysis is better
-       if n != want {
-               t.Errorf("with local buffer, did %d allocations, want %d", n, want)
-       }
-       n = numAllocations(func() {
-               AppendFloat(globalBuf[:0], 1.23, 'g', 5, 64)
-       })
-       if n != 0 {
-               t.Errorf("with reused buffer, did %d allocations, want 0", n)
+               prec := rand.Intn(12) + 5
+               shortFast = FormatFloat(x, 'e', prec, 64)
+               SetOptimize(false)
+               shortSlow = FormatFloat(x, 'e', prec, 64)
+               SetOptimize(true)
+               if shortSlow != shortFast {
+                       t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
+               }
        }
 }
 
-*/
-
 func BenchmarkFormatFloatDecimal(b *testing.B) {
        for i := 0; i < b.N; i++ {
                FormatFloat(33909, 'g', -1, 64)
@@ -224,37 +213,28 @@ func BenchmarkFormatFloatBig(b *testing.B) {
        }
 }
 
-func BenchmarkAppendFloatDecimal(b *testing.B) {
-       dst := make([]byte, 0, 30)
+func benchmarkAppendFloat(b *testing.B, f float64, fmt byte, prec, bitSize int) {
+       dst := make([]byte, 30)
        for i := 0; i < b.N; i++ {
-               AppendFloat(dst, 33909, 'g', -1, 64)
+               AppendFloat(dst[:0], f, fmt, prec, bitSize)
        }
 }
 
-func BenchmarkAppendFloat(b *testing.B) {
-       dst := make([]byte, 0, 30)
-       for i := 0; i < b.N; i++ {
-               AppendFloat(dst, 339.7784, 'g', -1, 64)
-       }
-}
-
-func BenchmarkAppendFloatExp(b *testing.B) {
-       dst := make([]byte, 0, 30)
-       for i := 0; i < b.N; i++ {
-               AppendFloat(dst, -5.09e75, 'g', -1, 64)
-       }
+func BenchmarkAppendFloatDecimal(b *testing.B) { benchmarkAppendFloat(b, 33909, 'g', -1, 64) }
+func BenchmarkAppendFloat(b *testing.B)        { benchmarkAppendFloat(b, 339.7784, 'g', -1, 64) }
+func BenchmarkAppendFloatExp(b *testing.B)     { benchmarkAppendFloat(b, -5.09e75, 'g', -1, 64) }
+func BenchmarkAppendFloatNegExp(b *testing.B)  { benchmarkAppendFloat(b, -5.11e-95, 'g', -1, 64) }
+func BenchmarkAppendFloatBig(b *testing.B) {
+       benchmarkAppendFloat(b, 123456789123456789123456789, 'g', -1, 64)
 }
 
-func BenchmarkAppendFloatNegExp(b *testing.B) {
-       dst := make([]byte, 0, 30)
-       for i := 0; i < b.N; i++ {
-               AppendFloat(dst, -5.11e-95, 'g', -1, 64)
-       }
-}
+func BenchmarkAppendFloat32Integer(b *testing.B)       { benchmarkAppendFloat(b, 33909, 'g', -1, 32) }
+func BenchmarkAppendFloat32ExactFraction(b *testing.B) { benchmarkAppendFloat(b, 3.375, 'g', -1, 32) }
+func BenchmarkAppendFloat32Point(b *testing.B)         { benchmarkAppendFloat(b, 339.7784, 'g', -1, 32) }
+func BenchmarkAppendFloat32Exp(b *testing.B)           { benchmarkAppendFloat(b, -5.09e25, 'g', -1, 32) }
+func BenchmarkAppendFloat32NegExp(b *testing.B)        { benchmarkAppendFloat(b, -5.11e-25, 'g', -1, 32) }
 
-func BenchmarkAppendFloatBig(b *testing.B) {
-       dst := make([]byte, 0, 30)
-       for i := 0; i < b.N; i++ {
-               AppendFloat(dst, 123456789123456789123456789, 'g', -1, 64)
-       }
-}
+func BenchmarkAppendFloat64Fixed1(b *testing.B) { benchmarkAppendFloat(b, 123456, 'e', 3, 64) }
+func BenchmarkAppendFloat64Fixed2(b *testing.B) { benchmarkAppendFloat(b, 123.456, 'e', 3, 64) }
+func BenchmarkAppendFloat64Fixed3(b *testing.B) { benchmarkAppendFloat(b, 1.23456e+78, 'e', 3, 64) }
+func BenchmarkAppendFloat64Fixed4(b *testing.B) { benchmarkAppendFloat(b, 1.23456e-78, 'e', 3, 64) }