1 2cdef extern from *: 3 ctypedef long long int128_t "__int128_t" 4 ctypedef unsigned long long uint128_t "__uint128_t" 5 6 7def bigint(x): 8 print(str(x).rstrip('L')) 9 10 11def unsigned_conversion(x): 12 """ 13 >>> bigint(unsigned_conversion(0)) 14 0 15 >>> bigint(unsigned_conversion(2)) 16 2 17 18 >>> unsigned_conversion(-2) # doctest: +ELLIPSIS 19 Traceback (most recent call last): 20 OverflowError: can't convert negative value to ...uint128_t 21 >>> unsigned_conversion(-2**120) # doctest: +ELLIPSIS 22 Traceback (most recent call last): 23 OverflowError: can't convert negative value to ...uint128_t 24 >>> unsigned_conversion(-2**127) # doctest: +ELLIPSIS 25 Traceback (most recent call last): 26 OverflowError: can't convert negative value to ...uint128_t 27 >>> unsigned_conversion(-2**128) # doctest: +ELLIPSIS 28 Traceback (most recent call last): 29 OverflowError: can't convert negative value to ...uint128_t 30 31 >>> bigint(unsigned_conversion(2**20)) 32 1048576 33 >>> bigint(unsigned_conversion(2**30-1)) 34 1073741823 35 >>> bigint(unsigned_conversion(2**30)) 36 1073741824 37 >>> bigint(unsigned_conversion(2**30+1)) 38 1073741825 39 40 >>> bigint(2**60) 41 1152921504606846976 42 >>> bigint(unsigned_conversion(2**60-1)) 43 1152921504606846975 44 >>> bigint(unsigned_conversion(2**60)) 45 1152921504606846976 46 >>> bigint(unsigned_conversion(2**60+1)) 47 1152921504606846977 48 >>> bigint(2**64) 49 18446744073709551616 50 >>> bigint(unsigned_conversion(2**64)) 51 18446744073709551616 52 53 >>> bigint(2**120) 54 1329227995784915872903807060280344576 55 >>> bigint(unsigned_conversion(2**120)) 56 1329227995784915872903807060280344576 57 >>> bigint(2**128-1) 58 340282366920938463463374607431768211455 59 >>> bigint(unsigned_conversion(2**128-1)) 60 340282366920938463463374607431768211455 61 >>> bigint(unsigned_conversion(2**128)) # doctest: +ELLIPSIS 62 Traceback (most recent call last): 63 OverflowError: ... too big to convert 64 """ 65 cdef uint128_t n = x 66 return n 67 68 69def signed_conversion(x): 70 """ 71 >>> bigint(signed_conversion(0)) 72 0 73 >>> bigint(signed_conversion(2)) 74 2 75 >>> bigint(signed_conversion(-2)) 76 -2 77 78 >>> bigint(signed_conversion(2**20)) 79 1048576 80 >>> bigint(signed_conversion(2**32)) 81 4294967296 82 >>> bigint(2**64) 83 18446744073709551616 84 >>> bigint(signed_conversion(2**64)) 85 18446744073709551616 86 >>> bigint(signed_conversion(-2**64)) 87 -18446744073709551616 88 89 >>> bigint(2**118) 90 332306998946228968225951765070086144 91 >>> bigint(signed_conversion(2**118)) 92 332306998946228968225951765070086144 93 >>> bigint(signed_conversion(-2**118)) 94 -332306998946228968225951765070086144 95 96 >>> bigint(2**120) 97 1329227995784915872903807060280344576 98 >>> bigint(signed_conversion(2**120)) 99 1329227995784915872903807060280344576 100 >>> bigint(signed_conversion(-2**120)) 101 -1329227995784915872903807060280344576 102 103 >>> bigint(2**127-1) 104 170141183460469231731687303715884105727 105 >>> bigint(signed_conversion(2**127-2)) 106 170141183460469231731687303715884105726 107 >>> bigint(signed_conversion(2**127-1)) 108 170141183460469231731687303715884105727 109 >>> bigint(signed_conversion(2**127)) # doctest: +ELLIPSIS 110 Traceback (most recent call last): 111 OverflowError: ... too big to convert 112 >>> bigint(signed_conversion(-2**127)) 113 -170141183460469231731687303715884105728 114 >>> bigint(signed_conversion(-2**127-1)) # doctest: +ELLIPSIS 115 Traceback (most recent call last): 116 OverflowError: ... too big to convert 117 """ 118 cdef int128_t n = x 119 return n 120