mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 16:47:28 +01:00
Add medium-precision-binary floating point impl
name old time/op new time/op delta strconv_parse_number_f64_1_lsh53_add0/clang5 218ns ± 1% 100ns ± 0% -54.13% (p=0.016 n=5+4) strconv_parse_number_f64_1_lsh53_add1/clang5 716ns ± 2% 741ns ± 1% +3.46% (p=0.008 n=5+5) strconv_parse_number_f64_pi_long/clang5 438ns ± 2% 153ns ± 1% -65.19% (p=0.008 n=5+5) strconv_parse_number_f64_pi_short/clang5 174ns ± 2% 62ns ± 2% -64.37% (p=0.008 n=5+5) strconv_parse_number_f64_1_lsh53_add0/gcc7 221ns ± 1% 97ns ± 4% -56.25% (p=0.008 n=5+5) strconv_parse_number_f64_1_lsh53_add1/gcc7 742ns ± 1% 757ns ± 0% +2.02% (p=0.008 n=5+5) strconv_parse_number_f64_pi_long/gcc7 477ns ± 1% 151ns ± 3% -68.40% (p=0.008 n=5+5) strconv_parse_number_f64_pi_short/gcc7 191ns ± 1% 55ns ± 0% -71.14% (p=0.000 n=5+4)
This commit is contained in:
1 parent
3f0d66285f
commit
d587c9e4fa
8 files changed
+1300
-42
No files matched your search
@@ -27,6 +27,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -48,6 +49,13 @@ func ellipsize(s string) string {
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := main1(); err != nil {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func main1() error {
|
||||
flag.Parse()
|
||||
|
||||
const WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL = 60
|
||||
@@ -111,4 +119,5 @@ func main() {
|
||||
fmt.Printf(" // offset=0x%04X\n", len(data)&^15)
|
||||
}
|
||||
fmt.Printf("};\n")
|
||||
return nil
|
||||
}
|
||||
@@ -33,6 +33,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -74,6 +75,13 @@ func codeString(code int) string {
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := main1(); err != nil {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func main1() error {
|
||||
n, s, q := new(naive), new(suf1), new(sufQ)
|
||||
decode(n)
|
||||
decode(s)
|
||||
@@ -109,6 +117,7 @@ func main() {
|
||||
fmt.Println()
|
||||
key++
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type implementation interface {
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
// 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-mpb-powers-of-10.go prints the
|
||||
// wuffs_base__private_implementation__medium_prec_bin__powers_of_10 tables.
|
||||
//
|
||||
// Usage: go run print-mpb-powers-of-10.go -comments
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
comments = flag.Bool("comments", false, "whether to print comments")
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := main1(); err != nil {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func main1() error {
|
||||
flag.Parse()
|
||||
|
||||
const bigCount = 1 + ((+340 - -348) / 8)
|
||||
fmt.Printf("static const uint32_t "+
|
||||
"wuffs_base__private_implementation__big_powers_of_10[%d] = {\n", 3*bigCount)
|
||||
for e := -348; e <= +340; e += 8 {
|
||||
if err := do(e); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fmt.Printf("};\n\n")
|
||||
|
||||
fmt.Printf("static const uint32_t " +
|
||||
"wuffs_base__private_implementation__small_powers_of_10[24] = {\n")
|
||||
for e := 0; e <= 7; e += 1 {
|
||||
if err := do(e); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fmt.Printf("};\n")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
one = big.NewInt(1)
|
||||
ten = big.NewInt(10)
|
||||
two64 = big.NewInt(0).Lsh(one, 64)
|
||||
)
|
||||
|
||||
// N is large enough so that (1<<N) is bigger than 1e348.
|
||||
const N = 2048
|
||||
|
||||
func do(e int) error {
|
||||
z := big.NewInt(0).Lsh(one, N)
|
||||
if e >= 0 {
|
||||
exp := big.NewInt(0).Exp(ten, big.NewInt(int64(+e)), nil)
|
||||
z.Mul(z, exp)
|
||||
} else {
|
||||
exp := big.NewInt(0).Exp(ten, big.NewInt(int64(-e)), nil)
|
||||
z.Div(z, exp)
|
||||
}
|
||||
|
||||
roundUp := false
|
||||
n := int32(-N)
|
||||
for z.Cmp(two64) >= 0 {
|
||||
roundUp = z.Bit(0) > 0
|
||||
z.Rsh(z, 1)
|
||||
n++
|
||||
}
|
||||
if roundUp {
|
||||
z.Add(z, one)
|
||||
}
|
||||
hex := fmt.Sprintf("%X", z)
|
||||
if len(hex) != 16 {
|
||||
return fmt.Errorf("invalid hexadecimal representation %q", hex)
|
||||
}
|
||||
|
||||
fmt.Printf(" 0x%s, 0x%s, 0x%08X,", hex[8:], hex[:8], uint32(n))
|
||||
if *comments {
|
||||
fmt.Printf(" // 1e%-04d ≈ (0x%s ", e, hex)
|
||||
if n >= 0 {
|
||||
fmt.Printf("<< %4d)", +n)
|
||||
} else {
|
||||
fmt.Printf(">> %4d)", -n)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user