Skip to content

Instantly share code, notes, and snippets.

@ricrogz
Last active June 6, 2021 12:55
Show Gist options
  • Save ricrogz/7f9c405450689866139c49476b959044 to your computer and use it in GitHub Desktop.
Save ricrogz/7f9c405450689866139c49476b959044 to your computer and use it in GitHub Desktop.
Use int128 (really LONG integers) in cython
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
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
// ... 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