blob: fece9bb2d43f0ae0a63d1f58d50966496346d55a [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,
Nigel Taoafe7f272020-09-23 15:52:13 +100023// not rounded to nearest. The base-2 exponent (an implicit third column) is
24// chosen so that the mantissa's most signficant bit (bit 127) is set.
Nigel Taod587c9e2020-03-29 22:10:49 +110025//
Nigel Tao53da68f2020-07-05 13:52:06 +100026// Usage: go run print-mpb-powers-of-10.go -detail
Nigel Tao1bc98b62020-07-06 17:01:41 +100027//
28// With -detail set, its output should include:
29//
Nigel Taoafe7f272020-09-23 15:52:13 +100030// {0xA5D3B6D479F8E056, 0x8FD0C16206306BAB},
Nigel Taof2be4dd2020-09-15 22:12:53 +100031// // 1e-307 ≈ (0x8FD0C16206306BABA5D3B6D479F8E056 >> 1147)
Nigel Tao1bc98b62020-07-06 17:01:41 +100032//
Nigel Taoafe7f272020-09-23 15:52:13 +100033// {0x8F48A4899877186C, 0xB3C4F1BA87BC8696},
Nigel Taof2be4dd2020-09-15 22:12:53 +100034// // 1e-306 ≈ (0xB3C4F1BA87BC86968F48A4899877186C >> 1144)
Nigel Tao1bc98b62020-07-06 17:01:41 +100035//
36// ...
37//
Nigel Taoafe7f272020-09-23 15:52:13 +100038// {0x3D70A3D70A3D70A3, 0xA3D70A3D70A3D70A},
Nigel Tao1bc98b62020-07-06 17:01:41 +100039// // 1e-2 ≈ (0xA3D70A3D70A3D70A3D70A3D70A3D70A3 >> 134)
40//
Nigel Taoafe7f272020-09-23 15:52:13 +100041// {0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC},
Nigel Tao1bc98b62020-07-06 17:01:41 +100042// // 1e-1 ≈ (0xCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC >> 131)
43//
Nigel Taoafe7f272020-09-23 15:52:13 +100044// {0x0000000000000000, 0x8000000000000000},
Nigel Tao1bc98b62020-07-06 17:01:41 +100045// // 1e0 ≈ (0x80000000000000000000000000000000 >> 127)
46//
Nigel Taoafe7f272020-09-23 15:52:13 +100047// {0x0000000000000000, 0xA000000000000000},
Nigel Tao1bc98b62020-07-06 17:01:41 +100048// // 1e1 ≈ (0xA0000000000000000000000000000000 >> 124)
49//
Nigel Taoafe7f272020-09-23 15:52:13 +100050// {0x0000000000000000, 0xC800000000000000},
Nigel Tao1bc98b62020-07-06 17:01:41 +100051// // 1e2 ≈ (0xC8000000000000000000000000000000 >> 121)
52//
53// ...
54//
Nigel Taoafe7f272020-09-23 15:52:13 +100055// {0x5C68F256BFFF5A74, 0xA81F301449EE8C70},
Nigel Taof2be4dd2020-09-15 22:12:53 +100056// // 1e287 ≈ (0xA81F301449EE8C705C68F256BFFF5A74 << 826)
Nigel Tao1bc98b62020-07-06 17:01:41 +100057//
Nigel Taoafe7f272020-09-23 15:52:13 +100058// {0x73832EEC6FFF3111, 0xD226FC195C6A2F8C},
Nigel Taof2be4dd2020-09-15 22:12:53 +100059// // 1e288 ≈ (0xD226FC195C6A2F8C73832EEC6FFF3111 << 829)
Nigel Taod587c9e2020-03-29 22:10:49 +110060
61import (
62 "flag"
63 "fmt"
64 "math/big"
65 "os"
66)
67
68var (
Nigel Tao53da68f2020-07-05 13:52:06 +100069 detail = flag.Bool("detail", false, "whether to print detailed comments")
Nigel Taod587c9e2020-03-29 22:10:49 +110070)
71
72func main() {
73 if err := main1(); err != nil {
74 os.Stderr.WriteString(err.Error() + "\n")
75 os.Exit(1)
76 }
77}
78
79func main1() error {
80 flag.Parse()
81
Nigel Taof2be4dd2020-09-15 22:12:53 +100082 const count = 1 + (+288 - -307)
Nigel Taoafe7f272020-09-23 15:52:13 +100083 fmt.Printf("static const uint64_t "+
84 "wuffs_base__private_implementation__powers_of_10[%d][2] = {\n", count)
Nigel Taof2be4dd2020-09-15 22:12:53 +100085 for e := -307; e <= +288; e++ {
Nigel Taod587c9e2020-03-29 22:10:49 +110086 if err := do(e); err != nil {
87 return err
88 }
89 }
90 fmt.Printf("};\n\n")
91
Nigel Taod587c9e2020-03-29 22:10:49 +110092 return nil
93}
94
95var (
Nigel Tao267bfc82020-07-05 14:37:57 +100096 one = big.NewInt(1)
97 ten = big.NewInt(10)
98 two128 = big.NewInt(0).Lsh(one, 128)
Nigel Taod587c9e2020-03-29 22:10:49 +110099)
100
Nigel Tao267bfc82020-07-05 14:37:57 +1000101// N is large enough so that (1<<N) is easily bigger than 1e310.
Nigel Taod587c9e2020-03-29 22:10:49 +1100102const N = 2048
103
Nigel Tao267bfc82020-07-05 14:37:57 +1000104// 1214 is 1023 + 191. 1023 is the bias for IEEE 754 double-precision floating
105// point. 191 is ((3 * 64) - 1) and we work with multiples-of-64-bit mantissas.
106const bias = 1214
107
Nigel Taod587c9e2020-03-29 22:10:49 +1100108func do(e int) error {
109 z := big.NewInt(0).Lsh(one, N)
110 if e >= 0 {
111 exp := big.NewInt(0).Exp(ten, big.NewInt(int64(+e)), nil)
112 z.Mul(z, exp)
113 } else {
114 exp := big.NewInt(0).Exp(ten, big.NewInt(int64(-e)), nil)
115 z.Div(z, exp)
116 }
117
Nigel Taod587c9e2020-03-29 22:10:49 +1100118 n := int32(-N)
Nigel Tao267bfc82020-07-05 14:37:57 +1000119 for z.Cmp(two128) >= 0 {
Nigel Taod587c9e2020-03-29 22:10:49 +1100120 z.Rsh(z, 1)
121 n++
122 }
Nigel Taod587c9e2020-03-29 22:10:49 +1100123 hex := fmt.Sprintf("%X", z)
Nigel Tao267bfc82020-07-05 14:37:57 +1000124 if len(hex) != 32 {
Nigel Taod587c9e2020-03-29 22:10:49 +1100125 return fmt.Errorf("invalid hexadecimal representation %q", hex)
126 }
127
Nigel Taob6d85522020-09-23 15:21:47 +1000128 // Confirm that the linear approximation to the biased-value-of-n is
129 // correct for this particular value of e.
130 approxN := uint32(((217706 * e) >> 16) + 1087)
131 biasedN := bias + uint32(n)
132 if approxN != biasedN {
133 return fmt.Errorf("biased-n approximation: have %d, want %d", approxN, biasedN)
134 }
135
Nigel Taoafe7f272020-09-23 15:52:13 +1000136 fmt.Printf(" {0x%s, 0x%s}, // 1e%-04d",
137 hex[16:], hex[:16], e)
Nigel Tao53da68f2020-07-05 13:52:06 +1000138 if *detail {
Nigel Tao1bc98b62020-07-06 17:01:41 +1000139 fmt.Printf(" ≈ (0x%s ", hex)
Nigel Taod587c9e2020-03-29 22:10:49 +1100140 if n >= 0 {
141 fmt.Printf("<< %4d)", +n)
142 } else {
143 fmt.Printf(">> %4d)", -n)
144 }
145 }
146
147 fmt.Println()
148 return nil
149}