blob: b8cce997ccf413a51e3384db207149de76a0508f [file] [log] [blame]
Nigel Taod587c9e2020-03-29 22:10:49 +11001// Copyright 2020 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// +build ignore
16
17package main
18
Nigel Tao267bfc82020-07-05 14:37:57 +100019// print-mpb-powers-of-10.go prints the medium-precision (128 bit mantissa)
Nigel Tao53da68f2020-07-05 13:52:06 +100020// binary (base 2) wuffs_base__private_implementation__powers_of_10 tables.
Nigel Taod587c9e2020-03-29 22:10:49 +110021//
Nigel Tao53da68f2020-07-05 13:52:06 +100022// Usage: go run print-mpb-powers-of-10.go -detail
Nigel Taod587c9e2020-03-29 22:10:49 +110023
24import (
25 "flag"
26 "fmt"
27 "math/big"
28 "os"
29)
30
31var (
Nigel Tao53da68f2020-07-05 13:52:06 +100032 detail = flag.Bool("detail", false, "whether to print detailed comments")
Nigel Taod587c9e2020-03-29 22:10:49 +110033)
34
35func main() {
36 if err := main1(); err != nil {
37 os.Stderr.WriteString(err.Error() + "\n")
38 os.Exit(1)
39 }
40}
41
42func main1() error {
43 flag.Parse()
44
Nigel Tao267bfc82020-07-05 14:37:57 +100045 const count = 1 + (+310 - -326)
Nigel Taod587c9e2020-03-29 22:10:49 +110046 fmt.Printf("static const uint32_t "+
Nigel Tao267bfc82020-07-05 14:37:57 +100047 "wuffs_base__private_implementation__powers_of_10[%d] = {\n", 5*count)
Nigel Tao53da68f2020-07-05 13:52:06 +100048 for e := -326; e <= +310; e++ {
Nigel Taod587c9e2020-03-29 22:10:49 +110049 if err := do(e); err != nil {
50 return err
51 }
52 }
53 fmt.Printf("};\n\n")
54
Nigel Taod587c9e2020-03-29 22:10:49 +110055 return nil
56}
57
58var (
Nigel Tao267bfc82020-07-05 14:37:57 +100059 one = big.NewInt(1)
60 ten = big.NewInt(10)
61 two128 = big.NewInt(0).Lsh(one, 128)
Nigel Taod587c9e2020-03-29 22:10:49 +110062)
63
Nigel Tao267bfc82020-07-05 14:37:57 +100064// N is large enough so that (1<<N) is easily bigger than 1e310.
Nigel Taod587c9e2020-03-29 22:10:49 +110065const N = 2048
66
Nigel Tao267bfc82020-07-05 14:37:57 +100067// 1214 is 1023 + 191. 1023 is the bias for IEEE 754 double-precision floating
68// point. 191 is ((3 * 64) - 1) and we work with multiples-of-64-bit mantissas.
69const bias = 1214
70
Nigel Taod587c9e2020-03-29 22:10:49 +110071func do(e int) error {
72 z := big.NewInt(0).Lsh(one, N)
73 if e >= 0 {
74 exp := big.NewInt(0).Exp(ten, big.NewInt(int64(+e)), nil)
75 z.Mul(z, exp)
76 } else {
77 exp := big.NewInt(0).Exp(ten, big.NewInt(int64(-e)), nil)
78 z.Div(z, exp)
79 }
80
Nigel Taod587c9e2020-03-29 22:10:49 +110081 n := int32(-N)
Nigel Tao267bfc82020-07-05 14:37:57 +100082 for z.Cmp(two128) >= 0 {
Nigel Taod587c9e2020-03-29 22:10:49 +110083 z.Rsh(z, 1)
84 n++
85 }
Nigel Taod587c9e2020-03-29 22:10:49 +110086 hex := fmt.Sprintf("%X", z)
Nigel Tao267bfc82020-07-05 14:37:57 +100087 if len(hex) != 32 {
Nigel Taod587c9e2020-03-29 22:10:49 +110088 return fmt.Errorf("invalid hexadecimal representation %q", hex)
89 }
90
Nigel Tao267bfc82020-07-05 14:37:57 +100091 fmt.Printf(" 0x%s, 0x%s, 0x%s, 0x%s, 0x%04X, // 1e%-04d",
92 hex[24:], hex[16:24], hex[8:16], hex[:8], uint32(n)+bias, e)
Nigel Tao53da68f2020-07-05 13:52:06 +100093 if *detail {
94 fmt.Printf("≈ (0x%s ", e, hex)
Nigel Taod587c9e2020-03-29 22:10:49 +110095 if n >= 0 {
96 fmt.Printf("<< %4d)", +n)
97 } else {
98 fmt.Printf(">> %4d)", -n)
99 }
100 }
101
102 fmt.Println()
103 return nil
104}