blob: c24d037a4a8759fbc6a2ff91fa4caf51d60212d3 [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 Tao1bc98b62020-07-06 17:01:41 +100019// print-mpb-powers-of-10.go prints the medium-precision (128-bit mantissa)
20// binary (base-2) wuffs_base__private_implementation__powers_of_10 tables.
21//
22// When the approximation to (10 ** N) is not exact, the mantissa is truncated,
23// not rounded to nearest. The base-2 exponent is chosen so that the mantissa's
24// most signficant bit (bit 127) is set.
25//
26// The final uint32_t entry in each row-of-5 is biased by 1214, also known as
27// 0x04BE, which simplifies its use in f64conv-submodule.c.
Nigel Taod587c9e2020-03-29 22:10:49 +110028//
Nigel Tao53da68f2020-07-05 13:52:06 +100029// Usage: go run print-mpb-powers-of-10.go -detail
Nigel Tao1bc98b62020-07-06 17:01:41 +100030//
31// With -detail set, its output should include:
32//
33// 0xF7604B57, 0x014BB630, 0xFE98746D, 0x84A57695, 0x0004,
34// // 1e-326 ≈ (0x84A57695FE98746D014BB630F7604B57 >> 1210)
35//
36// 0x35385E2D, 0x419EA3BD, 0x7E3E9188, 0xA5CED43B, 0x0007,
37// // 1e-325 ≈ (0xA5CED43B7E3E9188419EA3BD35385E2D >> 1207)
38//
39// ...
40//
41// 0x0A3D70A3, 0x3D70A3D7, 0x70A3D70A, 0xA3D70A3D, 0x0438,
42// // 1e-2 ≈ (0xA3D70A3D70A3D70A3D70A3D70A3D70A3 >> 134)
43//
44// 0xCCCCCCCC, 0xCCCCCCCC, 0xCCCCCCCC, 0xCCCCCCCC, 0x043B,
45// // 1e-1 ≈ (0xCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC >> 131)
46//
47// 0x00000000, 0x00000000, 0x00000000, 0x80000000, 0x043F,
48// // 1e0 ≈ (0x80000000000000000000000000000000 >> 127)
49//
50// 0x00000000, 0x00000000, 0x00000000, 0xA0000000, 0x0442,
51// // 1e1 ≈ (0xA0000000000000000000000000000000 >> 124)
52//
53// 0x00000000, 0x00000000, 0x00000000, 0xC8000000, 0x0445,
54// // 1e2 ≈ (0xC8000000000000000000000000000000 >> 121)
55//
56// ...
57//
58// 0x51E513DA, 0x2CD2CC65, 0x35D63F73, 0xB201833B, 0x0841,
59// // 1e309 ≈ (0xB201833B35D63F732CD2CC6551E513DA << 899)
60//
61// 0xA65E58D1, 0xF8077F7E, 0x034BCF4F, 0xDE81E40A, 0x0844,
62// // 1e310 ≈ (0xDE81E40A034BCF4FF8077F7EA65E58D1 << 902)
Nigel Taod587c9e2020-03-29 22:10:49 +110063
64import (
65 "flag"
66 "fmt"
67 "math/big"
68 "os"
69)
70
71var (
Nigel Tao53da68f2020-07-05 13:52:06 +100072 detail = flag.Bool("detail", false, "whether to print detailed comments")
Nigel Taod587c9e2020-03-29 22:10:49 +110073)
74
75func main() {
76 if err := main1(); err != nil {
77 os.Stderr.WriteString(err.Error() + "\n")
78 os.Exit(1)
79 }
80}
81
82func main1() error {
83 flag.Parse()
84
Nigel Tao267bfc82020-07-05 14:37:57 +100085 const count = 1 + (+310 - -326)
Nigel Taod587c9e2020-03-29 22:10:49 +110086 fmt.Printf("static const uint32_t "+
Nigel Tao267bfc82020-07-05 14:37:57 +100087 "wuffs_base__private_implementation__powers_of_10[%d] = {\n", 5*count)
Nigel Tao53da68f2020-07-05 13:52:06 +100088 for e := -326; e <= +310; e++ {
Nigel Taod587c9e2020-03-29 22:10:49 +110089 if err := do(e); err != nil {
90 return err
91 }
92 }
93 fmt.Printf("};\n\n")
94
Nigel Taod587c9e2020-03-29 22:10:49 +110095 return nil
96}
97
98var (
Nigel Tao267bfc82020-07-05 14:37:57 +100099 one = big.NewInt(1)
100 ten = big.NewInt(10)
101 two128 = big.NewInt(0).Lsh(one, 128)
Nigel Taod587c9e2020-03-29 22:10:49 +1100102)
103
Nigel Tao267bfc82020-07-05 14:37:57 +1000104// N is large enough so that (1<<N) is easily bigger than 1e310.
Nigel Taod587c9e2020-03-29 22:10:49 +1100105const N = 2048
106
Nigel Tao267bfc82020-07-05 14:37:57 +1000107// 1214 is 1023 + 191. 1023 is the bias for IEEE 754 double-precision floating
108// point. 191 is ((3 * 64) - 1) and we work with multiples-of-64-bit mantissas.
109const bias = 1214
110
Nigel Taod587c9e2020-03-29 22:10:49 +1100111func do(e int) error {
112 z := big.NewInt(0).Lsh(one, N)
113 if e >= 0 {
114 exp := big.NewInt(0).Exp(ten, big.NewInt(int64(+e)), nil)
115 z.Mul(z, exp)
116 } else {
117 exp := big.NewInt(0).Exp(ten, big.NewInt(int64(-e)), nil)
118 z.Div(z, exp)
119 }
120
Nigel Taod587c9e2020-03-29 22:10:49 +1100121 n := int32(-N)
Nigel Tao267bfc82020-07-05 14:37:57 +1000122 for z.Cmp(two128) >= 0 {
Nigel Taod587c9e2020-03-29 22:10:49 +1100123 z.Rsh(z, 1)
124 n++
125 }
Nigel Taod587c9e2020-03-29 22:10:49 +1100126 hex := fmt.Sprintf("%X", z)
Nigel Tao267bfc82020-07-05 14:37:57 +1000127 if len(hex) != 32 {
Nigel Taod587c9e2020-03-29 22:10:49 +1100128 return fmt.Errorf("invalid hexadecimal representation %q", hex)
129 }
130
Nigel Tao267bfc82020-07-05 14:37:57 +1000131 fmt.Printf(" 0x%s, 0x%s, 0x%s, 0x%s, 0x%04X, // 1e%-04d",
132 hex[24:], hex[16:24], hex[8:16], hex[:8], uint32(n)+bias, e)
Nigel Tao53da68f2020-07-05 13:52:06 +1000133 if *detail {
Nigel Tao1bc98b62020-07-06 17:01:41 +1000134 fmt.Printf(" ≈ (0x%s ", hex)
Nigel Taod587c9e2020-03-29 22:10:49 +1100135 if n >= 0 {
136 fmt.Printf("<< %4d)", +n)
137 } else {
138 fmt.Printf(">> %4d)", -n)
139 }
140 }
141
142 fmt.Println()
143 return nil
144}