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