| 1 | import struct |
| 2 | |
| 3 | class Count(object): |
| 4 | def __init__(self): |
| 5 | self.count = 0 |
| 6 | |
| 7 | def __next__(self): |
| 8 | self.count += 1 |
| 9 | return self.count |
| 10 | counter = Count() |
| 11 | |
| 12 | |
| 13 | def split32(long_int, n): |
| 14 | """Split long_int into n 32-bit words big-endian""" |
| 15 | |
| 16 | assert(long_int >= 0) |
| 17 | |
| 18 | result = [] |
| 19 | for x in range(n): |
| 20 | result += [ "0x%08xUL" % (long_int & (2**32-1)) ] |
| 21 | long_int >>= 32 |
| 22 | return result |
| 23 | |
| 24 | |
| 25 | def split64(long_int): |
| 26 | """Split long_int into 64-bit words big-endian""" |
| 27 | |
| 28 | assert(long_int >= 0) |
| 29 | |
| 30 | if long_int == 0: |
| 31 | return [ "0" ] |
| 32 | |
| 33 | result = [] |
| 34 | while long_int: |
| 35 | result += [ "0x%xULL" % (long_int & (2**64-1)) ] |
| 36 | long_int >>= 64 |
| 37 | return result |
| 38 | |
| 39 | def inverse(x, modulus): |
| 40 | if modulus == 0: |
| 41 | raise ZeroDivisionError("Modulus cannot be zero") |
| 42 | if modulus < 0: |
| 43 | raise ValueError("Modulus cannot be negative") |
| 44 | r_p, r_n = x, modulus |
| 45 | s_p, s_n = 1, 0 |
| 46 | while r_n > 0: |
| 47 | q = r_p // r_n |
| 48 | r_p, r_n = r_n, r_p - q * r_n |
| 49 | s_p, s_n = s_n, s_p - q * s_n |
| 50 | if r_p != 1: |
| 51 | raise ValueError("No inverse value can be computed" + str(r_p)) |
| 52 | while s_p < 0: |
| 53 | s_p += modulus |
| 54 | return s_p |
| 55 | |
| 56 | |
| 57 | def bin2int(bs): |
| 58 | res = 0 |
| 59 | while len(bs) > 0: |
| 60 | res = res*256 + struct.unpack("B", bs[:1])[0] |
| 61 | bs = bs[1:] |
| 62 | return res |
| 63 | |
| 64 | |
| 65 | def make_main(): |
| 66 | print("int main(void) {") |
| 67 | for i in range(1, next(counter)): |
| 68 | print(" test_%d();" % i) |
| 69 | print(" return 0;") |
| 70 | print("}") |
| 71 | |
| 72 |