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

Use PyLong_Import/Export API #495

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/gmpy2.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ static PyObject *GMPyExc_Overflow = NULL;
static PyObject *GMPyExc_Underflow = NULL;
static PyObject *GMPyExc_Erange = NULL;

/*
* Parameters of Python’s internal representation of integers.
*/


size_t int_digit_size, int_nails, int_bits_per_digit;
int int_digits_order, int_endianness;


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* End of global data declarations. *
Expand Down Expand Up @@ -596,6 +604,14 @@ PyMODINIT_FUNC PyInit_gmpy2(void)
PyObject* xmpz = NULL;
PyObject* limb_size = NULL;

/* Query parameters of Python’s internal representation of integers. */
const PyLongLayout *layout = PyLong_GetNativeLayout();

int_digit_size = layout->digit_size;
int_digits_order = layout->digits_order;
int_bits_per_digit = layout->bits_per_digit;
int_nails = int_digit_size*8 - int_bits_per_digit;
int_endianness = layout->endianness;

#ifndef STATIC
static void *GMPy_C_API[GMPy_API_pointers];
Expand Down
14 changes: 12 additions & 2 deletions src/gmpy2_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ GMPy_Integer_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}
}
else {
mpz_set_PyLong(result->z, y);
if (mpz_set_PyLong(result->z, y)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_add(result->z, MPZ(x), result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand All @@ -88,7 +93,12 @@ GMPy_Integer_AddWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}
}
else {
mpz_set_PyLong(result->z, x);
if (mpz_set_PyLong(result->z, x)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_add(result->z, result->z, MPZ(y));
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down
20 changes: 0 additions & 20 deletions src/gmpy2_convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,6 @@ extern "C" {
#define IS_TYPE_COMPLEX_ONLY(x) ((x > OBJ_TYPE_REAL) && \
(x < OBJ_TYPE_COMPLEX))

/* Compatibility macros (to work with PyLongObject internals).
*/

#if PY_VERSION_HEX >= 0x030C0000
# define TAG_FROM_SIGN_AND_SIZE(is_neg, size) ((is_neg?2:(size==0)) | (((size_t)size) << 3))
# define _PyLong_SetSignAndDigitCount(obj, is_neg, size) (obj->long_value.lv_tag = TAG_FROM_SIGN_AND_SIZE(is_neg, size))
#elif PY_VERSION_HEX >= 0x030900A4
# define _PyLong_SetSignAndDigitCount(obj, is_neg, size) (Py_SET_SIZE(obj, (is_neg?-1:1)*size))
#else
# define _PyLong_SetSignAndDigitCount(obj, is_neg, size) (Py_SIZE(obj) = (is_neg?-1:1)*size)
#endif

#if PY_VERSION_HEX >= 0x030C0000
# define GET_OB_DIGIT(obj) ((PyLongObject*)obj)->long_value.ob_digit
# define _PyLong_DigitCount(obj) (((PyLongObject*)obj)->long_value.lv_tag >> 3)
#else
# define GET_OB_DIGIT(obj) obj->ob_digit
# define _PyLong_DigitCount(obj) (_PyLong_Sign(obj)<0 ? -Py_SIZE(obj):Py_SIZE(obj))
#endif

/* Since the macros are used in gmpy2's codebase, these functions are skipped
* until they are needed for the C API in the future.
*/
Expand Down
115 changes: 70 additions & 45 deletions src/gmpy2_convert_gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,49 @@
* some basic types such as C longs or doubles.
*/

#include "pythoncapi_compat.h"

/* ======================================================================== *
* Conversion between native Python objects and MPZ. *
* ======================================================================== */

/* To support creation of temporary mpz objects. */
static void
static int
mpz_set_PyLong(mpz_t z, PyObject *obj)
{
int negative;
Py_ssize_t len;
PyLongObject *templong = (PyLongObject*)obj;

len = _PyLong_DigitCount(obj);
negative = _PyLong_Sign(obj) < 0;

switch (len) {
case 1:
mpz_set_si(z, (sdigit)GET_OB_DIGIT(templong)[0]);
break;
case 0:
mpz_set_si(z, 0);
break;
default:
mpz_import(z, len, -1, sizeof(digit), 0,
sizeof(digit)*8 - PyLong_SHIFT,
GET_OB_DIGIT(templong));
static PyLongExport long_export;

if (PyLong_Export(obj, &long_export) < 0) {
/* LCOV_EXCL_START */
return -1;
/* LCOV_EXCL_STOP */
}
if (long_export.digits) {
mpz_import(z, long_export.ndigits, int_digits_order, int_digit_size,
int_endianness, int_nails, long_export.digits);
if (long_export.negative) {
mpz_neg(z, z);
}
PyLong_FreeExport(&long_export);
}
else {
const int64_t value = long_export.value;

if (negative) {
mpz_neg(z, z);
if (LONG_MIN <= value && value <= LONG_MAX) {
mpz_set_si(z, value);
}
else {
mpz_import(z, 1, -1, sizeof(int64_t), 0, 0, &value);
if (value < 0) {
mpz_t tmp;
mpz_init(tmp);
mpz_ui_pow_ui(tmp, 2, 64);
mpz_sub(z, z, tmp);
mpz_clear(tmp);
}
}
}
return;
return 0;
}

static MPZ_Object *
Expand All @@ -79,7 +90,12 @@ GMPy_MPZ_From_PyLong(PyObject *obj, CTXT_Object *context)
/* LCOV_EXCL_STOP */
}

mpz_set_PyLong(MPZ(result), obj);
if (mpz_set_PyLong(MPZ(result), obj)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}

return result;
}
Expand Down Expand Up @@ -132,27 +148,21 @@ GMPy_PyLong_From_MPZ(MPZ_Object *obj, CTXT_Object *context)
return PyLong_FromLong(mpz_get_si(obj->z));
}

/* Assume gmp uses limbs as least as large as the builtin longs do */

size_t count, size = (mpz_sizeinbase(obj->z, 2) +
PyLong_SHIFT - 1) / PyLong_SHIFT;
PyLongObject *result;

if (!(result = _PyLong_New(size))) {
size_t size = (mpz_sizeinbase(obj->z, 2) +
int_bits_per_digit - 1) / int_bits_per_digit;
void *digits;
PyLongWriter *writer = PyLongWriter_Create(mpz_sgn(obj->z) < 0, size,
&digits);
if (writer == NULL) {
/* LCOV_EXCL_START */
return NULL;
/* LCOV_EXCL_STOP */
}

mpz_export(GET_OB_DIGIT(result), &count, -1, sizeof(digit), 0,
sizeof(digit)*8 - PyLong_SHIFT, obj->z);

for (size_t i = count; i < size; i++) {
GET_OB_DIGIT(result)[i] = 0;
}
_PyLong_SetSignAndDigitCount(result, mpz_sgn(obj->z) < 0, count);
mpz_export(digits, NULL, int_digits_order, int_digit_size,
int_endianness, int_nails, obj->z);

return (PyObject*)result;
return PyLongWriter_Finish(writer);
}

static PyObject *
Expand Down Expand Up @@ -365,7 +375,12 @@ GMPy_XMPZ_From_PyLong(PyObject *obj, CTXT_Object *context)
/* LCOV_EXCL_STOP */
}

mpz_set_PyLong(result->z, obj);
if (mpz_set_PyLong(result->z, obj)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}

return result;
}
Expand Down Expand Up @@ -833,16 +848,26 @@ GMPy_MPQ_From_Fraction(PyObject* obj, CTXT_Object *context)
den = PyObject_GetAttrString(obj, "denominator");
if (!num || !PyLong_Check(num) || !den || !PyLong_Check(den)) {
SYSTEM_ERROR("Object does not appear to be Fraction");
Py_XDECREF(num);
Py_XDECREF(den);
Py_DECREF((PyObject*)result);
return NULL;
goto error;
}
if (mpz_set_PyLong(mpq_numref(result->q), num)) {
/* LCOV_EXCL_START */
goto error;
/* LCOV_EXCL_STOP */
}
if (mpz_set_PyLong(mpq_denref(result->q), den)) {
/* LCOV_EXCL_START */
goto error;
/* LCOV_EXCL_STOP */
}
mpz_set_PyLong(mpq_numref(result->q), num);
mpz_set_PyLong(mpq_denref(result->q), den);
Py_DECREF(num);
Py_DECREF(den);
return result;
error:
Py_XDECREF(num);
Py_XDECREF(den);
Py_DECREF((PyObject*)result);
return NULL;
}

static MPQ_Object*
Expand Down
18 changes: 16 additions & 2 deletions src/gmpy2_divmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ GMPy_Integer_DivModWithType(PyObject *x, int xtype, PyObject *y, int ytype,

if (error) {
/* Use quo->z as a temporary variable. */
mpz_set_PyLong(quo->z, y);
if (mpz_set_PyLong(quo->z, y)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)rem);
Py_DECREF((PyObject*)quo);
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_fdiv_qr(quo->z, rem->z, MPZ(x), quo->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down Expand Up @@ -94,7 +101,14 @@ GMPy_Integer_DivModWithType(PyObject *x, int xtype, PyObject *y, int ytype,
goto error;
}
else {
mpz_set_PyLong(quo->z, x);
if (mpz_set_PyLong(quo->z, x)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)rem);
Py_DECREF((PyObject*)quo);
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_fdiv_qr(quo->z, rem->z, quo->z, MPZ(y));
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down
14 changes: 12 additions & 2 deletions src/gmpy2_floordiv.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ GMPy_Integer_FloorDivWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}
}
else {
mpz_set_PyLong(result->z, y);
if (mpz_set_PyLong(result->z, y)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_fdiv_q(result->z, MPZ(x), result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand All @@ -91,7 +96,12 @@ GMPy_Integer_FloorDivWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}

if (IS_TYPE_PyInteger(xtype)) {
mpz_set_PyLong(result->z, x);
if (mpz_set_PyLong(result->z, x)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_fdiv_q(result->z, result->z, MPZ(y));
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down
14 changes: 12 additions & 2 deletions src/gmpy2_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ GMPy_Integer_ModWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}
}
else {
mpz_set_PyLong(result->z, y);
if (mpz_set_PyLong(result->z, y)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_fdiv_r(result->z, MPZ(x), result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand All @@ -90,7 +95,12 @@ GMPy_Integer_ModWithType(PyObject *x, int xtype, PyObject *y, int ytype,
}

if (PyLong_Check(x)) {
mpz_set_PyLong(result->z, x);
if (mpz_set_PyLong(result->z, x)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
mpz_fdiv_r(result->z, result->z, MPZ(y));
return (PyObject*)result;
}
Expand Down
14 changes: 12 additions & 2 deletions src/gmpy2_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ GMPy_Integer_MulWithType(PyObject *x, int xtype, PyObject *y, int ytype,
mpz_mul_si(result->z, MPZ(x), temp);
}
else {
mpz_set_PyLong(result->z, y);
if (mpz_set_PyLong(result->z, y)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_mul(result->z, MPZ(x), result->z);
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand All @@ -77,7 +82,12 @@ GMPy_Integer_MulWithType(PyObject *x, int xtype, PyObject *y, int ytype,
mpz_mul_si(result->z, MPZ(y), temp);
}
else {
mpz_set_PyLong(result->z, x);
if (mpz_set_PyLong(result->z, x)) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)result);
return NULL;
/* LCOV_EXCL_STOP */
}
GMPY_MAYBE_BEGIN_ALLOW_THREADS(context);
mpz_mul(result->z, result->z, MPZ(y));
GMPY_MAYBE_END_ALLOW_THREADS(context);
Expand Down
7 changes: 6 additions & 1 deletion src/gmpy2_richcompare.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ GMPy_RichCompare_Slot(PyObject *a, PyObject *b, int op)
else {
mpz_t tempz;
mpz_init(tempz);
mpz_set_PyLong(tempz, b);
if (mpz_set_PyLong(tempz, b)) {
/* LCOV_EXCL_START */
mpz_clear(tempz);
return NULL;
/* LCOV_EXCL_STOP */
}
c = mpz_cmp(MPZ(a), tempz);
mpz_clear(tempz);
}
Expand Down
Loading
Loading