-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathyul64.go
183 lines (145 loc) · 3.55 KB
/
yul64.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package slow
import "github.com/holiman/uint256"
// These are type-safe pure functions *styled to translate to yul*, to use uint256 variables for 64 bit math.
// U64 is like a Go uint64, always within range, but represented as uint256 in memory with 0 padding.
type U64 uint256.Int
func (v U64) val() uint64 {
return (*uint256.Int)(&v).Uint64()
}
func byteToU256(v uint8) U256 {
return *uint256.NewInt(uint64(v))
}
func byteToU64(v uint8) U64 {
return U64(byteToU256(v))
}
func shortToU64(v uint16) U64 {
return U64(*uint256.NewInt(uint64(v)))
}
func shortToU256(v uint16) U256 {
return *uint256.NewInt(uint64(v))
}
// nolint:unused
func longToU256(v uint64) U256 {
return *uint256.NewInt(v)
}
func u256ToU64(v U256) U64 {
return U64(and(v, U256(u64Mask())))
}
func u64ToU256(v U64) U256 {
return U256(v)
}
func u64Mask() U64 { // max uint64
return U64(shr(byteToU256(192), not(U256{}))) // 256-64 = 192
}
func u32Mask() U64 {
return U64(shr(byteToU256(224), not(U256{}))) // 256-32 = 224
}
func mask32Signed64(v U64) U64 {
return signExtend64(and64(v, u32Mask()), byteToU64(31))
}
func u64Mod() U256 { // 1 << 64
return shl(byteToU256(64), byteToU256(1))
}
func u64TopBit() U256 { // 1 << 63
return shl(byteToU256(63), byteToU256(1))
}
func signExtend64(v U64, bit U64) U64 {
switch and(U256(v), shl(U256(bit), byteToU256(1))) {
case U256{}:
// fill with zeroes, by masking
return U64(and(U256(v), shr(sub(byteToU256(63), U256(bit)), U256(u64Mask()))))
default:
// fill with ones, by or-ing
return U64(or(U256(v), shl(U256(bit), shr(U256(bit), U256(u64Mask())))))
}
}
func signExtend64To256(v U64) U256 {
switch and(U256(v), u64TopBit()) {
case U256{}:
return U256(v)
default:
return or(shl(byteToU256(64), not(U256{})), U256(v))
}
}
func add64(x, y U64) (out U64) {
out = U64(mod(add(U256(x), U256(y)), u64Mod()))
return
}
func sub64(x, y U64) (out U64) {
out = U64(mod(sub(U256(x), U256(y)), u64Mod()))
return
}
func mul64(x, y U64) (out U64) {
out = u256ToU64(mul(U256(x), U256(y)))
return
}
func div64(x, y U64) (out U64) {
out = u256ToU64(div(U256(x), U256(y)))
return
}
func sdiv64(x, y U64) (out U64) { // note: signed overflow semantics are the same between Go and EVM assembly
out = u256ToU64(sdiv(signExtend64To256(x), signExtend64To256(y)))
return
}
func mod64(x, y U64) (out U64) {
out = U64(mod(U256(x), U256(y)))
return
}
func smod64(x, y U64) (out U64) {
out = u256ToU64(smod(signExtend64To256(x), signExtend64To256(y)))
return
}
func not64(x U64) (out U64) {
out = u256ToU64(not(U256(x)))
return
}
func lt64(x, y U64) (out U64) {
out = U64(lt(U256(x), U256(y)))
return
}
func gt64(x, y U64) (out U64) {
out = U64(gt(U256(x), U256(y)))
return
}
func slt64(x, y U64) (out U64) {
out = U64(slt(signExtend64To256(x), signExtend64To256(y)))
return
}
func sgt64(x, y U64) (out U64) {
out = U64(sgt(signExtend64To256(x), signExtend64To256(y)))
return
}
func eq64(x, y U64) (out U64) {
out = U64(eq(U256(x), U256(y)))
return
}
func iszero64(x U64) bool {
return iszero(U256(x))
}
func and64(x, y U64) (out U64) {
out = U64(and(U256(x), U256(y)))
return
}
func or64(x, y U64) (out U64) {
out = U64(or(U256(x), U256(y)))
return
}
func xor64(x, y U64) (out U64) {
out = U64(xor(U256(x), U256(y)))
return
}
// returns y << x
func shl64(x, y U64) (out U64) {
out = u256ToU64(shl(U256(x), U256(y)))
return
}
// returns y >> x
func shr64(x, y U64) (out U64) {
out = U64(shr(U256(x), U256(y)))
return
}
// returns y >> x (signed)
func sar64(x, y U64) (out U64) {
out = u256ToU64(sar(U256(x), signExtend64To256(y)))
return
}