Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 1 | // 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 | |
| 17 | package main |
| 18 | |
Nigel Tao | 1bc98b6 | 2020-07-06 17:01:41 +1000 | [diff] [blame^] | 19 | // 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 Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 28 | // |
Nigel Tao | 53da68f | 2020-07-05 13:52:06 +1000 | [diff] [blame] | 29 | // Usage: go run print-mpb-powers-of-10.go -detail |
Nigel Tao | 1bc98b6 | 2020-07-06 17:01:41 +1000 | [diff] [blame^] | 30 | // |
| 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 Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 63 | |
| 64 | import ( |
| 65 | "flag" |
| 66 | "fmt" |
| 67 | "math/big" |
| 68 | "os" |
| 69 | ) |
| 70 | |
| 71 | var ( |
Nigel Tao | 53da68f | 2020-07-05 13:52:06 +1000 | [diff] [blame] | 72 | detail = flag.Bool("detail", false, "whether to print detailed comments") |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 73 | ) |
| 74 | |
| 75 | func main() { |
| 76 | if err := main1(); err != nil { |
| 77 | os.Stderr.WriteString(err.Error() + "\n") |
| 78 | os.Exit(1) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func main1() error { |
| 83 | flag.Parse() |
| 84 | |
Nigel Tao | 267bfc8 | 2020-07-05 14:37:57 +1000 | [diff] [blame] | 85 | const count = 1 + (+310 - -326) |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 86 | fmt.Printf("static const uint32_t "+ |
Nigel Tao | 267bfc8 | 2020-07-05 14:37:57 +1000 | [diff] [blame] | 87 | "wuffs_base__private_implementation__powers_of_10[%d] = {\n", 5*count) |
Nigel Tao | 53da68f | 2020-07-05 13:52:06 +1000 | [diff] [blame] | 88 | for e := -326; e <= +310; e++ { |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 89 | if err := do(e); err != nil { |
| 90 | return err |
| 91 | } |
| 92 | } |
| 93 | fmt.Printf("};\n\n") |
| 94 | |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 95 | return nil |
| 96 | } |
| 97 | |
| 98 | var ( |
Nigel Tao | 267bfc8 | 2020-07-05 14:37:57 +1000 | [diff] [blame] | 99 | one = big.NewInt(1) |
| 100 | ten = big.NewInt(10) |
| 101 | two128 = big.NewInt(0).Lsh(one, 128) |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 102 | ) |
| 103 | |
Nigel Tao | 267bfc8 | 2020-07-05 14:37:57 +1000 | [diff] [blame] | 104 | // N is large enough so that (1<<N) is easily bigger than 1e310. |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 105 | const N = 2048 |
| 106 | |
Nigel Tao | 267bfc8 | 2020-07-05 14:37:57 +1000 | [diff] [blame] | 107 | // 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. |
| 109 | const bias = 1214 |
| 110 | |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 111 | func 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 Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 121 | n := int32(-N) |
Nigel Tao | 267bfc8 | 2020-07-05 14:37:57 +1000 | [diff] [blame] | 122 | for z.Cmp(two128) >= 0 { |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 123 | z.Rsh(z, 1) |
| 124 | n++ |
| 125 | } |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 126 | hex := fmt.Sprintf("%X", z) |
Nigel Tao | 267bfc8 | 2020-07-05 14:37:57 +1000 | [diff] [blame] | 127 | if len(hex) != 32 { |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 128 | return fmt.Errorf("invalid hexadecimal representation %q", hex) |
| 129 | } |
| 130 | |
Nigel Tao | 267bfc8 | 2020-07-05 14:37:57 +1000 | [diff] [blame] | 131 | 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 Tao | 53da68f | 2020-07-05 13:52:06 +1000 | [diff] [blame] | 133 | if *detail { |
Nigel Tao | 1bc98b6 | 2020-07-06 17:01:41 +1000 | [diff] [blame^] | 134 | fmt.Printf(" ≈ (0x%s ", hex) |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 135 | 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 | } |