Skip to content

Commit edf6be7

Browse files
Merge pull request #28 from oscarbenjamin/pr_slong
fix: use long long on Windows
2 parents c0af7d4 + dc6151f commit edf6be7

File tree

6 files changed

+54
-39
lines changed

6 files changed

+54
-39
lines changed

.github/workflows/buildwheel.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ jobs:
66
build_wheels:
77
name: Build wheels for ${{ matrix.os }}
88
runs-on: ${{ matrix.os }}
9+
continue-on-error: true
910
strategy:
1011
fail-fast: false
1112
matrix:
@@ -23,8 +24,10 @@ jobs:
2324
uses: pypa/[email protected]
2425
env:
2526
CIBW_BUILD: cp39-* cp310-* cp311-*
26-
CIBW_SKIP: "*-win32 *-manylinux_i686 *-musllinux_*"
27+
#CIBW_SKIP: "*-win32 *-manylinux_i686 *-musllinux_*"
28+
CIBW_SKIP: "*-win32 *-musllinux_*"
2729
CIBW_MANYLINUX_X86_64_IMAGE: manylinux2014
30+
CIBW_MANYLINUX_I686_IMAGE: manylinux2014
2831
CIBW_BEFORE_ALL_LINUX: bin/cibw_before_build_linux.sh
2932
CIBW_BEFORE_ALL_MACOS: bin/cibw_before_build_macosx.sh
3033
CIBW_BEFORE_BUILD: pip install numpy cython

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
build/*
22
dist/*
3-
src/*.c
3+
src/flint/*.c
44
doc/build/*
55
fmake*
66
*.whl
7+
*.pyc
8+
*.pyd
9+
*.so
10+
__pycache__
711
MANIFEST
812
.eggs
913
.local

bin/build_inplace.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Build the flint._flint module in place.
4+
5+
C_INCLUDE_PATH=.local/include/ LIBRARY_PATH=.local/lib/ python setup.py build_ext --inplace

bin/build_wheel.sh

+2-28
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,11 @@
55

66
set -o errexit
77

8-
PYTHONFLINTVER=0.3.0
9-
108
source bin/build_variables.sh
119

1210
python3 -m venv $PREFIX/venv
1311
source $PREFIX/venv/bin/activate
14-
pip install -U pip wheel delocate
15-
pip install numpy cython
16-
# Working as of cython==0.29.28
12+
pip install -U pip
13+
pip install numpy cython wheel
1714

1815
C_INCLUDE_PATH=.local/include/ LIBRARY_PATH=.local/lib/ pip wheel .
19-
20-
wheelfinal=*.whl
21-
22-
# On OSX bundle the dynamic libraries for the dependencies
23-
mkdir -p wheelhouse
24-
delocate-wheel -w wheelhouse $wheelfinal
25-
26-
echo ------------------------------------------
27-
echo
28-
echo Built wheel: wheelhouse/$wheelfinal
29-
echo
30-
echo Link dependencies:
31-
delocate-listdeps wheelhouse/$wheelfinal
32-
echo
33-
pip install wheelhouse/$wheelfinal
34-
echo
35-
echo Demonstration:
36-
echo
37-
python -c 'import flint; print("(3/2)**2 =", flint.fmpq(3, 2)**2)'
38-
echo
39-
echo Done!
40-
echo
41-
echo ------------------------------------------

src/flint/_flint.pxd

+35-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1-
# fixme!
2-
DEF FLINT_BITS = 64
1+
# _flint.pxd
2+
#
3+
# Define the contents of the Python, GMP, Flint and Arb headers.
34

45
cdef 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

1718
DEF FMPZ_UNKNOWN = 0
1819
DEF FMPZ_REF = 1
1920
DEF 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+
2132
cdef 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

3447
cdef 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+
4271
cdef extern from "flint/nmod_vec.h":
4372
ctypedef struct nmod_t:
4473
mp_limb_t n

src/flint/fmpz.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cdef inline int fmpz_set_pylong(fmpz_t x, obj):
22
cdef int overflow
3-
cdef long longval
4-
longval = PyLong_AsLongAndOverflow(<PyObject*>obj, &overflow)
3+
cdef slong longval
4+
longval = pylong_as_slong(<PyObject*>obj, &overflow)
55
if overflow:
66
s = "%x" % obj
77
fmpz_set_str(x, chars_from_str(s), 16)
@@ -28,7 +28,7 @@ cdef fmpz_get_intlong(fmpz_t x):
2828
libc.stdlib.free(s)
2929
return v
3030
else:
31-
return <long>x[0]
31+
return <slong>x[0]
3232

3333
cdef int fmpz_set_any_ref(fmpz_t x, obj):
3434
if typecheck(obj, fmpz):

0 commit comments

Comments
 (0)