Files
wuffs/script/print-render-number-f64-tests.go
T
Nigel Tao 86093c8194 Have parse_number_f64 use the Eisel algorithm
The algorithm is discussed at
https://lemire.me/blog/2020/03/10/fast-float-parsing-in-practice/

----

Binary size, before:
27848 gen/lib/c/clang-5.0-dynamic/wuffs-base-f64conv.lo
28040 gen/lib/c/clang-5.0-static/wuffs-base-f64conv.o
30392 gen/lib/c/gcc-dynamic/wuffs-base-f64conv.lo
30392 gen/lib/c/gcc-static/wuffs-base-f64conv.o

After:
29416 gen/lib/c/clang-5.0-dynamic/wuffs-base-f64conv.lo
29688 gen/lib/c/clang-5.0-static/wuffs-base-f64conv.o
31792 gen/lib/c/gcc-dynamic/wuffs-base-f64conv.lo
31792 gen/lib/c/gcc-static/wuffs-base-f64conv.o

The binary size difference does not account for the powers-of-10 table,
which was expanded in a previous commit in anticipation of this one. A
future commit will also remove some of the other f64 parser code used
before implementing the Eisel algorithm.

----

Processing citylots.json from github.com/zemirco/sf-city-lots-json
$ g++ -O3 script/process-json-numbers.c
$ time ./a.out -parse-number-f64 < citylots.json
Before/After:
real    0m1.231s
real	0m1.031s

The difference with and without -parse-number-f64 measures how much time
is spent in the parse_number_f64 function. Before: 424ms, after: 224ms.
$ time ./a.out                   < citylots.json
real	0m0.807s

Similarly, for canada.json from github.com/miloyip/nativejson-benchmark
$ time ./a.out -parse-number-f64 < canada.json
Before/After:
real	0m0.023s
real	0m0.017s
$ time ./a.out                   < canada.json
real	0m0.013s
Time spent in parse_number_f64, before: 10ms, after: 4ms.

----

name                                                old time/op  new time/op   delta
wuffs_strconv_parse_number_f64_1_lsh53_add0/clang5  77.0ns ± 0%   37.0ns ± 0%  -51.95%  (p=0.029 n=4+4)
wuffs_strconv_parse_number_f64_1_lsh53_add1/clang5   690ns ± 0%    725ns ± 2%   +4.95%  (p=0.008 n=5+5)
wuffs_strconv_parse_number_f64_pi_long/clang5       92.0ns ± 0%  136.0ns ± 2%  +47.83%  (p=0.016 n=4+5)
wuffs_strconv_parse_number_f64_pi_short/clang5      35.0ns ± 3%   18.0ns ± 0%  -48.57%  (p=0.008 n=5+5)
wuffs_strconv_parse_number_f64_1_lsh53_add0/gcc7    67.0ns ± 0%   34.0ns ± 0%  -49.25%  (p=0.000 n=5+4)
wuffs_strconv_parse_number_f64_1_lsh53_add1/gcc7     712ns ± 0%    760ns ± 2%   +6.72%  (p=0.008 n=5+5)
wuffs_strconv_parse_number_f64_pi_long/gcc7         88.4ns ± 1%  116.2ns ± 3%  +31.45%  (p=0.008 n=5+5)
wuffs_strconv_parse_number_f64_pi_short/gcc7        32.6ns ± 2%   21.4ns ± 7%  -34.36%  (p=0.008 n=5+5)
2020-07-07 15:27:51 +10:00

181 lines
3.8 KiB
Go

// Copyright 2020 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build ignore
package main
// print-render-number-f64-tests.go prints the
// test_wuffs_strconv_render_number_f64 test cases.
//
// Usage: go run print-render-number-f64-tests.go
import (
"fmt"
"math"
"os"
"sort"
"strconv"
)
func main() {
if err := main1(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}
func main1() error {
testCases := append([]uint64(nil), u64TestCases...)
for _, f := range f64TestCases {
testCases = append(testCases, math.Float64bits(f))
}
sort.Slice(testCases, func(i int, j int) bool {
return testCases[i] < testCases[j]
})
for i, tc := range testCases {
f := math.Float64frombits(tc)
if (i > 0) && (tc == testCases[i-1]) {
return fmt.Errorf("duplicate test case (f=%g, tc=0x%X)", f, tc)
}
// Check that calling strconv.FormatFloat with a precision of -1 (round
// to shortest) does indeed return a string that, when parsed, recovers
// the original number.
shortest := strconv.FormatFloat(f, 'g', -1, 64)
g, err := strconv.ParseFloat(shortest, 64)
if err != nil {
return fmt.Errorf("ParseFloat failed (f=%g, tc=0x%X): %v", f, tc, err)
}
equal := tc == math.Float64bits(g)
if math.IsNaN(f) {
equal = math.IsNaN(g)
}
if !equal {
return fmt.Errorf("round-trip failed (f=%g, tc=0x%X)", f, tc)
}
}
for _, tc := range testCases {
f := math.Float64frombits(tc)
fmt.Printf(`{
.x = 0x%016X,
.want__e = %s,
.want__f = %s,
.want_0g = %s,
.want_2e = %s,
.want_3f = %s,
.want_4g = %s,
},`+"\n",
tc,
do(f, -1, 'e'),
do(f, -1, 'f'),
do(f, +0, 'g'),
do(f, +2, 'e'),
do(f, +3, 'f'),
do(f, +4, 'g'),
)
}
return nil
}
func do(f float64, precision int, format byte) (ret string) {
s := strconv.FormatFloat(f, format, precision, 64)
for ; len(s) > 50; s = s[50:] {
ret += fmt.Sprintf("%q\n\t\t", s[:50])
}
ret += fmt.Sprintf("%q", s)
if ret == `"+Inf"` {
ret = `"Inf"`
}
return ret
}
var f64TestCases = []float64{
// Approximations of e, the base of the natural logarithm.
2.7,
2.72,
2.718,
2.7183,
2.71828,
2.718282,
2.7182818,
2.71828183,
// Approximations of N_A, the Avogadro constant.
6.0e23,
6.02e23,
6.022e23,
6.0221e23,
6.02214e23,
6.022141e23,
6.0221408e23,
6.02214076e23,
}
var u64TestCases = []uint64{
0x0000000000000000,
0x0000000000000001,
0x0000000000000002,
0x0000000000000003,
0x000FFFFFFFFFFFFF,
0x0010000000000000,
0x369C314ABE948EB1,
0x3F88000000000000,
0x3FD0000000000000,
0x3FD3333333333333,
0x3FD3333333333334,
0x3FD5555555555555,
0x3FEFFFFFFFFFFFFF,
0x3FF0000000000000,
0x3FF0000000000001,
0x3FF0000000000002,
0x3FF4000000000000,
0x3FF8000000000000,
0x4008000000000000,
0x400921F9F01B866E,
0x400921FB54442D11,
0x400921FB54442D18,
0x400C000000000000,
0x4014000000000000,
0x4036000000000000,
0x4037000000000000,
0x4038000000000000,
0x40FE240C9FCB0C02,
0x4202A05F20000000,
0x4330000000000000,
0x4330000000000001,
0x4330000000000002,
0x433FFFFFFFFFFFFE,
0x433FFFFFFFFFFFFF,
0x4340000000000000,
0x4340000000000001,
0x4340000000000002,
0x4370000000000000,
0x4415AF1D78B58C40,
0x44B52D02C7E14AF6,
0x46293E5939A08CEA,
0x54B249AD2594C37D,
0x7FEFFFFFFFFFFFFF,
0x7FF0000000000000,
0x7FFFFFFFFFFFFFFF,
0x8000000000000000,
0xC008000000000000,
0xFFF0000000000000,
0xFFFFFFFFFFFFFFFF,
}