@@ -393,27 +393,45 @@ func TestRoundTrip32(t *testing.T) {
393
393
t .Logf ("tested %d float32's" , count )
394
394
}
395
395
396
- func BenchmarkAtof64Decimal (b * testing.B ) {
397
- for i := 0 ; i < b .N ; i ++ {
398
- ParseFloat ("33909" , 64 )
399
- }
400
- }
401
-
402
- func BenchmarkAtof64Float (b * testing.B ) {
403
- for i := 0 ; i < b .N ; i ++ {
404
- ParseFloat ("339.7784" , 64 )
405
- }
406
- }
407
-
408
- func BenchmarkAtof64FloatExp (b * testing.B ) {
409
- for i := 0 ; i < b .N ; i ++ {
410
- ParseFloat ("-5.09e75" , 64 )
411
- }
396
+ var atofBenches = []struct {
397
+ name string
398
+ arg string
399
+ bitSize int
400
+ }{
401
+ {"64Decimal" , "33909" , 64 },
402
+ {"64Float" , "339.7784" , 64 },
403
+ {"64FloatExp" , "-5.09e75" , 64 },
404
+ {"64Big" , "123456789123456789123456789" , 64 },
405
+ {"64Denormal" , "622666234635.3213e-320" , 64 },
406
+ {"32Decimal" , "33909" , 32 },
407
+ {"32Float" , "339.778" , 32 },
408
+ {"32FloatExp" , "12.3456e32" , 32 },
409
+ // Numbers halfway (or close to halfway) between 2 floats
410
+ {"64HalfwayInt" , "100000000000000016777215" , 64 },
411
+ {"64HalfwayInt" , "100000000000000016777216" , 64 },
412
+ // Almost halfway, with less than 1e-16 ulp difference
413
+ // with only 16 decimal digits.
414
+ {"64HalfwayHard1" , "6808957268280643e116" , 64 }, // from ftoahard
415
+ {"64HalfwayHard2" , "4.334126125515466e-210" , 64 }, // from ftoahard
416
+ // Only 3e-13*ulp larger than halfway between denormals,
417
+ {"64HalfwayDenormal" , "1.68514038588815e-309" , 64 },
418
+ // Few digits, but 9.11691642378e-312 = 0x1ada385d67b.7fffffff5d9...p-1074
419
+ // so naive, rounded 64-bit arithmetic is not enough to round it correctly.
420
+ {"64HalfwayDenormalShort" , "9.11691642378e-312" , 64 },
421
+ // 1.62420278e-315 = 0x1398359e.7fffe022p-1074,
422
+ // should parsable using 64-bit arithmetic.
423
+ {"64HalfwayDenormalVeryShort" , "1.62420278e-315" , 64 },
424
+ // https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
425
+ {"64Denormal" , "2.2250738585072011e-308" , 64 },
412
426
}
413
427
414
- func BenchmarkAtof64Big (b * testing.B ) {
415
- for i := 0 ; i < b .N ; i ++ {
416
- ParseFloat ("123456789123456789123456789" , 64 )
428
+ func BenchmarkAtof (b * testing.B ) {
429
+ for _ , c := range atofBenches {
430
+ b .Run (c .arg , func (b * testing.B ) {
431
+ for i := 0 ; i < b .N ; i ++ {
432
+ ParseFloat (c .arg , c .bitSize )
433
+ }
434
+ })
417
435
}
418
436
}
419
437
@@ -429,24 +447,6 @@ func BenchmarkAtof64RandomFloats(b *testing.B) {
429
447
}
430
448
}
431
449
432
- func BenchmarkAtof32Decimal (b * testing.B ) {
433
- for i := 0 ; i < b .N ; i ++ {
434
- ParseFloat ("33909" , 32 )
435
- }
436
- }
437
-
438
- func BenchmarkAtof32Float (b * testing.B ) {
439
- for i := 0 ; i < b .N ; i ++ {
440
- ParseFloat ("339.778" , 32 )
441
- }
442
- }
443
-
444
- func BenchmarkAtof32FloatExp (b * testing.B ) {
445
- for i := 0 ; i < b .N ; i ++ {
446
- ParseFloat ("12.3456e32" , 32 )
447
- }
448
- }
449
-
450
450
var float32strings [4096 ]string
451
451
452
452
func BenchmarkAtof32Random (b * testing.B ) {
0 commit comments