blob: 82ec20e7cfbca8e717497d690b8f07d384f708d5 [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//
Nigel Taof2be4dd2020-09-15 22:12:53 +100033// 0x79F8E056, 0xA5D3B6D4, 0x06306BAB, 0x8FD0C162, 0x0043,
34// // 1e-307 ≈ (0x8FD0C16206306BABA5D3B6D479F8E056 >> 1147)
Nigel Tao1bc98b62020-07-06 17:01:41 +100035//
Nigel Taof2be4dd2020-09-15 22:12:53 +100036// 0x9877186C, 0x8F48A489, 0x87BC8696, 0xB3C4F1BA, 0x0046,
37// // 1e-306 ≈ (0xB3C4F1BA87BC86968F48A4899877186C >> 1144)
Nigel Tao1bc98b62020-07-06 17:01:41 +100038//
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//
Nigel Taof2be4dd2020-09-15 22:12:53 +100058// 0xBFFF5A74, 0x5C68F256, 0x49EE8C70, 0xA81F3014, 0x07F8,
59// // 1e287 ≈ (0xA81F301449EE8C705C68F256BFFF5A74 << 826)
Nigel Tao1bc98b62020-07-06 17:01:41 +100060//
Nigel Taof2be4dd2020-09-15 22:12:53 +100061// 0x6FFF3111, 0x73832EEC, 0x5C6A2F8C, 0xD226FC19, 0x07FB,
62// // 1e288 ≈ (0xD226FC195C6A2F8C73832EEC6FFF3111 << 829)
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 Taof2be4dd2020-09-15 22:12:53 +100085 const count = 1 + (+288 - -307)
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 Taof2be4dd2020-09-15 22:12:53 +100088 for e := -307; e <= +288; 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}