-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbig.py
656 lines (518 loc) · 16.6 KB
/
big.py
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
#! /usr/bin/python
# Professor Dimitri Lisin
import unittest
import math
####################################################################
# Functions for converting between python integers and integers
# stored in bytearrays.
def int2bytearray(val, nbytes=-1):
if val < 0:
val = -val
sign = -1
else:
sign = 1
if nbytes == -1:
nbytes = int(math.ceil(math.log(val, 2)) / 8) + 1
array = bytearray(nbytes)
for i in range(nbytes - 1, -1, -1):
remainder = val % 256
val = val / 256
array[i] = remainder
if sign == -1:
negate_in_place(array)
return array
def bytearray2int(input_array):
temp_array = bytearray(len(input_array))
temp_array[:] = input_array
sign = 1
if is_negative(temp_array):
negate_in_place(temp_array)
sign = -1
val = 0
factor = 1
for i in range(len(temp_array) - 1, -1, -1):
val += temp_array[i] * factor
factor *= 256
return sign * val
###############################################################
# Arithmetic functions for integers represented as bytearrays.
def is_negative(array):
return get_highest_bit(array[0]) != 0
def negate(array):
output = bytearray(len(array))
output[:] = array
negate_in_place(output)
return output
def sign_extend(array, nbytes=1):
# Extend the number by nbytes bytes.
pad = bytearray(nbytes)
if is_negative(array):
for i in range(len(pad)):
pad[i] = 0xff
output = pad + array
return output
def make_same_size(a, b):
if len(a) < len(b):
a_pad = sign_extend(a, len(b) - len(a))
else:
a_pad = bytearray(len(a))
a_pad[:] = a
if len(b) < len(a):
b_pad = sign_extend(b, len(a) - len(b))
else:
b_pad = bytearray(len(b))
b_pad[:] = b
return [a_pad, b_pad]
def add(a, b):
[a_pad, b_pad] = make_same_size(a, b)
c = add_aux(a_pad, b_pad)
if (not is_negative(a) and not is_negative(b) and is_negative(c)) or \
(is_negative(a) and is_negative(b) and not is_negative(c)):
a_pad = sign_extend(a_pad)
b_pad = sign_extend(b_pad)
c = add_aux(a_pad, b_pad)
return c
def equal(a, b):
[a_pad, b_pad] = make_same_size(a, b)
for i in range(len(a_pad)):
if a_pad[i] != b_pad[i]:
return False
return True
def greater_than(a, b):
[a_pad, b_pad] = make_same_size(a, b)
if is_negative(a_pad) and is_negative(b_pad):
return greater_negative(a_pad, b_pad)
elif not is_negative(a_pad) and not is_negative(b_pad):
return greater_positive(a_pad, b_pad)
elif is_negative(b_pad):
return True
else:
return False
def less_than(a, b):
return not equal(a, b) and not greater_than(a, b)
def is_even(array):
return get_lowest_bit(array[-1]) == 0
def half(array):
if is_negative(array):
output = negate(array)
if is_even(output):
return negate(half_positive(output))
else:
return add(negate(half_positive(output)), int2bytearray(-1, len(output)))
else:
return half_positive(array)
def twice(array):
if is_negative(array):
return negate(twice_positive(negate(array)))
else:
return twice_positive(array)
def multiply(a, b):
if len(a) < len(b):
a_pad = sign_extend(a, len(b) - len(a))
else:
a_pad = a
if len(b) < len(a):
b_pad = sign_extend(b, len(a) - len(b))
else:
b_pad = b
if is_negative(a_pad) and is_negative(b_pad):
return multiply_positive(negate(a_pad), negate(b_pad))
elif is_negative(a_pad):
return negate(multiply_positive(negate(a_pad), b_pad))
elif is_negative(b_pad):
return negate(multiply_positive(a_pad, negate(b_pad)))
else:
return multiply_positive(a_pad, b_pad)
#############################################################
# Helper functions
def add_aux(a, b):
c = bytearray(len(a))
carry = 0
for i in range(len(a) - 1, -1, -1):
val = a[i] + b[i] + carry
c[i] = val & 0xff
carry = val >> 8
return c
def greater_positive(a, b):
for i in range(len(a)):
if a[i] > b[i]:
return True
elif a[i] < b[i]:
return False
def greater_negative(a, b):
a_mag = negate(a)
b_mag = negate(b)
return greater_positive(b_mag, a_mag)
def get_lowest_bit(byte):
return byte & 0x01
def get_highest_bit(byte):
return byte & 0x80
def half_positive(array):
output = bytearray(len(array))
carry = 0
for i in range(0, len(array)):
val = array[i]
output[i] = (val >> 1) + carry
carry = (get_lowest_bit(val) << 7) & 0xff
return output
def twice_positive(array):
# Check for overflow. The 0th bit is the sign bit,
# which we assume to be 0 in this case. Thus twice()
# will overflow, if the 1st bit 1.
if array[0] & 0x40:
return twice_positive_impl(sign_extend(array))
else:
return twice_positive_impl(array)
def twice_positive_impl(array):
output = bytearray(len(array))
carry = 0
for i in range(len(array) - 1, -1, -1):
val = ((array[i] << 1) + carry)
carry = val >> 8
output[i] = val & 0xff
return output
def multiply_positive(x, y):
zero = bytearray(len(x))
if equal(y, zero):
return zero
z = multiply_positive(x, half(y))
if is_even(y):
return twice(z)
else:
return add(x, twice(z))
def negate_in_place(array):
for i in range(len(array)):
array[i] = ~array[i] & 0xff
val = array[-1] + 1
array[-1] = val & 0xff
carry = val >> 8
for i in range(len(array) - 2, -1, -1):
val = array[i] + carry
array[i] = val & 0xff
carry = val >> 8
##############################################
# Tests
class TestIntToBytearray(unittest.TestCase):
def test0(self):
nbytes = 4
b = int2bytearray(0, nbytes)
for i in range(len(b)):
self.assertEqual(b[i], 0)
def test5(self):
nbytes = 4
b = int2bytearray(5, nbytes)
self.assertEqual(b[nbytes - 1], 5)
def test256(self):
nbytes = 4
b = int2bytearray(256, nbytes)
self.assertEqual(b[nbytes - 1], 0)
self.assertEqual(b[nbytes - 2], 1)
def testAutosize5(self):
b = int2bytearray(5)
self.assertEqual(len(b), 1)
self.assertEqual(bytearray2int(b), 5)
def testAutosize729(self):
b = int2bytearray(729)
self.assertEqual(len(b), 2)
self.assertEqual(bytearray2int(b), 729)
##############################################
class TestBytearrayToInt(unittest.TestCase):
def test0(self):
nbytes = 4
array = bytearray(nbytes)
val = bytearray2int(array)
self.assertEqual(val, 0)
def test5(self):
nbytes = 4
array = bytearray(nbytes)
array[nbytes - 1] = 5;
val = bytearray2int(array)
self.assertEqual(val, 5)
def test256(self):
nbytes = 4
array = bytearray(nbytes)
array[nbytes - 2] = 1;
val = bytearray2int(array)
self.assertEqual(val, 256)
##############################################
class TestNegation(unittest.TestCase):
def test0(self):
array = bytearray(4)
negate_in_place(array)
self.assertEqual(bytearray2int(array), 0)
def testNeg5(self):
nbytes = 4
array = int2bytearray(5, nbytes)
negate_in_place(array)
self.assertEqual(bytearray2int(array), -5)
def testNeg50000(self):
nbytes = 4
array = int2bytearray(50000, nbytes)
negate_in_place(array)
self.assertEqual(bytearray2int(array), -50000)
def testCreateNegativeArray(self):
nbytes = 4
array = int2bytearray(-50000, nbytes)
self.assertTrue(is_negative(array))
self.assertEqual(bytearray2int(array), -50000)
##############################################
class TestAddition(unittest.TestCase):
def test0and0(self):
a = bytearray(4)
b = bytearray(4)
c = add(a, b)
self.assertEqual(bytearray2int(c), 0)
def test10and5(self):
nbytes = 4
a = int2bytearray(10, nbytes)
b = int2bytearray(5, nbytes)
c = add(a, b)
self.assertEqual(bytearray2int(c), 15)
def testBigNumbers(self):
nbytes = 4
x = 50000
y = 60729
a = int2bytearray(x, nbytes)
b = int2bytearray(y, nbytes)
c = add(a, b)
self.assertEqual(bytearray2int(c), x + y)
def testAddNegative(self):
nbytes = 4
x = 50000
y = -60729
a = int2bytearray(x, nbytes)
b = int2bytearray(y, nbytes)
c = add(a, b)
self.assertEqual(bytearray2int(c), x + y)
def testPositiveOverflow(self):
nbytes = 4
a = bytearray(nbytes)
b = bytearray(nbytes)
a[0] = 0x7f
b[0] = 0x7f
for i in range(1, len(a)):
a[i] = 0xff
b[i] = 0xff
c = add(a, b)
self.assertFalse(is_negative(c))
self.assertEqual(len(c), len(a) + 1)
def testSignExtension(self):
nbytes = 4
a = int2bytearray(-5, nbytes)
b = sign_extend(a)
self.assertEqual(bytearray2int(a), bytearray2int(b))
self.assertEqual(len(b), len(a) + 1)
def testNegativeOverflow(self):
nbytes = 2
val = -32768
a = int2bytearray(val, nbytes)
b = int2bytearray(val, nbytes)
self.assertTrue(is_negative(a))
self.assertTrue(is_negative(b))
c = add(a, b)
self.assertTrue(is_negative(c))
self.assertEqual(len(c), len(a) + 1)
def testAddDifferentSizes(self):
val = 32767
a = int2bytearray(val, 2)
b = int2bytearray(2 * val, 4)
c = add(a, b)
self.assertEqual(len(c), len(b))
self.assertEqual(bytearray2int(c), 3 * val)
c = add(b, a)
self.assertEqual(bytearray2int(c), 3 * val)
##############################################
class TestEqual(unittest.TestCase):
def test10and5(self):
nbytes = 4
a = int2bytearray(10, nbytes)
b = int2bytearray(5, nbytes)
self.assertFalse(equal(a, b))
self.assertTrue(equal(a, a))
def testDifferenceSizes(self):
a = int2bytearray(5, 2)
b = int2bytearray(5, 8)
self.assertTrue(equal(a, b))
##############################################
class TestGreaterPositive(unittest.TestCase):
def test10and5(self):
nbytes = 4
a = int2bytearray(10, nbytes)
b = int2bytearray(5, nbytes)
self.assertTrue(greater_positive(a, b))
self.assertFalse(greater_positive(b, a))
self.assertFalse(greater_positive(a, a))
##############################################
class TestGreaterNegative(unittest.TestCase):
def testNeg10and5(self):
nbytes = 4
a = int2bytearray(-10, nbytes)
b = int2bytearray(-5, nbytes)
self.assertTrue(greater_negative(b, a))
self.assertFalse(greater_negative(a, b))
self.assertFalse(greater_negative(b, b))
##############################################
class TestGreaterThan(unittest.TestCase):
def testPositives(self):
nbytes = 4
a = int2bytearray(10, nbytes)
b = int2bytearray(5, nbytes)
self.assertTrue(greater_than(a, b))
self.assertFalse(greater_than(b, a))
self.assertFalse(greater_than(a, a))
def testNegatives(self):
nbytes = 4
a = int2bytearray(-10, nbytes)
b = int2bytearray(-5, nbytes)
self.assertTrue(greater_than(b, a))
self.assertFalse(greater_than(a, b))
self.assertFalse(greater_than(b, b))
def testMixedSigns(self):
nbytes = 4
a = int2bytearray(-10, nbytes)
b = int2bytearray(5, nbytes)
self.assertTrue(greater_than(b, a))
self.assertFalse(greater_than(a, b))
self.assertFalse(greater_than(b, b))
def testDifferentSizes(self):
a = int2bytearray(100)
b = int2bytearray(48957655)
self.assertFalse(greater_than(a, b))
##############################################
class TestLessThan(unittest.TestCase):
def testPositives(self):
nbytes = 4
a = int2bytearray(10, nbytes)
b = int2bytearray(5, nbytes)
self.assertFalse(less_than(a, b))
self.assertTrue(less_than(b, a))
self.assertFalse(less_than(a, a))
def testNegatives(self):
nbytes = 4
a = int2bytearray(-10, nbytes)
b = int2bytearray(-5, nbytes)
self.assertFalse(less_than(b, a))
self.assertTrue(less_than(a, b))
self.assertFalse(less_than(b, b))
def testMixedSigns(self):
nbytes = 4
a = int2bytearray(-10, nbytes)
b = int2bytearray(5, nbytes)
self.assertFalse(less_than(b, a))
self.assertTrue(less_than(a, b))
self.assertFalse(less_than(b, b))
##############################################
class TestIsEven(unittest.TestCase):
def test4(self):
nbytes = 8
a = int2bytearray(4, nbytes)
self.assertTrue(is_even(a))
def test3(self):
nbytes = 8
a = int2bytearray(3, nbytes)
self.assertFalse(is_even(a))
##############################################
class TestHalf(unittest.TestCase):
def test4(self):
nbytes = 1
a = int2bytearray(4, nbytes)
self.assertFalse(is_negative(a))
b = half(a)
self.assertEqual(bytearray2int(b), 2)
def test500(self):
nbytes = 16
a = int2bytearray(500, nbytes)
b = half(a)
self.assertEqual(bytearray2int(b), 250)
def test501(self):
nbytes = 16
a = int2bytearray(501, nbytes)
b = half(a)
self.assertEqual(bytearray2int(b), 250)
def testNeg4(self):
nbytes = 16
a = int2bytearray(-4, nbytes)
b = half(a)
self.assertEqual(bytearray2int(b), -2)
def testNeg5(self):
nbytes = 16
a = int2bytearray(-5, nbytes)
b = half(a)
self.assertEqual(bytearray2int(b), -3)
##############################################
class TestTwice(unittest.TestCase):
def test4(self):
nbytes = 1
a = int2bytearray(4, nbytes)
b = twice(a)
self.assertEqual(bytearray2int(b), 8)
def test250(self):
nbytes = 16
a = int2bytearray(250, nbytes)
b = twice(a)
self.assertEqual(bytearray2int(b), 500)
def testNeg250(self):
nbytes = 16
a = int2bytearray(-250, nbytes)
b = twice(a)
self.assertEqual(bytearray2int(b), -500)
def testOverflow(self):
nbytes = 2
val = 32767
a = int2bytearray(val, nbytes)
b = twice(a)
self.assertEqual(len(b), 3)
self.assertEqual(bytearray2int(b), 2 * val)
##############################################
class TestMultiply(unittest.TestCase):
def testZero(self):
nbytes = 4
x = int2bytearray(729, nbytes)
y = int2bytearray(0, nbytes)
z = multiply(x, y)
self.assertEqual(bytearray2int(z), 0)
def testBigNumbers(self):
nbytes = 4
x = int2bytearray(729, nbytes)
y = int2bytearray(927, nbytes)
z = multiply(x, y)
self.assertEqual(bytearray2int(z), 729 * 927)
def testBigNegativeNumbers(self):
nbytes = 4
x = int2bytearray(-729, nbytes)
y = int2bytearray(-927, nbytes)
z = multiply(x, y)
self.assertEqual(bytearray2int(z), -729 * -927)
def testBigMixedNumbers1(self):
nbytes = 4
x = int2bytearray(-729, nbytes)
y = int2bytearray(927, nbytes)
z = multiply(x, y)
self.assertEqual(bytearray2int(z), -729 * 927)
def testBigMixedNumbers2(self):
nbytes = 4
x = int2bytearray(729, nbytes)
y = int2bytearray(-927, nbytes)
z = multiply(x, y)
self.assertEqual(bytearray2int(z), -729 * 927)
def testInfinitePrecision(self):
nbytes = 2
val = 32767
x = int2bytearray(val, nbytes)
y = int2bytearray(val, nbytes)
z = multiply(x, y)
self.assertEqual(bytearray2int(z), val * val)
self.assertEqual(len(z), 4)
def testDifferentSizes(self):
a = 729
b = 123456789
x = int2bytearray(a)
y = int2bytearray(b)
c = multiply(x, y)
self.assertEqual(bytearray2int(c), a * b)
c = multiply(y, x)
self.assertEqual(bytearray2int(c), b * a)
##############################################
if __name__ == '__main__':
# run the unit tests
unittest.main()