-
Notifications
You must be signed in to change notification settings - Fork 0
/
signed_math.go
64 lines (48 loc) · 1.37 KB
/
signed_math.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
package main
import (
_ "embed"
"fmt"
"math/big"
)
//go:embed signed_math.move.template
var signed_math_template string
var one *big.Int = big.NewInt(1)
type signedMath struct {
BaseWidth uint
ModuleNameFmt string
Address string
}
type signedMathGenerated struct {
signedMath
BaseTypeName string
BreakPoint string
MaxUnsigned string
MaxPositive string
ModuleName string
TypeName string
UnderlyingUnsignedType string
}
func newSignedMathGenerated(s *signedMath) *signedMathGenerated {
r := &signedMathGenerated{
signedMath: *s,
}
r.BaseTypeName = fmt.Sprintf("u%d", s.BaseWidth)
break_point := big.NewInt(0)
break_point.Lsh(one, s.BaseWidth-1)
r.BreakPoint = break_point.String()
max_unsigned := big.NewInt(0)
max_unsigned.Lsh(one, s.BaseWidth)
max_unsigned.Sub(max_unsigned, one)
r.MaxUnsigned = max_unsigned.String()
max_positive := big.NewInt(0)
max_positive.Lsh(one, s.BaseWidth-1)
max_positive.Sub(max_positive, one)
r.MaxPositive = max_positive.String()
r.ModuleName = fmt.Sprintf(s.ModuleNameFmt, s.BaseWidth)
r.TypeName = fmt.Sprintf("Int%d", s.BaseWidth)
r.UnderlyingUnsignedType = fmt.Sprintf("u%d", s.BaseWidth)
return r
}
func (s signedMathGenerated) GenText() (string, error) {
return GenText(fmt.Sprintf(s.ModuleNameFmt, s.BaseWidth), signed_math_template, s)
}