Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More efficient conversion from Python int to fmpz #159

Open
oscarbenjamin opened this issue Jul 4, 2024 · 1 comment
Open

More efficient conversion from Python int to fmpz #159

oscarbenjamin opened this issue Jul 4, 2024 · 1 comment

Comments

@oscarbenjamin
Copy link
Collaborator

It looks like Python 3.13 will provide public C API for efficiently converting to and from a PyLong. These should be compatible with mpz_import/mpz_export rather than converting between hex strings:

python/cpython#121339

cdef inline int fmpz_set_pylong(fmpz_t x, obj):
cdef int overflow
cdef slong longval
longval = pylong_as_slong(<PyObject*>obj, &overflow)
if overflow:
s = "%x" % obj
fmpz_set_str(x, chars_from_str(s), 16)
else:
fmpz_set_si(x, longval)

cdef fmpz_get_intlong(fmpz_t x):
"""
Convert fmpz_t to a Python int or long.
"""
cdef char * s
if COEFF_IS_MPZ(x[0]):
s = fmpz_get_str(NULL, 16, x)
v = int(str_from_chars(s), 16)
libc.stdlib.free(s)
return v
else:
return <slong>x[0]

@skirpichev
Copy link

It looks like Python 3.13 will provide public C API

Nit: 3.14. Maybe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants