Last active
June 6, 2021 12:55
-
-
Save ricrogz/7f9c405450689866139c49476b959044 to your computer and use it in GitHub Desktop.
Use int128 (really LONG integers) in cython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Taken from https://stackoverflow.com/questions/27582001/how-to-use-128-bit-integers-in-cython | |
Basically, we make cython believe we will use 64 bit int to generate the .c file, | |
but by using a C header we will, in fact, define a 128 bit int (the definition | |
in the .h file does not match what we put into the pyx file). | |
Once Cython has generated the .c file, we can compile it with GCC without further trouble, | |
as GCC does support 128 bit ints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cdef extern from "header_int128.h": | |
# Basically, we cheat cython to think we will use unsigned long long, | |
# which is a 64 bit int ... | |
ctypedef unsigned long long int128 | |
print "hello world" | |
cpdef int foo(): | |
cdef int128 foo = 4 | |
return 32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... but in the header, we give another definition, | |
// the one for a 128 bit int. | |
typedef __int128_t int128; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment