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