1- # fixme!
2- DEF FLINT_BITS = 64
1+ # _flint.pxd
2+ #
3+ # Define the contents of the Python, GMP, Flint and Arb headers.
34
45cdef extern from " Python.h" :
56 ctypedef void PyObject
67 ctypedef void PyTypeObject
78 ctypedef long Py_ssize_t
89 int PyObject_TypeCheck(object , PyTypeObject* )
910 int PyInt_Check(PyObject * o)
10- PyObject* PyInt_FromLong(long ival)
1111 int PyLong_Check(PyObject * o)
1212 long PyInt_AS_LONG(PyObject * io)
1313 double PyFloat_AS_DOUBLE(PyObject * io)
1414 Py_ssize_t PyList_GET_SIZE(PyObject * list )
1515 long PyLong_AsLongAndOverflow(PyObject * pylong, int * overflow)
16+ long long PyLong_AsLongLongAndOverflow(PyObject * pylong, int * overflow)
1617
1718DEF FMPZ_UNKNOWN = 0
1819DEF FMPZ_REF = 1
1920DEF FMPZ_TMP = 2
2021
22+ #
23+ # Note: ulong and slong are used throughout Flint/Arb. They are expected to be
24+ # 32 bit unsigned and signed integer types on a 32 bit system and 64 bit on a
25+ # 64 bit system. We denote them as unsigned long and long here which would be
26+ # incorrect on 64 bit Windows but the definition here does not matter because
27+ # their actual sizes will be determined by the values from gmp.h and
28+ # flint/flint.h. Their size in bits (32 or 64) is recorded in the FLINT_BITS
29+ # macro which is defined in flint/flint.h.
30+ #
31+
2132cdef extern from " gmp.h" :
2233 ctypedef unsigned long ulong
2334 ctypedef unsigned long mp_limb_t
@@ -27,18 +38,36 @@ cdef extern from "gmp.h":
2738 ctypedef mp_limb_t* mp_srcptr
2839 ctypedef unsigned long mp_bitcnt_t
2940
30- ctypedef long fmpz_struct
31- ctypedef long slong
32- ctypedef ulong flint_bitcnt_t
41+ cdef extern from " flint/fmpz.h" :
42+ ctypedef long slong
43+ ctypedef ulong flint_bitcnt_t
44+
45+ ctypedef slong fmpz_struct
3346
3447cdef extern from " flint/flint.h" :
48+ const int FLINT_BITS
3549 ctypedef void * flint_rand_t
3650 void flint_randinit(flint_rand_t state)
3751 void flint_randclear(flint_rand_t state)
3852 void flint_set_num_threads(long )
3953 long flint_get_num_threads()
4054 void flint_cleanup()
4155
56+ cdef extern from * :
57+ """
58+ /* FLINT_BITS is not known until C compile time. We need to check if long
59+ * or long long matches FLINT_BITS to know which CPython function to call.
60+ */
61+ #if FLINT_BITS == 32 && LONG_MAX == 2147483647
62+ #define pylong_as_slong PyLong_AsLongAndOverflow
63+ #elif FLINT_BITS == 64 && LLONG_MAX == 9223372036854775807
64+ #define pylong_as_slong PyLong_AsLongLongAndOverflow
65+ #else
66+ #error FLINT_BITS does not match width of long or long long.
67+ #endif
68+ """
69+ slong pylong_as_slong(PyObject * pylong, int * overflow)
70+
4271cdef extern from " flint/nmod_vec.h" :
4372 ctypedef struct nmod_t:
4473 mp_limb_t n
0 commit comments