Skip to content

Commit

Permalink
Merge pull request #471 from skirpichev/misc
Browse files Browse the repository at this point in the history
Misc fixes
  • Loading branch information
casevh authored Mar 24, 2024
2 parents 712b3f0 + 283c665 commit 38bf13e
Show file tree
Hide file tree
Showing 15 changed files with 1,661 additions and 618 deletions.
20 changes: 0 additions & 20 deletions build-wheels.conf

This file was deleted.

445 changes: 371 additions & 74 deletions docs/history.rst

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ or some specific version with::

pip install gmpy2==2.1.5


From Sources
------------

If pre-compiled binary wheels aren't available for your platform, the pip will
fallback to installation from sources. In this case you will need to have
required libraries (GMP, MPFR and MPC) already installed on your system, along
Expand All @@ -25,9 +29,6 @@ systed-wide with::

pacman -S gcc gmp-devel mpfr-devel mpc-devel python-setuptools python-pip

From Sources
------------

If you are a developer or like to get the latest updates as they come, be sure
to install from the git repository and include required extra dependencies,
for example the optional "tests" list, which include packages required
Expand Down
427 changes: 0 additions & 427 deletions src/gmpy2.c

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions src/gmpy2_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ GMPy_MPZ_NewInit(PyTypeObject *type, PyObject *args, PyObject *keywds)
if (!MPZ_Check(out)) {
PyErr_Format(PyExc_TypeError,
"object of type '%.200s' can not be interpreted as mpz",
out->ob_type->tp_name);
Py_TYPE(out)->tp_name);
Py_DECREF(out);
return NULL;
}
Expand Down Expand Up @@ -186,7 +186,7 @@ GMPy_MPZ_Dealloc(MPZ_Object *self)
}
else {
mpz_clear(self->z);
PyObject_Del(self);
PyObject_Free(self);
}
}

Expand Down Expand Up @@ -322,7 +322,7 @@ GMPy_XMPZ_Dealloc(XMPZ_Object *self)
}
else {
mpz_clear(self->z);
PyObject_Del((PyObject*)self);
PyObject_Free((PyObject*)self);
}
}

Expand Down Expand Up @@ -453,7 +453,7 @@ GMPy_MPQ_Dealloc(MPQ_Object *self)
}
else {
mpq_clear(self->q);
PyObject_Del(self);
PyObject_Free(self);
}
}

Expand Down Expand Up @@ -572,7 +572,7 @@ GMPy_MPFR_NewInit(PyTypeObject *type, PyObject *args, PyObject *keywds)
if (!MPFR_Check(out)) {
PyErr_Format(PyExc_TypeError,
"object of type '%.200s' can not be interpreted as mpfr",
out->ob_type->tp_name);
Py_TYPE(out)->tp_name);
Py_DECREF(out);
return NULL;
}
Expand Down Expand Up @@ -614,7 +614,7 @@ GMPy_MPFR_Dealloc(MPFR_Object *self)
}
else {
mpfr_clear(self->f);
PyObject_Del(self);
PyObject_Free(self);
}
}

Expand Down Expand Up @@ -757,7 +757,7 @@ GMPy_MPC_NewInit(PyTypeObject *type, PyObject *args, PyObject *keywds)
if (!MPC_Check(out)) {
PyErr_Format(PyExc_TypeError,
"object of type '%.200s' can not be interpreted as mpc",
out->ob_type->tp_name);
Py_TYPE(out)->tp_name);
Py_DECREF(out);
return NULL;
}
Expand Down Expand Up @@ -889,7 +889,7 @@ GMPy_MPC_Dealloc(MPC_Object *self)
}
else {
mpc_clear(self->c);
PyObject_Del(self);
PyObject_Free(self);
}
}

9 changes: 6 additions & 3 deletions src/gmpy2_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
* GMPy_current_context
*/


#include "pythoncapi_compat.h"

/* Create and delete Context objects. */

static PyObject *
Expand Down Expand Up @@ -85,7 +88,7 @@ GMPy_CTXT_New(void)
static void
GMPy_CTXT_Dealloc(CTXT_Object *self)
{
PyObject_Del(self);
PyObject_Free(self);
};

/* Begin support for context vars. */
Expand Down Expand Up @@ -657,7 +660,7 @@ GMPy_CTXT_Set_##NAME(CTXT_Object *self, PyObject *value, void *closure) \
TYPE_ERROR(#NAME " must be True or False"); \
return -1; \
} \
self->ctx.NAME = (value == Py_True) ? 1 : 0; \
self->ctx.NAME = Py_IsTrue(value) ? 1 : 0; \
return 0; \
}

Expand All @@ -678,7 +681,7 @@ GMPy_CTXT_Set_##NAME(CTXT_Object *self, PyObject *value, void *closure) \
TYPE_ERROR(#NAME " must be True or False"); \
return -1; \
} \
if (value == Py_True) \
if (Py_IsTrue(value)) \
self->ctx.traps |= TRAP; \
else \
self->ctx.traps &= ~(TRAP); \
Expand Down
4 changes: 1 addition & 3 deletions src/gmpy2_convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,10 @@ extern "C" {

#if PY_VERSION_HEX >= 0x030C0000
# define GET_OB_DIGIT(obj) obj->long_value.ob_digit
# define _PyLong_IsNegative(obj) ((obj->long_value.lv_tag & 3) == 2)
# define _PyLong_DigitCount(obj) (obj->long_value.lv_tag >> 3)
#else
# define GET_OB_DIGIT(obj) obj->ob_digit
# define _PyLong_IsNegative(obj) (Py_SIZE(obj) < 0)
# define _PyLong_DigitCount(obj) (_PyLong_IsNegative(obj)? -Py_SIZE(obj):Py_SIZE(obj))
# 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
Expand Down
73 changes: 18 additions & 55 deletions src/gmpy2_convert_gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,6 @@
* Conversion between native Python objects and MPZ. *
* ======================================================================== */

static MPZ_Object *
GMPy_MPZ_From_PyLong(PyObject *obj, CTXT_Object *context)
{
MPZ_Object *result;
int negative;
Py_ssize_t len;
PyLongObject *templong = (PyLongObject*)obj;

if(!(result = GMPy_MPZ_New(context))) {
/* LCOV_EXCL_START */
return NULL;
/* LCOV_EXCL_STOP */
}

len = _PyLong_DigitCount(templong);
negative = _PyLong_IsNegative(templong);

switch (len) {
case 1:
mpz_set_si(result->z, (sdigit)GET_OB_DIGIT(templong)[0]);
break;
case 0:
mpz_set_si(result->z, 0);
break;
default:
mpz_import(result->z, len, -1, sizeof(GET_OB_DIGIT(templong)[0]), 0,
sizeof(GET_OB_DIGIT(templong)[0])*8 - PyLong_SHIFT,
GET_OB_DIGIT(templong));
}

if (negative) {
mpz_neg(result->z, result->z);
}
return result;
}

/* To support creation of temporary mpz objects. */
static void
mpz_set_PyLong(mpz_t z, PyObject *obj)
Expand All @@ -83,7 +47,7 @@ mpz_set_PyLong(mpz_t z, PyObject *obj)
PyLongObject *templong = (PyLongObject*)obj;

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

switch (len) {
case 1:
Expand All @@ -104,6 +68,22 @@ mpz_set_PyLong(mpz_t z, PyObject *obj)
return;
}

static MPZ_Object *
GMPy_MPZ_From_PyLong(PyObject *obj, CTXT_Object *context)
{
MPZ_Object *result;

if(!(result = GMPy_MPZ_New(context))) {
/* LCOV_EXCL_START */
return NULL;
/* LCOV_EXCL_STOP */
}

mpz_set_PyLong(MPZ(result), obj);

return result;
}

static MPZ_Object *
GMPy_MPZ_From_PyStr(PyObject *s, int base, CTXT_Object *context)
{
Expand Down Expand Up @@ -393,25 +373,8 @@ GMPy_XMPZ_From_PyLong(PyObject *obj, CTXT_Object *context)
/* LCOV_EXCL_STOP */
}

len = _PyLong_DigitCount(templong);
negative = _PyLong_IsNegative(templong);

switch (len) {
case 1:
mpz_set_si(result->z, (sdigit)GET_OB_DIGIT(templong)[0]);
break;
case 0:
mpz_set_si(result->z, 0);
break;
default:
mpz_import(result->z, len, -1, sizeof(GET_OB_DIGIT(templong)[0]), 0,
sizeof(GET_OB_DIGIT(templong)[0])*8 - PyLong_SHIFT,
GET_OB_DIGIT(templong));
}
mpz_set_PyLong(result->z, obj);

if (negative) {
mpz_neg(result->z, result->z);
}
return result;
}

Expand Down
29 changes: 15 additions & 14 deletions src/gmpy2_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* License along with GMPY2; if not, see <http://www.gnu.org/licenses/> *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


#include "pythoncapi_compat.h"

static Py_hash_t
GMPy_MPZ_Hash_Slot(MPZ_Object *self)
{
Expand All @@ -33,7 +36,7 @@ GMPy_MPZ_Hash_Slot(MPZ_Object *self)
return self->hash_cache;
}

hash = (Py_hash_t)mpn_mod_1(self->z->_mp_d, (mp_size_t)mpz_size(self->z), _PyHASH_MODULUS);
hash = (Py_hash_t)mpn_mod_1(self->z->_mp_d, (mp_size_t)mpz_size(self->z), PyHASH_MODULUS);
if (mpz_sgn(self->z) < 0) {
hash = -hash;
}
Expand All @@ -57,14 +60,14 @@ GMPy_MPQ_Hash_Slot(MPQ_Object *self)
mpz_init(temp1);
mpz_init(mask);
mpz_set_si(mask, 1);
mpz_mul_2exp(mask, mask, _PyHASH_BITS);
mpz_mul_2exp(mask, mask, PyHASH_BITS);
mpz_sub_ui(mask, mask, 1);

if (!mpz_invert(temp, mpq_denref(self->q), mask)) {
mpz_clear(temp);
mpz_clear(temp1);
mpz_clear(mask);
hash = _PyHASH_INF;
hash = PyHASH_INF;
if (mpz_sgn(mpq_numref(self->q)) < 0) {
hash = -hash;
}
Expand All @@ -77,7 +80,7 @@ GMPy_MPQ_Hash_Slot(MPQ_Object *self)

mpz_tdiv_r(temp1, mpq_numref(self->q), mask);
mpz_mul(temp, temp, temp1);
hash = (Py_hash_t)mpn_mod_1(temp->_mp_d, (mp_size_t)mpz_size(temp), _PyHASH_MODULUS);
hash = (Py_hash_t)mpn_mod_1(temp->_mp_d, (mp_size_t)mpz_size(temp), PyHASH_MODULUS);

if (mpz_sgn(mpq_numref(self->q)) < 0) {
hash = -hash;
Expand All @@ -104,17 +107,15 @@ _mpfr_hash(mpfr_t f)
if (!mpfr_number_p(f)) {
if (mpfr_inf_p(f)) {
if (mpfr_sgn(f) > 0) {
return _PyHASH_INF;
return PyHASH_INF;
}
else {
return -_PyHASH_INF;
return -PyHASH_INF;
}
}
else {
#if PY_VERSION_HEX >= 0x030A00A0
/* The default object hash implementation in the CPython
* accepts void* pointer. */
return PyBaseObject_Type.tp_hash((PyObject*)f);
return Py_HashPointer(f);
#else
return _PyHASH_NAN;
#endif
Expand All @@ -126,11 +127,11 @@ _mpfr_hash(mpfr_t f)

/* Calculate the hash of the mantissa. */
if (mpfr_sgn(f) > 0) {
hash = mpn_mod_1(f->_mpfr_d, (mp_size_t)msize, _PyHASH_MODULUS);
hash = mpn_mod_1(f->_mpfr_d, (mp_size_t)msize, PyHASH_MODULUS);
sign = 1;
}
else if (mpfr_sgn(f) < 0) {
hash = mpn_mod_1(f->_mpfr_d, (mp_size_t)msize, _PyHASH_MODULUS);
hash = mpn_mod_1(f->_mpfr_d, (mp_size_t)msize, PyHASH_MODULUS);
sign = -1;
}
else {
Expand All @@ -139,8 +140,8 @@ _mpfr_hash(mpfr_t f)

/* Calculate the final hash. */
exp = f->_mpfr_exp - (msize * mp_bits_per_limb);
exp = exp >= 0 ? exp % _PyHASH_BITS : _PyHASH_BITS-1-((-1-exp) % _PyHASH_BITS);
hash = ((hash << exp) & _PyHASH_MODULUS) | hash >> (_PyHASH_BITS - exp);
exp = exp >= 0 ? exp % PyHASH_BITS : PyHASH_BITS-1-((-1-exp) % PyHASH_BITS);
hash = ((hash << exp) & PyHASH_MODULUS) | hash >> (PyHASH_BITS - exp);

hash *= sign;
if (hash == (Py_uhash_t)(-1)) {
Expand Down Expand Up @@ -169,7 +170,7 @@ GMPy_MPC_Hash_Slot(MPC_Object *self)

hashreal = (Py_uhash_t)_mpfr_hash(mpc_realref(self->c));
hashimag = (Py_uhash_t)_mpfr_hash(mpc_imagref(self->c));
combined = hashreal + _PyHASH_IMAG * hashimag;
combined = hashreal + PyHASH_IMAG * hashimag;
if (combined == (Py_uhash_t)(-1)) {
combined = (Py_uhash_t)(-2);
}
Expand Down
Loading

0 comments on commit 38bf13e

Please sign in to comment.