/
dataIntOperations.go
488 lines (430 loc) · 12.7 KB
/
dataIntOperations.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package ieletestingmodel
import (
"math"
"math/big"
)
var bigOne = big.NewInt(1)
// helper function for writing operations in fewer lines
func (*ModelState) bothSmall(ref1 KReference, ref2 KReference) (int64, int64, bool) {
small1, isSmall1 := parseKrefSmallInt(ref1)
if !isSmall1 {
return 0, 0, false
}
small2, isSmall2 := parseKrefSmallInt(ref2)
if !isSmall2 {
return 0, 0, false
}
return small1, small2, true
}
// helper function for writing operations in fewer lines
func (ms *ModelState) bothBig(ref1 KReference, ref2 KReference) (*big.Int, *big.Int, bool) {
big1, isInt1 := ms.GetBigIntUnsafe(ref1)
if !isInt1 {
return nil, nil, false
}
big2, isInt2 := ms.GetBigIntUnsafe(ref2)
if !isInt2 {
return nil, nil, false
}
return big1, big2, true
}
// IntEquals returns ref1 == ref2, if types ok.
// Also compares big ints with small ints.
func (ms *ModelState) IntEquals(ref1 KReference, ref2 KReference) (bool, bool) {
if small1, isSmall1 := parseKrefSmallInt(ref1); isSmall1 {
if small2, isSmall2 := parseKrefSmallInt(ref2); isSmall2 {
// small == small
return small1 == small2, true
} else if big2, isBig2 := ms.getBigIntObject(ref2); isBig2 {
// small == big
if big2.bigValue.Cmp(maxSmallIntAsBigInt) > 0 {
return false, true
}
if big2.bigValue.Cmp(minSmallIntAsBigInt) < 0 {
return false, true
}
return small1 == big2.bigValue.Int64(), true
}
return false, false
} else if big1, isBig1 := ms.getBigIntObject(ref1); isBig1 {
if small2, isSmall2 := parseKrefSmallInt(ref2); isSmall2 {
// big == small
if big1.bigValue.Cmp(maxSmallIntAsBigInt) > 0 {
return false, true
}
if big1.bigValue.Cmp(minSmallIntAsBigInt) < 0 {
return false, true
}
return big1.bigValue.Int64() == small2, true
} else if big2, isBig2 := ms.getBigIntObject(ref2); isBig2 {
// big == big
return big1.bigValue.Cmp(big2.bigValue) == 0, true
}
}
return false, false
}
// IntGt returns ref1 > ref2, if types ok.
// Also compares big ints with small ints.
func (ms *ModelState) IntGt(ref1 KReference, ref2 KReference) (bool, bool) {
if small1, isSmall1 := parseKrefSmallInt(ref1); isSmall1 {
if small2, isSmall2 := parseKrefSmallInt(ref2); isSmall2 {
// small > small
return small1 > small2, true
} else if big2, isBig2 := ms.getBigIntObject(ref2); isBig2 {
// small > big
if big2.bigValue.Cmp(maxSmallIntAsBigInt) > 0 {
return false, true
}
if big2.bigValue.Cmp(minSmallIntAsBigInt) < 0 {
return true, true
}
return small1 > big2.bigValue.Int64(), true
}
return false, false
} else if big1, isBig1 := ms.getBigIntObject(ref1); isBig1 {
if small2, isSmall2 := parseKrefSmallInt(ref2); isSmall2 {
// big > small
if big1.bigValue.Cmp(maxSmallIntAsBigInt) > 0 {
return true, true
}
if big1.bigValue.Cmp(minSmallIntAsBigInt) < 0 {
return false, true
}
return big1.bigValue.Int64() > small2, true
} else if big2, isBig2 := ms.getBigIntObject(ref2); isBig2 {
// big > big
return big1.bigValue.Cmp(big2.bigValue) > 0, true
}
}
return false, false
}
// IntGe returns ref1 >= ref2, if types ok.
// Also compares big ints with small ints.
func (ms *ModelState) IntGe(ref1 KReference, ref2 KReference) (bool, bool) {
if small1, isSmall1 := parseKrefSmallInt(ref1); isSmall1 {
if small2, isSmall2 := parseKrefSmallInt(ref2); isSmall2 {
// small >= small
return small1 >= small2, true
} else if big2, isBig2 := ms.getBigIntObject(ref2); isBig2 {
// small >= big
if big2.bigValue.Cmp(maxSmallIntAsBigInt) > 0 {
return false, true
}
if big2.bigValue.Cmp(minSmallIntAsBigInt) < 0 {
return true, true
}
return small1 >= big2.bigValue.Int64(), true
}
return false, false
} else if big1, isBig1 := ms.getBigIntObject(ref1); isBig1 {
if small2, isSmall2 := parseKrefSmallInt(ref2); isSmall2 {
// big >= small
if big1.bigValue.Cmp(maxSmallIntAsBigInt) > 0 {
return true, true
}
if big1.bigValue.Cmp(minSmallIntAsBigInt) < 0 {
return false, true
}
return big1.bigValue.Int64() >= small2, true
} else if big2, isBig2 := ms.getBigIntObject(ref2); isBig2 {
// big >= big
return big1.bigValue.Cmp(big2.bigValue) >= 0, true
}
}
return false, false
}
// IntLt returns ref1 < ref2, if types ok.
// Also compares big ints with small ints.
func (ms *ModelState) IntLt(ref1 KReference, ref2 KReference) (bool, bool) {
return ms.IntGt(ref2, ref1)
}
// IntLe returns ref1 <= ref2, if types ok.
// Also compares big ints with small ints.
func (ms *ModelState) IntLe(ref1 KReference, ref2 KReference) (bool, bool) {
return ms.IntGe(ref2, ref1)
}
// IntAdd returns ref1 + ref2, if types ok
func (ms *ModelState) IntAdd(ref1 KReference, ref2 KReference) (KReference, bool) {
small1, small2, smallOk := ms.bothSmall(ref1, ref2)
// TODO: overflow handling
if smallOk && small1 < math.MaxInt32 && small1 > math.MinInt32 && small2 < math.MaxInt32 && small2 > math.MinInt32 {
result := int64(small1) + int64(small2)
if fitsInSmallIntReference(result) {
return createKrefSmallInt(result), true
}
}
big1, big2, bigOk := ms.bothBig(ref1, ref2)
if bigOk {
if big1.Sign() == 0 {
return ref2, true
}
if big2.Sign() == 0 {
return ref1, true
}
ref, obj := ms.newBigIntObject()
obj.bigValue.Add(big1, big2)
return ref, true
}
return NullReference, false
}
// IntSub returns ref1 - ref2, if types ok
func (ms *ModelState) IntSub(ref1 KReference, ref2 KReference) (KReference, bool) {
small1, small2, smallOk := ms.bothSmall(ref1, ref2)
// TODO: overflow handling
if smallOk && small1 < math.MaxInt32 && small1 > math.MinInt32 && small2 < math.MaxInt32 && small2 > math.MinInt32 {
result := int64(small1) - int64(small2)
if fitsInSmallIntReference(result) {
return createKrefSmallInt(result), true
}
}
big1, big2, bigOk := ms.bothBig(ref1, ref2)
if bigOk {
if big2.Sign() == 0 {
return ref1, true
}
ref, obj := ms.newBigIntObject()
obj.bigValue.Sub(big1, big2)
return ref, true
}
return NullReference, false
}
// IntMul returns ref1 x ref2, if types ok
func (ms *ModelState) IntMul(ref1 KReference, ref2 KReference) (KReference, bool) {
small1, small2, smallOk := ms.bothSmall(ref1, ref2)
if smallOk && smallMultiplicationSafe(small1, small2) {
return createKrefSmallInt(small1 * small2), true
}
big1, big2, bigOk := ms.bothBig(ref1, ref2)
if bigOk {
if big1.Sign() == 0 || big2.Sign() == 0 {
return IntZero, true
}
if big1.Cmp(bigOne) == 0 {
return ref2, true
}
if big2.Cmp(bigOne) == 0 {
return ref1, true
}
ref, obj := ms.newBigIntObject()
obj.bigValue.Mul(big1, big2)
return ref, true
}
return NullReference, false
}
// IntDiv performs integer division.
// The result is truncated towards zero and obeys the rule of signs.
func (ms *ModelState) IntDiv(ref1 KReference, ref2 KReference) (KReference, bool) {
small1, small2, smallOk := ms.bothSmall(ref1, ref2)
if smallOk {
return createKrefSmallInt(small1 / small2), true
}
big1, big2, bigOk := ms.bothBig(ref1, ref2)
if bigOk {
resultPositive := true
if big1.Sign() < 0 {
resultPositive = !resultPositive
big1 = big.NewInt(0).Neg(big1)
}
if big2.Sign() < 0 {
resultPositive = !resultPositive
big2 = big.NewInt(0).Neg(big2)
}
ref, obj := ms.newBigIntObject()
obj.bigValue.Div(big1, big2)
if !resultPositive {
obj.bigValue.Neg(obj.bigValue)
}
return ref, true
}
return NullReference, false
}
// IntMod performs integer remainder.
// The result of rem a b has the sign of a, and its absolute value is strictly smaller than the absolute value of b.
// The result satisfies the equality a = b * div a b + rem a b.
func (ms *ModelState) IntMod(ref1 KReference, ref2 KReference) (KReference, bool) {
small1, small2, smallOk := ms.bothSmall(ref1, ref2)
if smallOk {
return createKrefSmallInt(small1 % small2), true
}
big1, big2, bigOk := ms.bothBig(ref1, ref2)
if bigOk {
arg1Negative := false
if big1.Sign() < 0 {
arg1Negative = true
big1 = big.NewInt(0).Neg(big1)
}
if big2.Sign() < 0 {
big2 = big.NewInt(0).Neg(big2)
}
ref, obj := ms.newBigIntObject()
obj.bigValue.Mod(big1, big2)
if arg1Negative {
obj.bigValue.Neg(obj.bigValue)
}
return ref, true
}
return NullReference, false
}
// IntEuclidianDiv performs Euclidian division.
func (ms *ModelState) IntEuclidianDiv(ref1 KReference, ref2 KReference) (KReference, bool) {
big1, big2, bigOk := ms.bothBig(ref1, ref2)
if bigOk {
ref, obj := ms.newBigIntObject()
obj.bigValue.Div(big1, big2)
return ref, true
}
return NullReference, false
}
// IntEuclidianMod performs Euclidian remainder.
func (ms *ModelState) IntEuclidianMod(ref1 KReference, ref2 KReference) (KReference, bool) {
big1, big2, bigOk := ms.bothBig(ref1, ref2)
if bigOk {
ref, obj := ms.newBigIntObject()
obj.bigValue.Mod(big1, big2)
return ref, true
}
return NullReference, false
}
// IntPow returns ref1 ^ ref2, if types ok
func (ms *ModelState) IntPow(ref1 KReference, ref2 KReference) (KReference, bool) {
big1, big2, bigOk := ms.bothBig(ref1, ref2)
if bigOk {
ref, obj := ms.newBigIntObject()
obj.bigValue.Exp(big1, big2, nil)
return ref, true
}
return NullReference, false
}
// IntPowMod returns (ref1 ^ ref2) mod ref3, if types ok
func (ms *ModelState) IntPowMod(ref1 KReference, ref2 KReference, ref3 KReference) (KReference, bool) {
big1, big2, bigOk := ms.bothBig(ref1, ref2)
if bigOk {
big3, big3Ok := ms.GetBigInt(ref3)
if big3Ok {
ref, obj := ms.newBigIntObject()
obj.bigValue.Exp(big1, big2, big3)
return ref, true
}
}
return NullReference, false
}
// IntShl returns ref1 << ref2, if types ok
func (ms *ModelState) IntShl(ref1 KReference, ref2 KReference) (KReference, bool) {
arg2, arg2Ok := ms.GetUint(ref2)
if !arg2Ok {
return NullReference, false
}
arg1, arg1Ok := ms.GetBigInt(ref1)
if arg1Ok {
ref, obj := ms.newBigIntObject()
obj.bigValue.Lsh(arg1, arg2)
return ref, true
}
return NullReference, false
}
// IntShr returns ref1 >> ref2, if types ok
func (ms *ModelState) IntShr(ref1 KReference, ref2 KReference) (KReference, bool) {
arg2, arg2Ok := ms.GetUint(ref2)
if !arg2Ok {
return NullReference, false
}
arg1, arg1Ok := ms.GetBigInt(ref1)
if arg1Ok {
ref, obj := ms.newBigIntObject()
obj.bigValue.Rsh(arg1, arg2)
return ref, true
}
return NullReference, false
}
// IntAnd returns bitwise and, ref1 & ref2, if types ok
func (ms *ModelState) IntAnd(ref1 KReference, ref2 KReference) (KReference, bool) {
if ms.IsZero(ref1) || ms.IsZero(ref2) {
return IntZero, true
}
big1, big2, bigOk := ms.bothBig(ref1, ref2)
if bigOk {
ref, obj := ms.newBigIntObject()
obj.bigValue.And(big1, big2)
return ref, true
}
return NullReference, false
}
// IntOr returns bitwise or, ref1 | ref2, if types ok
func (ms *ModelState) IntOr(ref1 KReference, ref2 KReference) (KReference, bool) {
if ms.IsZero(ref1) {
return ref2, true
}
if ms.IsZero(ref2) {
return ref1, true
}
big1, big2, bigOk := ms.bothBig(ref1, ref2)
if bigOk {
ref, obj := ms.newBigIntObject()
obj.bigValue.Or(big1, big2)
return ref, true
}
return NullReference, false
}
// IntXor returns bitwise xor, ref1 xor ref2, if types ok
func (ms *ModelState) IntXor(ref1 KReference, ref2 KReference) (KReference, bool) {
big1, big2, bigOk := ms.bothBig(ref1, ref2)
if bigOk {
ref, obj := ms.newBigIntObject()
obj.bigValue.Xor(big1, big2)
return ref, true
}
return NullReference, false
}
// IntNot returns bitwise not, if type ok
func (ms *ModelState) IntNot(ref KReference) (KReference, bool) {
arg, argOk := ms.GetBigInt(ref)
if argOk {
ref, obj := ms.newBigIntObject()
obj.bigValue.Not(arg)
return ref, true
}
return NullReference, false
}
// IntAbs returns the absoute value, if type ok
func (ms *ModelState) IntAbs(ref KReference) (KReference, bool) {
small, isSmall := parseKrefSmallInt(ref)
if isSmall {
if small >= 0 {
return ref, true
}
return createKrefSmallInt(-small), true
}
bigArg, bigOk := ms.GetBigInt(ref)
if bigOk {
if bigArg.Sign() >= 0 {
return ref, true
}
ref, obj := ms.newBigIntObject()
obj.bigValue.Neg(bigArg)
return ref, true
}
return NullReference, false
}
// IntLog2 basically counts the number of bits after the most significant bit.
// It is equal to a a truncated log2 of the number.
// Argument must be strictly positive.
func (ms *ModelState) IntLog2(ref KReference) (KReference, bool) {
if small, isSmall := parseKrefSmallInt(ref); isSmall {
if small <= 0 {
return NullReference, false
}
nrBits := 0
for small > 0 {
small = small >> 1
nrBits++
}
return ms.FromInt(nrBits - 1), true
} else if bigArg, isBig := ms.getBigIntObject(ref); isBig {
if bigArg.bigValue.Sign() <= 0 {
return NullReference, false
}
nrBits := bigArg.bigValue.BitLen()
return ms.FromInt(nrBits - 1), true
}
return NullReference, false
}