-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitwise.go
264 lines (232 loc) · 4.98 KB
/
bitwise.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// bitwise contains bitwise operations for Uint instances.
// This file includes functions to perform bitwise AND, OR, XOR, and NOT operations, as well as bit shifting.
// These operations are crucial for manipulating individual bits within a 256-bit unsigned integer.
package uint256
// Or sets z = x | y and returns z.
func (z *Uint) Or(x, y *Uint) *Uint {
z.arr[0] = x.arr[0] | y.arr[0]
z.arr[1] = x.arr[1] | y.arr[1]
z.arr[2] = x.arr[2] | y.arr[2]
z.arr[3] = x.arr[3] | y.arr[3]
return z
}
// And sets z = x & y and returns z.
func (z *Uint) And(x, y *Uint) *Uint {
z.arr[0] = x.arr[0] & y.arr[0]
z.arr[1] = x.arr[1] & y.arr[1]
z.arr[2] = x.arr[2] & y.arr[2]
z.arr[3] = x.arr[3] & y.arr[3]
return z
}
// Not sets z = ^x and returns z.
func (z *Uint) Not(x *Uint) *Uint {
z.arr[3], z.arr[2], z.arr[1], z.arr[0] = ^x.arr[3], ^x.arr[2], ^x.arr[1], ^x.arr[0]
return z
}
// AndNot sets z = x &^ y and returns z.
func (z *Uint) AndNot(x, y *Uint) *Uint {
z.arr[0] = x.arr[0] &^ y.arr[0]
z.arr[1] = x.arr[1] &^ y.arr[1]
z.arr[2] = x.arr[2] &^ y.arr[2]
z.arr[3] = x.arr[3] &^ y.arr[3]
return z
}
// Xor sets z = x ^ y and returns z.
func (z *Uint) Xor(x, y *Uint) *Uint {
z.arr[0] = x.arr[0] ^ y.arr[0]
z.arr[1] = x.arr[1] ^ y.arr[1]
z.arr[2] = x.arr[2] ^ y.arr[2]
z.arr[3] = x.arr[3] ^ y.arr[3]
return z
}
// Lsh sets z = x << n and returns z.
func (z *Uint) Lsh(x *Uint, n uint) *Uint {
// n % 64 == 0
if n&0x3f == 0 {
switch n {
case 0:
return z.Set(x)
case 64:
return z.lsh64(x)
case 128:
return z.lsh128(x)
case 192:
return z.lsh192(x)
default:
return z.Clear()
}
}
var a, b uint64
// Big swaps first
switch {
case n > 192:
if n > 256 {
return z.Clear()
}
z.lsh192(x)
n -= 192
goto sh192
case n > 128:
z.lsh128(x)
n -= 128
goto sh128
case n > 64:
z.lsh64(x)
n -= 64
goto sh64
default:
z.Set(x)
}
// remaining shifts
a = z.arr[0] >> (64 - n)
z.arr[0] = z.arr[0] << n
sh64:
b = z.arr[1] >> (64 - n)
z.arr[1] = (z.arr[1] << n) | a
sh128:
a = z.arr[2] >> (64 - n)
z.arr[2] = (z.arr[2] << n) | b
sh192:
z.arr[3] = (z.arr[3] << n) | a
return z
}
// Rsh sets z = x >> n and returns z.
func (z *Uint) Rsh(x *Uint, n uint) *Uint {
// n % 64 == 0
if n&0x3f == 0 {
switch n {
case 0:
return z.Set(x)
case 64:
return z.rsh64(x)
case 128:
return z.rsh128(x)
case 192:
return z.rsh192(x)
default:
return z.Clear()
}
}
var a, b uint64
// Big swaps first
switch {
case n > 192:
if n > 256 {
return z.Clear()
}
z.rsh192(x)
n -= 192
goto sh192
case n > 128:
z.rsh128(x)
n -= 128
goto sh128
case n > 64:
z.rsh64(x)
n -= 64
goto sh64
default:
z.Set(x)
}
// remaining shifts
a = z.arr[3] << (64 - n)
z.arr[3] = z.arr[3] >> n
sh64:
b = z.arr[2] << (64 - n)
z.arr[2] = (z.arr[2] >> n) | a
sh128:
a = z.arr[1] << (64 - n)
z.arr[1] = (z.arr[1] >> n) | b
sh192:
z.arr[0] = (z.arr[0] >> n) | a
return z
}
// SRsh (Signed/Arithmetic right shift)
// considers z to be a signed integer, during right-shift
// and sets z = x >> n and returns z.
func (z *Uint) SRsh(x *Uint, n uint) *Uint {
// If the MSB is 0, SRsh is same as Rsh.
if !x.isBitSet(255) {
return z.Rsh(x, n)
}
if n%64 == 0 {
switch n {
case 0:
return z.Set(x)
case 64:
return z.srsh64(x)
case 128:
return z.srsh128(x)
case 192:
return z.srsh192(x)
default:
return z.SetAllOne()
}
}
var a uint64 = MaxUint64 << (64 - n%64)
// Big swaps first
switch {
case n > 192:
if n > 256 {
return z.SetAllOne()
}
z.srsh192(x)
n -= 192
goto sh192
case n > 128:
z.srsh128(x)
n -= 128
goto sh128
case n > 64:
z.srsh64(x)
n -= 64
goto sh64
default:
z.Set(x)
}
// remaining shifts
z.arr[3], a = (z.arr[3]>>n)|a, z.arr[3]<<(64-n)
sh64:
z.arr[2], a = (z.arr[2]>>n)|a, z.arr[2]<<(64-n)
sh128:
z.arr[1], a = (z.arr[1]>>n)|a, z.arr[1]<<(64-n)
sh192:
z.arr[0] = (z.arr[0] >> n) | a
return z
}
func (z *Uint) lsh64(x *Uint) *Uint {
z.arr[3], z.arr[2], z.arr[1], z.arr[0] = x.arr[2], x.arr[1], x.arr[0], 0
return z
}
func (z *Uint) lsh128(x *Uint) *Uint {
z.arr[3], z.arr[2], z.arr[1], z.arr[0] = x.arr[1], x.arr[0], 0, 0
return z
}
func (z *Uint) lsh192(x *Uint) *Uint {
z.arr[3], z.arr[2], z.arr[1], z.arr[0] = x.arr[0], 0, 0, 0
return z
}
func (z *Uint) rsh64(x *Uint) *Uint {
z.arr[3], z.arr[2], z.arr[1], z.arr[0] = 0, x.arr[3], x.arr[2], x.arr[1]
return z
}
func (z *Uint) rsh128(x *Uint) *Uint {
z.arr[3], z.arr[2], z.arr[1], z.arr[0] = 0, 0, x.arr[3], x.arr[2]
return z
}
func (z *Uint) rsh192(x *Uint) *Uint {
z.arr[3], z.arr[2], z.arr[1], z.arr[0] = 0, 0, 0, x.arr[3]
return z
}
func (z *Uint) srsh64(x *Uint) *Uint {
z.arr[3], z.arr[2], z.arr[1], z.arr[0] = MaxUint64, x.arr[3], x.arr[2], x.arr[1]
return z
}
func (z *Uint) srsh128(x *Uint) *Uint {
z.arr[3], z.arr[2], z.arr[1], z.arr[0] = MaxUint64, MaxUint64, x.arr[3], x.arr[2]
return z
}
func (z *Uint) srsh192(x *Uint) *Uint {
z.arr[3], z.arr[2], z.arr[1], z.arr[0] = MaxUint64, MaxUint64, MaxUint64, x.arr[3]
return z
}