Skip to content

Commit

Permalink
sync with CPython API
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Sep 4, 2024
1 parent 697f164 commit 00bbdc4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
17 changes: 9 additions & 8 deletions src/gmpy2_convert_gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ mpz_set_PyLong(mpz_t z, PyObject *obj)
static PyLong_DigitArray long_export;

PyLong_AsDigitArray(obj, &long_export);
const Py_digit *digits = long_export.digits;
if (long_export.ndigits == 1) {
mpz_set_si(z, long_export.digits[0]);
mpz_set_si(z, digits[0]);
}
else {
PyLongLayout* layout = long_export.layout;
mpz_import(z, long_export.ndigits, layout->array_endian,
layout->digit_size, layout->word_endian,
const PyLongLayout* layout = long_export.layout;
mpz_import(z, long_export.ndigits, layout->endian,
layout->digit_size, layout->digits_order,
layout->digit_size*8 - layout->bits_per_digit,
long_export.digits);
digits);
}
if (long_export.negative) {
mpz_neg(z, z);
Expand Down Expand Up @@ -130,7 +131,7 @@ GMPy_PyLong_From_MPZ(MPZ_Object *obj, CTXT_Object *context)
const PyLongLayout *layout = PyLong_GetNativeLayout();
size_t size = (mpz_sizeinbase(obj->z, 2) +
layout->bits_per_digit - 1) / layout->bits_per_digit;
Py_digit *digits;
void *digits;
PyLongWriter *writer = PyLongWriter_Create(mpz_sgn(obj->z) < 0, size,
&digits, layout);
if (writer == NULL) {
Expand All @@ -139,8 +140,8 @@ GMPy_PyLong_From_MPZ(MPZ_Object *obj, CTXT_Object *context)
/* LCOV_EXCL_STOP */
}

mpz_export(digits, NULL, layout->array_endian,
layout->digit_size, layout->word_endian,
mpz_export(digits, NULL, layout->endian,
layout->digit_size, layout->digits_order,
layout->digit_size*8 - layout->bits_per_digit,
obj->z);

Expand Down
12 changes: 6 additions & 6 deletions src/pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1214,14 +1214,14 @@ typedef digit Py_digit;
typedef struct PyLongLayout {
uint8_t bits_per_digit;
uint8_t digit_size;
int8_t word_endian;
int8_t array_endian;
int8_t digits_order;
int8_t endian;
} PyLongLayout;

const PyLongLayout PyLong_LAYOUT = {
.bits_per_digit = PyLong_SHIFT,
.word_endian = PY_LITTLE_ENDIAN ? -1 : 1,
.array_endian = -1, // least significant first
.digits_order = PY_LITTLE_ENDIAN ? -1 : 1,
.endian = -1, // least significant first
.digit_size = sizeof(digit),
};

Expand All @@ -1234,7 +1234,7 @@ typedef struct PyLong_DigitArray {
PyObject *obj;
int negative;
size_t ndigits;
const Py_digit *digits;
const void *digits;
const PyLongLayout *layout;
} PyLong_DigitArray;

Expand Down Expand Up @@ -1288,7 +1288,7 @@ PyLong_FreeDigitArray(PyLong_DigitArray *array)
}

static inline PyLongWriter*
PyLongWriter_Create(int negative, Py_ssize_t ndigits, Py_digit **digits,
PyLongWriter_Create(int negative, Py_ssize_t ndigits, void **digits,
const PyLongLayout *layout)
{
if (ndigits < 0) {
Expand Down

0 comments on commit 00bbdc4

Please sign in to comment.