Nigel Tao | 499ca13 | 2020-03-09 14:25:30 +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 | |
Nigel Tao | 788479d | 2021-08-22 10:52:51 +1000 | [diff] [blame] | 15 | //go:build ignore |
Nigel Tao | 499ca13 | 2020-03-09 14:25:30 +1100 | [diff] [blame] | 16 | // +build ignore |
| 17 | |
| 18 | package main |
| 19 | |
| 20 | // print-hpd-left-shift.go prints the |
| 21 | // wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits |
| 22 | // tables. |
| 23 | // |
| 24 | // Usage: go run print-hpd-left-shift.go -comments |
| 25 | |
| 26 | import ( |
| 27 | "flag" |
| 28 | "fmt" |
| 29 | "math" |
| 30 | "math/big" |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 31 | "os" |
Nigel Tao | 499ca13 | 2020-03-09 14:25:30 +1100 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | var ( |
| 35 | comments = flag.Bool("comments", false, "whether to print comments") |
| 36 | ) |
| 37 | |
| 38 | // powerOf5 return "5 ** n" as a string. |
| 39 | func powerOf5(n int64) string { |
| 40 | x := big.NewInt(5) |
| 41 | x.Exp(x, big.NewInt(n), nil) |
| 42 | return x.String() |
| 43 | } |
| 44 | |
| 45 | func ellipsize(s string) string { |
| 46 | if len(s) <= 16 { |
| 47 | return s |
| 48 | } |
| 49 | return s[:8] + "..." + s[len(s)-5:] |
| 50 | } |
| 51 | |
| 52 | func main() { |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 53 | if err := main1(); err != nil { |
| 54 | os.Stderr.WriteString(err.Error() + "\n") |
| 55 | os.Exit(1) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func main1() error { |
Nigel Tao | 499ca13 | 2020-03-09 14:25:30 +1100 | [diff] [blame] | 60 | flag.Parse() |
| 61 | |
| 62 | const WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL = 60 |
| 63 | const log2log10 = math.Ln2 / math.Ln10 |
| 64 | data := []byte(nil) |
| 65 | |
| 66 | fmt.Printf("static const uint16_t " + |
| 67 | "wuffs_base__private_implementation__hpd_left_shift[65] = {\n") |
| 68 | fmt.Printf(" 0x0000,") |
| 69 | if *comments { |
| 70 | fmt.Printf("// i= 0\n") |
| 71 | } |
| 72 | for i := int64(1); i <= WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL; i++ { |
| 73 | offset := int64(len(data)) |
| 74 | if offset > 0x07FF { |
| 75 | panic("offset requires more than 11 bits") |
| 76 | } |
| 77 | numNewDigits := int64(log2log10*float64(i)) + 1 |
| 78 | if numNewDigits > 31 { |
| 79 | panic("numNewDigits requires more than 5 bits") |
| 80 | } |
| 81 | code := (numNewDigits << 11) | offset |
| 82 | |
| 83 | p := powerOf5(i) |
| 84 | data = append(data, p...) |
| 85 | fmt.Printf(" 0x%04X,", code) |
| 86 | if *comments { |
| 87 | fmt.Printf(" // i=%2d, num_new_digits=%2d, offset=0x%04X, 5**i=%s\n", |
| 88 | i, numNewDigits, offset, ellipsize(p)) |
| 89 | } else if i&3 == 3 { |
| 90 | fmt.Println() |
| 91 | } |
| 92 | } |
| 93 | for i := 1 + WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL; i < 65; i++ { |
| 94 | fmt.Printf(" 0x%04X,", len(data)) |
| 95 | if *comments { |
| 96 | fmt.Printf(" // i=%2d\n", i) |
| 97 | } |
| 98 | } |
| 99 | fmt.Printf("};\n\n") |
| 100 | if len(data) > 0x07FF { |
| 101 | panic("offset requires more than 11 bits") |
| 102 | } |
| 103 | |
| 104 | fmt.Printf("static const uint8_t "+ |
| 105 | "wuffs_base__private_implementation__powers_of_5[0x%04X] = {\n", len(data)) |
| 106 | for i, x := range data { |
| 107 | if (i & 15) == 0 { |
| 108 | fmt.Printf(" ") |
| 109 | } |
| 110 | fmt.Printf("%d, ", x&0x0F) |
| 111 | if (i & 15) == 15 { |
| 112 | if *comments { |
| 113 | fmt.Printf(" // offset=0x%04X\n", i&^15) |
| 114 | } else { |
| 115 | fmt.Println() |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | if *comments { |
| 120 | fmt.Printf(" // offset=0x%04X\n", len(data)&^15) |
| 121 | } |
| 122 | fmt.Printf("};\n") |
Nigel Tao | d587c9e | 2020-03-29 22:10:49 +1100 | [diff] [blame] | 123 | return nil |
Nigel Tao | 499ca13 | 2020-03-09 14:25:30 +1100 | [diff] [blame] | 124 | } |