blob: ac3a7d450b401b939c267c5ebdf1d605360f0988 [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
Nigel Tao788479d2021-08-22 10:52:51 +100015//go:build ignore
Nigel Taod587c9e2020-03-29 22:10:49 +110016// +build ignore
17
18package main
19
Nigel Tao1bc98b62020-07-06 17:01:41 +100020// print-mpb-powers-of-10.go prints the medium-precision (128-bit mantissa)
21// binary (base-2) wuffs_base__private_implementation__powers_of_10 tables.
22//
23// When the approximation to (10 ** N) is not exact, the mantissa is truncated,
Nigel Taoafe7f272020-09-23 15:52:13 +100024// not rounded to nearest. The base-2 exponent (an implicit third column) is
25// chosen so that the mantissa's most signficant bit (bit 127) is set.
Nigel Taod587c9e2020-03-29 22:10:49 +110026//
Nigel Tao53da68f2020-07-05 13:52:06 +100027// Usage: go run print-mpb-powers-of-10.go -detail
Nigel Tao1bc98b62020-07-06 17:01:41 +100028//
29// With -detail set, its output should include:
30//
Nigel Taoafe7f272020-09-23 15:52:13 +100031// {0xA5D3B6D479F8E056, 0x8FD0C16206306BAB},
Nigel Taof2be4dd2020-09-15 22:12:53 +100032// // 1e-307 ≈ (0x8FD0C16206306BABA5D3B6D479F8E056 >> 1147)
Nigel Tao1bc98b62020-07-06 17:01:41 +100033//
Nigel Taoafe7f272020-09-23 15:52:13 +100034// {0x8F48A4899877186C, 0xB3C4F1BA87BC8696},
Nigel Taof2be4dd2020-09-15 22:12:53 +100035// // 1e-306 ≈ (0xB3C4F1BA87BC86968F48A4899877186C >> 1144)
Nigel Tao1bc98b62020-07-06 17:01:41 +100036//
37// ...
38//
Nigel Taoafe7f272020-09-23 15:52:13 +100039// {0x3D70A3D70A3D70A3, 0xA3D70A3D70A3D70A},
Nigel Tao1bc98b62020-07-06 17:01:41 +100040// // 1e-2 ≈ (0xA3D70A3D70A3D70A3D70A3D70A3D70A3 >> 134)
41//
Nigel Taoafe7f272020-09-23 15:52:13 +100042// {0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC},
Nigel Tao1bc98b62020-07-06 17:01:41 +100043// // 1e-1 ≈ (0xCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC >> 131)
44//
Nigel Taoafe7f272020-09-23 15:52:13 +100045// {0x0000000000000000, 0x8000000000000000},
Nigel Tao1bc98b62020-07-06 17:01:41 +100046// // 1e0 ≈ (0x80000000000000000000000000000000 >> 127)
47//
Nigel Taoafe7f272020-09-23 15:52:13 +100048// {0x0000000000000000, 0xA000000000000000},
Nigel Tao1bc98b62020-07-06 17:01:41 +100049// // 1e1 ≈ (0xA0000000000000000000000000000000 >> 124)
50//
Nigel Taoafe7f272020-09-23 15:52:13 +100051// {0x0000000000000000, 0xC800000000000000},
Nigel Tao1bc98b62020-07-06 17:01:41 +100052// // 1e2 ≈ (0xC8000000000000000000000000000000 >> 121)
53//
54// ...
55//
Nigel Taoafe7f272020-09-23 15:52:13 +100056// {0x5C68F256BFFF5A74, 0xA81F301449EE8C70},
Nigel Taof2be4dd2020-09-15 22:12:53 +100057// // 1e287 ≈ (0xA81F301449EE8C705C68F256BFFF5A74 << 826)
Nigel Tao1bc98b62020-07-06 17:01:41 +100058//
Nigel Taoafe7f272020-09-23 15:52:13 +100059// {0x73832EEC6FFF3111, 0xD226FC195C6A2F8C},
Nigel Taof2be4dd2020-09-15 22:12:53 +100060// // 1e288 ≈ (0xD226FC195C6A2F8C73832EEC6FFF3111 << 829)
Nigel Taod587c9e2020-03-29 22:10:49 +110061
62import (
63 "flag"
64 "fmt"
65 "math/big"
66 "os"
67)
68
69var (
Nigel Tao53da68f2020-07-05 13:52:06 +100070 detail = flag.Bool("detail", false, "whether to print detailed comments")
Nigel Taod587c9e2020-03-29 22:10:49 +110071)
72
73func main() {
74 if err := main1(); err != nil {
75 os.Stderr.WriteString(err.Error() + "\n")
76 os.Exit(1)
77 }
78}
79
80func main1() error {
81 flag.Parse()
82
Nigel Taof2be4dd2020-09-15 22:12:53 +100083 const count = 1 + (+288 - -307)
Nigel Taoafe7f272020-09-23 15:52:13 +100084 fmt.Printf("static const uint64_t "+
85 "wuffs_base__private_implementation__powers_of_10[%d][2] = {\n", count)
Nigel Taof2be4dd2020-09-15 22:12:53 +100086 for e := -307; e <= +288; e++ {
Nigel Taod587c9e2020-03-29 22:10:49 +110087 if err := do(e); err != nil {
88 return err
89 }
90 }
91 fmt.Printf("};\n\n")
92
Nigel Taod587c9e2020-03-29 22:10:49 +110093 return nil
94}
95
96var (
Nigel Tao267bfc82020-07-05 14:37:57 +100097 one = big.NewInt(1)
98 ten = big.NewInt(10)
99 two128 = big.NewInt(0).Lsh(one, 128)
Nigel Taod587c9e2020-03-29 22:10:49 +1100100)
101
Nigel Tao267bfc82020-07-05 14:37:57 +1000102// N is large enough so that (1<<N) is easily bigger than 1e310.
Nigel Taod587c9e2020-03-29 22:10:49 +1100103const N = 2048
104
Nigel Tao267bfc82020-07-05 14:37:57 +1000105// 1214 is 1023 + 191. 1023 is the bias for IEEE 754 double-precision floating
106// point. 191 is ((3 * 64) - 1) and we work with multiples-of-64-bit mantissas.
107const bias = 1214
108
Nigel Taod587c9e2020-03-29 22:10:49 +1100109func do(e int) error {
110 z := big.NewInt(0).Lsh(one, N)
111 if e >= 0 {
112 exp := big.NewInt(0).Exp(ten, big.NewInt(int64(+e)), nil)
113 z.Mul(z, exp)
114 } else {
115 exp := big.NewInt(0).Exp(ten, big.NewInt(int64(-e)), nil)
116 z.Div(z, exp)
117 }
118
Nigel Taod587c9e2020-03-29 22:10:49 +1100119 n := int32(-N)
Nigel Tao267bfc82020-07-05 14:37:57 +1000120 for z.Cmp(two128) >= 0 {
Nigel Taod587c9e2020-03-29 22:10:49 +1100121 z.Rsh(z, 1)
122 n++
123 }
Nigel Taod587c9e2020-03-29 22:10:49 +1100124 hex := fmt.Sprintf("%X", z)
Nigel Tao267bfc82020-07-05 14:37:57 +1000125 if len(hex) != 32 {
Nigel Taod587c9e2020-03-29 22:10:49 +1100126 return fmt.Errorf("invalid hexadecimal representation %q", hex)
127 }
128
Nigel Taob6d85522020-09-23 15:21:47 +1000129 // Confirm that the linear approximation to the biased-value-of-n is
130 // correct for this particular value of e.
131 approxN := uint32(((217706 * e) >> 16) + 1087)
132 biasedN := bias + uint32(n)
133 if approxN != biasedN {
134 return fmt.Errorf("biased-n approximation: have %d, want %d", approxN, biasedN)
135 }
136
Nigel Taoafe7f272020-09-23 15:52:13 +1000137 fmt.Printf(" {0x%s, 0x%s}, // 1e%-04d",
138 hex[16:], hex[:16], e)
Nigel Tao53da68f2020-07-05 13:52:06 +1000139 if *detail {
Nigel Tao1bc98b62020-07-06 17:01:41 +1000140 fmt.Printf(" ≈ (0x%s ", hex)
Nigel Taod587c9e2020-03-29 22:10:49 +1100141 if n >= 0 {
142 fmt.Printf("<< %4d)", +n)
143 } else {
144 fmt.Printf(">> %4d)", -n)
145 }
146 }
147
148 fmt.Println()
149 return nil
150}