static PyObject * GMPy_MPZ_Xor_Slot(PyObject *self, PyObject *other) { MPZ_Object *result; if (CHECK_MPZANY(self)) { if (CHECK_MPZANY(other)) { if (!(result = GMPy_MPZ_New(NULL))) return NULL; mpz_xor(result->z, MPZ(self), MPZ(other)); } else { if (!(result = GMPy_MPZ_From_Integer(other, NULL))) return NULL; mpz_xor(result->z, MPZ(self), result->z); } } else if (CHECK_MPZANY(other)) { if (!(result = GMPy_MPZ_From_Integer(self, NULL))) return NULL; mpz_xor(result->z, result->z, MPZ(other)); } else { Py_RETURN_NOTIMPLEMENTED; } return (PyObject*)result; }
static PyObject * GMPy_MPZ_FloorDiv_Slot(PyObject *x, PyObject *y) { if (CHECK_MPZANY(x) && CHECK_MPZANY(y)) { MPZ_Object *result; if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); return NULL; } if ((result = GMPy_MPZ_New(NULL))) { mpz_fdiv_q(result->z, MPZ(x), MPZ(y)); } return (PyObject*)result; } if (IS_INTEGER(x) && IS_INTEGER(y)) return GMPy_Integer_FloorDiv(x, y, NULL); if (IS_RATIONAL(x) && IS_RATIONAL(y)) return GMPy_Rational_FloorDiv(x, y, NULL); if (IS_REAL(x) && IS_REAL(y)) return GMPy_Real_FloorDiv(x, y, NULL); if (IS_COMPLEX(x) && IS_COMPLEX(y)) return GMPy_Complex_FloorDiv(x, y, NULL); Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_MPZ_Add_Slot(PyObject *x, PyObject *y) { if (CHECK_MPZANY(x) && CHECK_MPZANY(y)) { MPZ_Object *result; if ((result = GMPy_MPZ_New(NULL))) { mpz_add(result->z, MPZ(x), MPZ(y)); } return (PyObject*)result; } if (IS_INTEGER(x) && IS_INTEGER(y)) return GMPy_Integer_Add(x, y, NULL); if (IS_RATIONAL(x) && IS_RATIONAL(y)) return GMPy_Rational_Add(x, y, NULL); if (IS_REAL(x) && IS_REAL(y)) return GMPy_Real_Add(x, y, NULL); if (IS_COMPLEX(x) && IS_COMPLEX(y)) return GMPy_Complex_Add(x, y, NULL); Py_RETURN_NOTIMPLEMENTED; }
static PyObject * Pympany_printf(PyObject *self, PyObject *args) { PyObject *result = 0, *x = 0; char *buffer = 0, *fmtcode = 0; int buflen; void *generic; if (!PyArg_ParseTuple(args, "sO", &fmtcode, &x)) return NULL; if (CHECK_MPZANY(x) || Pympq_Check(x)) { if (CHECK_MPZANY(x)) generic = Pympz_AS_MPZ(x); else generic = Pympq_AS_MPQ(x); buflen = gmp_asprintf(&buffer, fmtcode, generic); result = Py_BuildValue("s", buffer); PyMem_Free(buffer); return result; } #ifdef WITHMPFR else if(Pympfr_Check(x)) { generic = Pympfr_AS_MPFR(x); buflen = mpfr_asprintf(&buffer, fmtcode, generic); result = Py_BuildValue("s", buffer); PyMem_Free(buffer); return result; } #endif else { TYPE_ERROR("printf() requires a gmpy2 object as argument"); return NULL; } }
static PyObject * Pygmpy_t_divmod(PyObject *self, PyObject *args) { PyObject *x, *y, *result; PympzObject *q, *r, *tempx, *tempy; if (PyTuple_GET_SIZE(args) != 2) { TYPE_ERROR("t_divmod() requires 'mpz','mpz' arguments"); return NULL; } x = PyTuple_GET_ITEM(args, 0); y = PyTuple_GET_ITEM(args, 1); CREATE_TWO_MPZ_TUPLE(q, r, result); if (CHECK_MPZANY(x) && CHECK_MPZANY(y)) { if (mpz_sgn(Pympz_AS_MPZ(y)) == 0) { ZERO_ERROR("t_divmod() division by 0"); Py_DECREF((PyObject*)q); Py_DECREF((PyObject*)r); Py_DECREF(result); return NULL; } mpz_tdiv_qr(q->z, r->z, Pympz_AS_MPZ(x), Pympz_AS_MPZ(y)); } else { tempx = Pympz_From_Integer(x); tempy = Pympz_From_Integer(y); if (!tempx || !tempy) { TYPE_ERROR("t_divmod() requires 'mpz','mpz' arguments"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)q); Py_DECREF((PyObject*)r); Py_DECREF(result); return NULL; } if (mpz_sgn(Pympz_AS_MPZ(tempy)) == 0) { ZERO_ERROR("t_divmod() division by 0"); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); Py_DECREF((PyObject*)q); Py_DECREF((PyObject*)r); Py_DECREF(result); return NULL; } mpz_tdiv_qr(q->z, r->z, tempx->z, tempy->z); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); } PyTuple_SET_ITEM(result, 0, (PyObject*)q); PyTuple_SET_ITEM(result, 1, (PyObject*)r); return result; }
static MPC_Object * GMPy_MPC_From_MPZ(MPZ_Object *obj, mpfr_prec_t rprec, mpfr_prec_t iprec, CTXT_Object *context) { MPC_Object *result = NULL; assert(CHECK_MPZANY(obj)); CHECK_CONTEXT(context); if (rprec == 0 || rprec == 1) rprec = GET_REAL_PREC(context) + rprec * GET_GUARD_BITS(context); if (iprec == 0 || iprec == 1) iprec = GET_IMAG_PREC(context) + iprec * GET_GUARD_BITS(context); if ((result = GMPy_MPC_New(rprec, iprec, context))) { result->rc = mpc_set_z(result->c, obj->z, GET_MPC_ROUND(context)); if (rprec != 1) { GMPY_MPC_CHECK_RANGE(result, context); } GMPY_MPC_SUBNORMALIZE(result, context); GMPY_MPC_EXCEPTIONS(result, context); } return result; }
static PyObject * GMPy_MPZ_Lshift_Slot(PyObject *self, PyObject *other) { mp_bitcnt_t count; MPZ_Object *result, *tempx; count = mp_bitcnt_t_From_Integer(other); if ((count == (mp_bitcnt_t)(-1)) && PyErr_Occurred()) return NULL; if (!(result = GMPy_MPZ_New(NULL))) return NULL; if (CHECK_MPZANY(self)) { mpz_mul_2exp(result->z, MPZ(self), count); return (PyObject*)result; } else { if (!(tempx = GMPy_MPZ_From_Integer(self, NULL))) { Py_XDECREF((PyObject*)result); Py_XDECREF((PyObject*)tempx); return NULL; } mpz_mul_2exp(result->z, tempx->z, count); Py_DECREF((PyObject*)tempx); return (PyObject*)result; } }
static PyObject * GMPy_MPZ_Function_Isqrt(PyObject *self, PyObject *other) { MPZ_Object *result; if (CHECK_MPZANY(other)) { if (mpz_sgn(MPZ(other)) < 0) { VALUE_ERROR("isqrt() of negative number"); return NULL; } if ((result = GMPy_MPZ_New(NULL))) { mpz_sqrt(result->z, MPZ(other)); } } else { if (!(result = GMPy_MPZ_From_Integer(other, NULL))) { TYPE_ERROR("isqrt() requires 'mpz' argument"); return NULL; } if (mpz_sgn(result->z) < 0) { VALUE_ERROR("isqrt() of negative number"); Py_DECREF((PyObject*)result); return NULL; } mpz_sqrt(result->z, result->z); } return (PyObject*)result; }
static PyObject * GMPy_MPZ_Function_IsOdd(PyObject *self, PyObject *other) { int res; MPZ_Object *tempx; if (CHECK_MPZANY(other)) { res = mpz_odd_p(MPZ(other)); } else { if (!(tempx = GMPy_MPZ_From_Integer(other, NULL))) { TYPE_ERROR("is_odd() requires 'mpz' argument"); return NULL; } else { res = mpz_odd_p(tempx->z); Py_DECREF((PyObject*)tempx); } } if (res) Py_RETURN_TRUE; else Py_RETURN_FALSE; }
static PyObject * GMPy_XMPZ_IAdd_Slot(PyObject *self, PyObject *other) { /* Try to make mpz + small_int faster */ if (PyIntOrLong_Check(other)) { int error; long temp = GMPy_Integer_AsLongAndError(other, &error); if (!error) { if (temp >= 0) { mpz_add_ui(MPZ(self), MPZ(self), temp); } else { mpz_sub_ui(MPZ(self), MPZ(self), -temp); } } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, other); mpz_add(MPZ(self), MPZ(self), tempz); mpz_cloc(tempz); } Py_INCREF(self); return self; } if (CHECK_MPZANY(other)) { mpz_add(MPZ(self), MPZ(self), MPZ(other)); Py_INCREF(self); return self; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_XMPZ_IMul_Slot(PyObject *self, PyObject *other) { if (PyIntOrLong_Check(other)) { int error; long temp = GMPy_Integer_AsLongAndError(other, &error); if (!error) { mpz_mul_si(MPZ(self), MPZ(self), temp); } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, other); mpz_mul(MPZ(self), MPZ(self), tempz); mpz_cloc(tempz); } Py_INCREF(self); return self; } if (CHECK_MPZANY(other)) { mpz_mul(MPZ(self), MPZ(self), MPZ(other)); Py_INCREF(self); return self; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * Pyxmpz_inplace_sub(PyObject *a, PyObject *b) { mpz_t tempz; mpir_si temp_si; int overflow; if (PyIntOrLong_Check(b)) { temp_si = PyLong_AsSIAndOverflow(b, &overflow); if (overflow) { mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, b); mpz_sub(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), tempz); mpz_cloc(tempz); } else if(temp_si >= 0) { mpz_sub_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si); } else { mpz_add_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), -temp_si); } Py_INCREF(a); return a; } if (CHECK_MPZANY(b)) { mpz_sub(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(b)); Py_INCREF(a); return a; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_MPZ_IMul_Slot(PyObject *self, PyObject *other) { MPZ_Object *rz; if (!(rz = GMPy_MPZ_New(NULL))) return NULL; if (CHECK_MPZANY(other)) { mpz_mul(rz->z, MPZ(self), MPZ(other)); return (PyObject*)rz; } if (PyIntOrLong_Check(other)) { int error; long temp = GMPy_Integer_AsLongAndError(other, &error); if (!error) { mpz_mul_si(rz->z, MPZ(self), temp); } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, other); mpz_mul(rz->z, MPZ(self), tempz); mpz_cloc(tempz); } return (PyObject*)rz; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_XMPZ_ISub_Slot(PyObject *self, PyObject *other) { if (PyIntOrLong_Check(other)) { int error; native_si temp = GMPy_Integer_AsNative_siAndError(other, &error); if (!error) { if (temp >= 0) { mpz_sub_ui(MPZ(self), MPZ(self), temp); } else { mpz_add_ui(MPZ(self), MPZ(self), -temp); } } else { mpz_set_PyIntOrLong(global.tempz, other); mpz_sub(MPZ(self), MPZ(self), global.tempz); } Py_INCREF(self); return self; } if (CHECK_MPZANY(other)) { mpz_sub(MPZ(self), MPZ(self), MPZ(other)); Py_INCREF(self); return self; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * Pygmpy_c_div(PyObject *self, PyObject *args) { PyObject *x, *y; PympzObject *q, *tempx, *tempy; if (PyTuple_GET_SIZE(args) != 2) { TYPE_ERROR("c_div() requires 'mpz','mpz' arguments"); return NULL; } x = PyTuple_GET_ITEM(args, 0); y = PyTuple_GET_ITEM(args, 1); if (!(q = Pympz_new())) return NULL; if (CHECK_MPZANY(x) && CHECK_MPZANY(y)) { if (mpz_sgn(Pympz_AS_MPZ(y)) == 0) { ZERO_ERROR("c_div() division by 0"); Py_DECREF((PyObject*)q); return NULL; } mpz_cdiv_q(q->z, Pympz_AS_MPZ(x), Pympz_AS_MPZ(y)); } else { tempx = Pympz_From_Integer(x); tempy = Pympz_From_Integer(y); if (!tempx || !tempy) { TYPE_ERROR("c_div() requires 'mpz','mpz' arguments"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)q); return NULL; } if (mpz_sgn(Pympz_AS_MPZ(tempy)) == 0) { ZERO_ERROR("c_div() division by 0"); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); Py_DECREF((PyObject*)q); return NULL; } mpz_cdiv_q(q->z, tempx->z, tempy->z); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); } return (PyObject*)q; }
static PyObject * GMPy_MPZ_IAdd_Slot(PyObject *self, PyObject *other) { MPZ_Object *result = NULL; if (CHECK_MPZANY(other)) { if ((result = GMPy_MPZ_New(NULL))) { mpz_add(result->z, MPZ(self), MPZ(other)); } return (PyObject*)result; } if (PyLong_CheckExact(other)) { if ((result = GMPy_MPZ_New(NULL))) { switch (Py_SIZE((PyLongObject*)other)) { case -1: mpz_sub_ui(result->z, MPZ(self), ((PyLongObject*)other)->ob_digit[0]); return (PyObject*)result; case 0: case 1: mpz_add_ui(result->z, MPZ(self), ((PyLongObject*)other)->ob_digit[0]); return (PyObject*)result; default: break; } } else { return NULL; } } if (PyIntOrLong_Check(other)) { int error; long temp = GMPy_Integer_AsLongAndError(other, &error); if ((result = GMPy_MPZ_New(NULL))) { if (!error) { if (temp >= 0) { mpz_add_ui(result->z, MPZ(self), temp); } else { mpz_sub_ui(result->z, MPZ(self), -temp); } } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, other); mpz_add(result->z, MPZ(self), tempz); mpz_cloc(tempz); } } return (PyObject*)result; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * Pympany_pow(PyObject *base, PyObject *exp, PyObject *mod) { #ifndef WITHMPFR PyObject *result = 0, *temp; #endif if (isInteger(base) && isInteger(exp)) return Pympz_pow(base, exp, mod); else if (isRational(base) && isRational(exp)) return Pympq_pow(base, exp, mod); #ifdef WITHMPFR else if (isReal(base) && isReal(exp)); return Pympfr2_pow(base, exp, mod); #else /* Support mpz**float and float**mpz. */ if (CHECK_MPZANY(base) && PyFloat_Check(exp)) { temp = PyFloat_FromDouble(mpz_get_d(Pympz_AS_MPZ(base))); if (temp) { result = PyNumber_Power(temp, exp, mod); Py_DECREF(temp); } return result; } if (CHECK_MPZANY(exp) && PyFloat_Check(base)) { temp = PyFloat_FromDouble(mpz_get_d(Pympz_AS_MPZ(exp))); if (temp) { result = PyNumber_Power(base, temp, mod); Py_DECREF(temp); } return result; } #endif Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_MPZ_IFloorDiv_Slot(PyObject *self, PyObject *other) { MPZ_Object *rz; if (!(rz = GMPy_MPZ_New(NULL))) return NULL; if (CHECK_MPZANY(other)) { if (mpz_sgn(MPZ(other)) == 0) { ZERO_ERROR("mpz division by zero"); return NULL; } mpz_fdiv_q(rz->z, MPZ(self), MPZ(other)); return (PyObject*)rz; } if (PyIntOrLong_Check(other)) { int error; long temp = GMPy_Integer_AsLongAndError(other, &error); if (!error) { if (temp == 0) { ZERO_ERROR("mpz division by zero"); return NULL; } else if(temp > 0) { mpz_fdiv_q_ui(rz->z, MPZ(self), temp); } else { mpz_cdiv_q_ui(rz->z, MPZ(self), -temp); mpz_neg(rz->z, rz->z); } } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, other); mpz_fdiv_q(rz->z, MPZ(self), tempz); mpz_cloc(tempz); } return (PyObject*)rz; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_XMPZ_IIor_Slot(PyObject *self, PyObject *other) { if(CHECK_MPZANY(other)) { mpz_ior(MPZ(self), MPZ(self), MPZ(other)); Py_INCREF(self); return self; } if(PyIntOrLong_Check(other)) { mpz_set_PyIntOrLong(global.tempz, other); mpz_ior(MPZ(self), MPZ(self), global.tempz); Py_INCREF(self); return self; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_XMPZ_IRem_Slot(PyObject *self, PyObject *other) { if (PyIntOrLong_Check(other)) { int error; long temp = GMPy_Integer_AsLongAndError(other, &error); if (!error) { if (temp > 0) { mpz_fdiv_r_ui(MPZ(self), MPZ(self), temp); } else if (temp == 0) { ZERO_ERROR("xmpz modulo by zero"); return NULL; } else { mpz_cdiv_r_ui(MPZ(self), MPZ(self), -temp); } } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, other); mpz_fdiv_r(MPZ(self), MPZ(self), tempz); mpz_cloc(tempz); } Py_INCREF(self); return self; } if (CHECK_MPZANY(other)) { if(mpz_sgn(MPZ(other)) == 0) { ZERO_ERROR("xmpz modulo by zero"); return NULL; } mpz_fdiv_r(MPZ(self), MPZ(self), MPZ(other)); Py_INCREF(self); return self; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * Pyxmpz_inplace_floordiv(PyObject *a, PyObject *b) { mpz_t tempz; mpir_si temp_si; int overflow; if (PyIntOrLong_Check(b)) { temp_si = PyLong_AsSIAndOverflow(b, &overflow); if (overflow) { mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, b); mpz_fdiv_q(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), tempz); mpz_cloc(tempz); } else if(temp_si == 0) { ZERO_ERROR("xmpz division by zero"); return NULL; } else if(temp_si > 0) { mpz_fdiv_q_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si); } else { mpz_cdiv_q_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), -temp_si); mpz_neg(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a)); } Py_INCREF(a); return a; } if (CHECK_MPZANY(b)) { if (mpz_sgn(Pyxmpz_AS_MPZ(b)) == 0) { ZERO_ERROR("xmpz division by zero"); return NULL; } mpz_fdiv_q(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(b)); Py_INCREF(a); return a; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_XMPZ_IFloorDiv_Slot(PyObject *self, PyObject *other) { if (PyIntOrLong_Check(other)) { int error; native_si temp = GMPy_Integer_AsNative_siAndError(other, &error); if (!error) { if (temp == 0) { ZERO_ERROR("xmpz division by zero"); return NULL; } else if(temp > 0) { mpz_fdiv_q_ui(MPZ(self), MPZ(self), temp); } else { mpz_cdiv_q_ui(MPZ(self), MPZ(self), -temp); mpz_neg(MPZ(self), MPZ(self)); } } else { mpz_set_PyIntOrLong(global.tempz, other); mpz_fdiv_q(MPZ(self), MPZ(self), global.tempz); } Py_INCREF(self); return self; } if (CHECK_MPZANY(other)) { if (mpz_sgn(MPZ(other)) == 0) { ZERO_ERROR("xmpz division by zero"); return NULL; } mpz_fdiv_q(MPZ(self), MPZ(self), MPZ(other)); Py_INCREF(self); return self; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * Pyxmpz_inplace_rshift(PyObject *a, PyObject *b) { mpir_si temp_si; int overflow; if (PyIntOrLong_Check(b)) { temp_si = PyLong_AsSIAndOverflow(b, &overflow); if (overflow) { OVERFLOW_ERROR("outrageous shift count"); return NULL; } else if(temp_si >= 0) { mpz_fdiv_q_2exp(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si); Py_INCREF(a); return a; } else { VALUE_ERROR("negative shift count"); return NULL; } } if (CHECK_MPZANY(b)) { if (mpz_sgn(Pyxmpz_AS_MPZ(b)) < 0) { VALUE_ERROR("negative shift count"); return NULL; } if (!mpz_fits_si_p(Pyxmpz_AS_MPZ(b))) { OVERFLOW_ERROR("outrageous shift count"); return NULL; } temp_si = mpz_get_si(Pyxmpz_AS_MPZ(b)); mpz_fdiv_q_2exp(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si); Py_INCREF(a); return a; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_XMPZ_IXor_Slot(PyObject *self, PyObject *other) { mpz_t tempz; if(CHECK_MPZANY(other)) { mpz_xor(MPZ(self), MPZ(self), MPZ(other)); Py_INCREF(self); return self; } if(PyIntOrLong_Check(other)) { mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, other); mpz_xor(MPZ(self), MPZ(self), tempz); mpz_cloc(tempz); Py_INCREF(self); return self; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * Pyxmpz_inplace_ior(PyObject *self, PyObject *other) { mpz_t tempz; if(CHECK_MPZANY(other)) { mpz_ior(Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(other)); Py_INCREF(self); return self; } if(PyIntOrLong_Check(other)) { mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, other); mpz_ior(Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(self), tempz); mpz_cloc(tempz); Py_INCREF(self); return self; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_Integer_Add(PyObject *x, PyObject *y, CTXT_Object *context) { MPZ_Object *result; if (!(result = GMPy_MPZ_New(context))) return NULL; if (CHECK_MPZANY(x)) { if (PyIntOrLong_Check(y)) { int error; long temp = GMPy_Integer_AsLongAndError(y, &error); if (!error) { if (temp >= 0) { mpz_add_ui(result->z, MPZ(x), temp); } else { mpz_sub_ui(result->z, MPZ(x), -temp); } } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, y); mpz_add(result->z, MPZ(x), tempz); mpz_cloc(tempz); } return (PyObject*)result; } if (CHECK_MPZANY(y)) { mpz_add(result->z, MPZ(x), MPZ(y)); return (PyObject*)result; } } if (CHECK_MPZANY(y)) { if (PyIntOrLong_Check(x)) { int error; long temp = GMPy_Integer_AsLongAndError(x, &error); if (!error) { if (temp >= 0) { mpz_add_ui(result->z, MPZ(y), temp); } else { mpz_sub_ui(result->z, MPZ(y), -temp); } return (PyObject*)result; } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, x); mpz_add(result->z, MPZ(y), tempz); mpz_cloc(tempz); return (PyObject*)result; } } } if (IS_INTEGER(x) && IS_INTEGER(y)) { MPZ_Object *tempx, *tempy; tempx = GMPy_MPZ_From_Integer(x, context); tempy = GMPy_MPZ_From_Integer(y, context); if (!tempx || !tempy) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } mpz_add(result->z, tempx->z, tempy->z); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); return (PyObject*)result; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_Real_Add(PyObject *x, PyObject *y, CTXT_Object *context) { MPFR_Object *result; CHECK_CONTEXT(context); if (!(result = GMPy_MPFR_New(0, context))) return NULL; if (MPFR_Check(x) && MPFR_Check(y)) { mpfr_clear_flags(); result->rc = mpfr_add(result->f, MPFR(x), MPFR(y), GET_MPFR_ROUND(context)); goto done; } if (MPFR_Check(x)) { if (PyIntOrLong_Check(y)) { mpz_t tempz; long temp; int error; temp = GMPy_Integer_AsLongAndError(y, &error); if (error) { mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, y); mpfr_clear_flags(); result->rc = mpfr_add_z(result->f, MPFR(x), tempz, GET_MPFR_ROUND(context)); mpz_cloc(tempz); goto done; } else { mpfr_clear_flags(); result->rc = mpfr_add_si(result->f, MPFR(x), temp, GET_MPFR_ROUND(context)); goto done; } } if (CHECK_MPZANY(y)) { mpfr_clear_flags(); result->rc = mpfr_add_z(result->f, MPFR(x), MPZ(y), GET_MPFR_ROUND(context)); goto done; } if (IS_RATIONAL(y)) { MPQ_Object *tempy; if (!(tempy = GMPy_MPQ_From_Number(y, context))) { Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_add_q(result->f, MPFR(x), tempy->q, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempy); goto done; } if (PyFloat_Check(y)) { mpfr_clear_flags(); result->rc = mpfr_add_d(result->f, MPFR(x), PyFloat_AS_DOUBLE(y), GET_MPFR_ROUND(context)); goto done; } } if (MPFR_Check(y)) { if (PyIntOrLong_Check(x)) { mpz_t tempz; long temp; int error; temp = GMPy_Integer_AsLongAndError(x, &error); if (error) { mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, x); mpfr_clear_flags(); result->rc = mpfr_add_z(result->f, MPFR(y), tempz, GET_MPFR_ROUND(context)); mpz_cloc(tempz); goto done; } else { mpfr_clear_flags(); result->rc = mpfr_add_si(result->f, MPFR(y), temp, GET_MPFR_ROUND(context)); goto done; } } if (CHECK_MPZANY(x)) { mpfr_clear_flags(); result->rc = mpfr_add_z(result->f, MPFR(y), MPZ(x), GET_MPFR_ROUND(context)); goto done; } if (IS_RATIONAL(x)) { MPQ_Object *tempx; if (!(tempx = GMPy_MPQ_From_Number(x, context))) { Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_add_q(result->f, MPFR(y), tempx->q, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); goto done; } if (PyFloat_Check(x)) { mpfr_clear_flags(); result->rc = mpfr_add_d(result->f, MPFR(y), PyFloat_AS_DOUBLE(x), GET_MPFR_ROUND(context)); goto done; } } if (IS_REAL(x) && IS_REAL(y)) { MPFR_Object *tempx, *tempy; tempx = GMPy_MPFR_From_Real(x, 1, context); tempy = GMPy_MPFR_From_Real(y, 1, context); if (!tempx || !tempy) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_add(result->f, MPFR(tempx), MPFR(tempy), GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); goto done; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; done: GMPY_MPFR_CLEANUP(result, context, "addition"); return (PyObject*)result; }
static PyObject * GMPy_Integer_Mod(PyObject *x, PyObject *y, CTXT_Object *context) { MPZ_Object *result; CHECK_CONTEXT(context); if (!(result = GMPy_MPZ_New(context))) return NULL; if (CHECK_MPZANY(x)) { if (PyIntOrLong_Check(y)) { int error; long temp = GMPy_Integer_AsLongAndError(y, &error); if (!error) { if (temp > 0) { mpz_fdiv_r_ui(result->z, MPZ(x), temp); } else if (temp == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)result); return NULL; } else { mpz_cdiv_r_ui(result->z, MPZ(x), -temp); } } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, y); mpz_fdiv_r(result->z, MPZ(x), tempz); mpz_cloc(tempz); } return (PyObject*)result; } if (CHECK_MPZANY(y)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)result); return NULL; } mpz_fdiv_r(result->z, MPZ(x), MPZ(y)); return (PyObject*)result; } } if (CHECK_MPZANY(y)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)result); return NULL; } if (PyIntOrLong_Check(x)) { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, x); mpz_fdiv_r(result->z, tempz, MPZ(y)); mpz_cloc(tempz); return (PyObject*)result; } } if (IS_INTEGER(x) && IS_INTEGER(y)) { MPZ_Object *tempx, *tempy; tempx = GMPy_MPZ_From_Integer(x, context); tempy = GMPy_MPZ_From_Integer(y, context); if (!tempx || !tempy) { SYSTEM_ERROR("could not convert Integer to mpz"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } if (mpz_sgn(tempy->z) == 0) { ZERO_ERROR("division or modulo by zero"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } mpz_fdiv_r(result->z, tempx->z, tempy->z); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); return (PyObject*)result; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_Real_FloorDiv(PyObject *x, PyObject *y, CTXT_Object *context) { MPFR_Object *result; CHECK_CONTEXT(context); if (!(result = GMPy_MPFR_New(0, context))) { /* LCOV_EXCL_START */ return NULL; /* LCOV_EXCL_STOP */ } if (MPFR_Check(x)) { if (MPFR_Check(y)) { mpfr_clear_flags(); result->rc = mpfr_div(result->f, MPFR(x), MPFR(y), GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } if (PyIntOrLong_Check(y)) { int error; long tempi = GMPy_Integer_AsLongAndError(y, &error); if (!error) { mpfr_clear_flags(); result->rc = mpfr_div_si(result->f, MPFR(x), tempi, GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } else { mpz_set_PyIntOrLong(global.tempz, y); mpfr_clear_flags(); result->rc = mpfr_div_z(result->f, MPFR(x), global.tempz, GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } } if (CHECK_MPZANY(y)) { mpfr_clear_flags(); result->rc = mpfr_div_z(result->f, MPFR(x), MPZ(y), GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } if (IS_RATIONAL(y)) { MPQ_Object *tempy; if (!(tempy = GMPy_MPQ_From_Number(y, context))) { Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_div_q(result->f, MPFR(x), tempy->q, GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); Py_DECREF((PyObject*)tempy); goto done; } if (PyFloat_Check(y)) { mpfr_clear_flags(); result->rc = mpfr_div_d(result->f, MPFR(x), PyFloat_AS_DOUBLE(y), GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } } if (MPFR_Check(y)) { if (PyIntOrLong_Check(x)) { int error; long tempi = GMPy_Integer_AsLongAndError(x, &error); if (!error) { mpfr_clear_flags(); result->rc = mpfr_si_div(result->f, tempi, MPFR(y), GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } } /* Since mpfr_z_div does not exist, this combination is handled at the * end by converting x to an mpfr. Ditto for rational.*/ if (PyFloat_Check(x)) { mpfr_clear_flags(); result->rc = mpfr_d_div(result->f, PyFloat_AS_DOUBLE(x), MPFR(y), GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } } /* Handle the remaining cases. * Note: verify that MPZ if converted at full precision! */ if (IS_REAL(x) && IS_REAL(y)) { MPFR_Object *tempx, *tempy; tempx = GMPy_MPFR_From_Real(x, 1, context); tempy = GMPy_MPFR_From_Real(y, 1, context); if (!tempx || !tempy) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_div(result->f, MPFR(tempx), MPFR(tempy), GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); goto done; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; done: _GMPy_MPFR_Cleanup(&result, context); return (PyObject*)result; }
static PyObject * GMPy_Integer_FloorDiv(PyObject *x, PyObject *y, CTXT_Object *context) { MPZ_Object *result; if (!(result = GMPy_MPZ_New(context))) return NULL; if (CHECK_MPZANY(x)) { if (PyIntOrLong_Check(y)) { int error; native_si temp = GMPy_Integer_AsNative_siAndError(y, &error); if (!error) { if (temp > 0) { mpz_fdiv_q_ui(result->z, MPZ(x), temp); } else if (temp == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)result); return NULL; } else { mpz_cdiv_q_ui(result->z, MPZ(x), -temp); mpz_neg(result->z, result->z); } } else { mpz_set_PyIntOrLong(global.tempz, y); mpz_fdiv_q(result->z, MPZ(x), global.tempz); } return (PyObject*)result; } if (CHECK_MPZANY(y)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)result); return NULL; } mpz_fdiv_q(result->z, MPZ(x), MPZ(y)); return (PyObject*)result; } } if (CHECK_MPZANY(y)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)result); return NULL; } if (PyIntOrLong_Check(x)) { mpz_set_PyIntOrLong(global.tempz, x); mpz_fdiv_q(result->z, global.tempz, MPZ(y)); return (PyObject*)result; } } if (IS_INTEGER(x) && IS_INTEGER(y)) { MPZ_Object *tempx, *tempy; tempx = GMPy_MPZ_From_Integer(x, context); tempy = GMPy_MPZ_From_Integer(y, context); if (!tempx || !tempy) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } if (mpz_sgn(tempy->z) == 0) { ZERO_ERROR("division or modulo by zero"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } mpz_fdiv_q(result->z, tempx->z, tempy->z); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); return (PyObject*)result; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; }