-
Notifications
You must be signed in to change notification settings - Fork 0
/
double_width_unsigned.go
152 lines (121 loc) · 3.86 KB
/
double_width_unsigned.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
_ "embed"
"fmt"
"math/big"
"os"
"strings"
"github.com/spf13/cobra"
)
//go:embed double_width_unsigned.move.template
var double_width_unsigned_template string
type doubleWidthUnsigned struct {
DesiredWidth uint
Address string
DoTest bool
Args string
Version string
}
type doubleWidthUnsignedGenerated struct {
doubleWidthUnsigned
BaseWidth uint
HalfWidth uint
BaseTypeName string
BreakPoint string
MaxUnsigned string
MaxPositive string
ModuleName string
TypeName string
Lower1s string
Upper1s string
MaxShiftSize uint
UnrolledForLeadingZeros []unrolledUpper
}
// unrolledUpper contains Ones that is ((1 << Width + 1) - 1) (Width number of ones)
type unrolledUpper struct {
// Wdith is the number of 1s in the binary presentation.
Width uint
// Ones is Width ones together.
Ones string
}
// newUnrolledUpper creates an unrolled
func newUnrolledUpper(n uint, w uint) unrolledUpper {
return unrolledUpper{
Width: n,
Ones: big.NewInt(0).Lsh(big.NewInt(0).Sub(big.NewInt(0).Lsh(one, n), one), w-n).String(),
}
}
func getDoubleWidthModuleName(desiredWith uint) string {
return fmt.Sprintf("uint%d", desiredWith)
}
func newDoubleWidthUnsignedGenerated(s *doubleWidthUnsigned) *doubleWidthUnsignedGenerated {
r := &doubleWidthUnsignedGenerated{
doubleWidthUnsigned: *s,
}
r.BaseWidth = r.DesiredWidth / 2
r.HalfWidth = r.BaseWidth / 2
r.BaseTypeName = fmt.Sprintf("u%d", r.BaseWidth)
break_point := big.NewInt(0)
break_point.Lsh(one, r.BaseWidth-1)
r.BreakPoint = break_point.String()
max_unsigned := big.NewInt(0)
max_unsigned.Lsh(one, r.BaseWidth)
max_unsigned.Sub(max_unsigned, one)
r.MaxUnsigned = max_unsigned.String()
max_positive := big.NewInt(0)
max_positive.Lsh(one, r.BaseWidth-1)
max_positive.Sub(max_positive, one)
r.MaxPositive = max_positive.String()
r.ModuleName = getDoubleWidthModuleName(r.DesiredWidth)
r.TypeName = fmt.Sprintf("Uint%d", r.DesiredWidth)
lower_1s := big.NewInt(1)
lower_1s = lower_1s.Lsh(lower_1s, r.HalfWidth)
lower_1s = lower_1s.Sub(lower_1s, one)
r.Lower1s = lower_1s.String()
upper_1s := big.NewInt(0)
upper_1s = upper_1s.Lsh(lower_1s, r.HalfWidth)
r.Upper1s = upper_1s.String()
r.MaxShiftSize = r.DesiredWidth - 1
for n := r.BaseWidth >> 1; n > 0; n = n >> 1 {
r.UnrolledForLeadingZeros = append(r.UnrolledForLeadingZeros, newUnrolledUpper(n, r.BaseWidth))
}
return r
}
func (s doubleWidthUnsignedGenerated) GenText() (string, error) {
return GenText(s.ModuleName, double_width_unsigned_template, s)
}
func newDoubleWidthUnsignedCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "double-width",
Short: "generate double width unsigned integer (for example, Uint16 based on u8)",
Args: cobra.NoArgs,
}
output := "./sources/double_width_unsigned.move"
address := "more_math"
widths := newIntWidth([]uint{16, 32, 64, 128, 256})
doTest := false
cmd.Flags().StringVarP(&output, "out", "o", output, "output file. this should be the in the sources folder of your move package module")
cmd.MarkFlagFilename("out")
cmd.Flags().StringVarP(&address, "address", "p", address, "(named) address")
cmd.Flags().VarP(widths, "width", "w", fmt.Sprintf("int widths, must be one of %s. can be specified multiple times.", sliceToString(widths.permitted)))
cmd.Flags().BoolVarP(&doTest, "do-test", "t", false, "generate test code")
cmd.Run = func(cmd *cobra.Command, args []string) {
allTexts := []string{}
for _, w := range widths.values {
s := doubleWidthUnsigned{
DesiredWidth: w,
Address: address,
DoTest: doTest,
Args: strings.Join(os.Args[1:], " "),
Version: version,
}
code, err := newDoubleWidthUnsignedGenerated(&s).GenText()
if err != nil {
panic(err)
}
allTexts = append(allTexts, code)
}
os.WriteFile(output, []byte(strings.Join(allTexts, "\n")), 0o666)
}
return cmd
}