Skip to content

Commit

Permalink
Port more functions/methods to METH_FASTCALL convention
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Feb 3, 2024
1 parent 0eff868 commit a13038d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
8 changes: 4 additions & 4 deletions src/gmpy2.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,18 +754,18 @@ static PyMethodDef Pygmpy_methods [] =
{ "is_lucas_prp", GMPY_mpz_is_lucas_prp, METH_VARARGS, doc_mpz_is_lucas_prp },
{ "is_odd", GMPy_MPZ_Function_IsOdd, METH_O, GMPy_doc_mpz_function_is_odd },
{ "is_power", GMPy_MPZ_Function_IsPower, METH_O, GMPy_doc_mpz_function_is_power },
{ "is_prime", GMPy_MPZ_Function_IsPrime, METH_VARARGS, GMPy_doc_mpz_function_is_prime },
{ "is_prime", (PyCFunction)GMPy_MPZ_Function_IsPrime, METH_FASTCALL, GMPy_doc_mpz_function_is_prime },
{ "is_probab_prime", (PyCFunction)GMPy_MPZ_Function_IsProbabPrime, METH_FASTCALL, GMPy_doc_mpz_function_is_probab_prime },
{ "is_selfridge_prp", GMPY_mpz_is_selfridge_prp, METH_VARARGS, doc_mpz_is_selfridge_prp },
{ "is_square", GMPy_MPZ_Function_IsSquare, METH_O, GMPy_doc_mpz_function_is_square },
{ "is_strong_prp", GMPY_mpz_is_strong_prp, METH_VARARGS, doc_mpz_is_strong_prp },
{ "is_strong_bpsw_prp", GMPY_mpz_is_strongbpsw_prp, METH_VARARGS, doc_mpz_is_strongbpsw_prp },
{ "is_strong_lucas_prp", GMPY_mpz_is_stronglucas_prp, METH_VARARGS, doc_mpz_is_stronglucas_prp },
{ "is_strong_selfridge_prp", GMPY_mpz_is_strongselfridge_prp, METH_VARARGS, doc_mpz_is_strongselfridge_prp },
{ "jacobi", GMPy_MPZ_Function_Jacobi, METH_VARARGS, GMPy_doc_mpz_function_jacobi },
{ "kronecker", GMPy_MPZ_Function_Kronecker, METH_VARARGS, GMPy_doc_mpz_function_kronecker },
{ "jacobi", (PyCFunction)GMPy_MPZ_Function_Jacobi, METH_FASTCALL, GMPy_doc_mpz_function_jacobi },
{ "kronecker", (PyCFunction)GMPy_MPZ_Function_Kronecker, METH_FASTCALL, GMPy_doc_mpz_function_kronecker },
{ "lcm", (PyCFunction)GMPy_MPZ_Function_LCM, METH_FASTCALL, GMPy_doc_mpz_function_lcm },
{ "legendre", GMPy_MPZ_Function_Legendre, METH_VARARGS, GMPy_doc_mpz_function_legendre },
{ "legendre", (PyCFunction)GMPy_MPZ_Function_Legendre, METH_FASTCALL, GMPy_doc_mpz_function_legendre },
{ "license", GMPy_get_license, METH_NOARGS, GMPy_doc_license },
{ "lucas", GMPy_MPZ_Function_Lucas, METH_O, GMPy_doc_mpz_function_lucas },
{ "lucasu", GMPY_mpz_lucasu, METH_VARARGS, doc_mpz_lucasu },
Expand Down
2 changes: 1 addition & 1 deletion src/gmpy2_mpz.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static PyMethodDef GMPy_MPZ_methods[] = {
{ "is_even", GMPy_MPZ_Method_IsEven, METH_NOARGS, GMPy_doc_mpz_method_is_even },
{ "is_odd", GMPy_MPZ_Method_IsOdd, METH_NOARGS, GMPy_doc_mpz_method_is_odd },
{ "is_power", GMPy_MPZ_Method_IsPower, METH_NOARGS, GMPy_doc_mpz_method_is_power },
{ "is_prime", GMPy_MPZ_Method_IsPrime, METH_VARARGS, GMPy_doc_mpz_method_is_prime },
{ "is_prime", (PyCFunction)GMPy_MPZ_Method_IsPrime, METH_FASTCALL, GMPy_doc_mpz_method_is_prime },
{ "is_probab_prime", (PyCFunction)GMPy_MPZ_Method_IsProbabPrime, METH_FASTCALL, GMPy_doc_mpz_method_is_probab_prime },
{ "is_square", GMPy_MPZ_Method_IsSquare, METH_NOARGS, GMPy_doc_mpz_method_is_square },
{ "num_digits", (PyCFunction)GMPy_MPZ_Method_NumDigits, METH_FASTCALL, GMPy_doc_mpz_method_num_digits },
Expand Down
53 changes: 26 additions & 27 deletions src/gmpy2_mpz_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1321,22 +1321,20 @@ PyDoc_STRVAR(GMPy_doc_mpz_function_is_prime,
"to n Miller-Rabin tests are performed.");

static PyObject *
GMPy_MPZ_Function_IsPrime(PyObject *self, PyObject *args)
GMPy_MPZ_Function_IsPrime(PyObject *self, PyObject * const *args,
Py_ssize_t nargs)
{
int i;
unsigned long reps = 25;
MPZ_Object* tempx;
Py_ssize_t argc;

argc = PyTuple_GET_SIZE(args);

if (argc == 0 || argc > 2) {
if (nargs == 0 || nargs > 2) {
TYPE_ERROR("is_prime() requires 'mpz'[,'int'] arguments");
return NULL;
}

if (PyTuple_GET_SIZE(args) == 2) {
reps = GMPy_Integer_AsUnsignedLong(PyTuple_GET_ITEM(args, 1));
if (nargs == 2) {
reps = GMPy_Integer_AsUnsignedLong(args[1]);
if (reps == (unsigned long)(-1) && PyErr_Occurred()) {
return NULL;
}
Expand All @@ -1346,7 +1344,7 @@ GMPy_MPZ_Function_IsPrime(PyObject *self, PyObject *args)
}
}

if (!(tempx = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 0), NULL))) {
if (!(tempx = GMPy_MPZ_From_Integer(args[0], NULL))) {
return NULL;
}

Expand All @@ -1371,21 +1369,19 @@ PyDoc_STRVAR(GMPy_doc_mpz_method_is_prime,
"to n Miller-Rabin tests are performed.");

static PyObject *
GMPy_MPZ_Method_IsPrime(PyObject *self, PyObject *args)
GMPy_MPZ_Method_IsPrime(PyObject *self, PyObject * const *args,
Py_ssize_t nargs)
{
int i;
unsigned long reps = 25;
Py_ssize_t argc;

argc = PyTuple_GET_SIZE(args);

if (argc > 1) {
if (nargs > 1) {
TYPE_ERROR("is_prime() takes at most 1 argument");
return NULL;
}

if (PyTuple_GET_SIZE(args) == 1) {
reps = GMPy_Integer_AsUnsignedLong(PyTuple_GET_ITEM(args, 0));
if (nargs == 1) {
reps = GMPy_Integer_AsUnsignedLong(args[0]);
if (reps == (unsigned long)(-1) && PyErr_Occurred()) {
return NULL;
}
Expand Down Expand Up @@ -1559,18 +1555,19 @@ PyDoc_STRVAR(GMPy_doc_mpz_function_jacobi,
"Return the Jacobi symbol (x|y). y must be odd and >0.");

static PyObject *
GMPy_MPZ_Function_Jacobi(PyObject *self, PyObject *args)
GMPy_MPZ_Function_Jacobi(PyObject *self, PyObject *const *args,
Py_ssize_t nargs)
{
MPZ_Object *tempx = NULL, *tempy = NULL;
long res;

if (PyTuple_GET_SIZE(args) != 2) {
if (nargs != 2) {
TYPE_ERROR("jacobi() requires 'mpz','mpz' arguments");
return NULL;
}

if (!(tempx = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 0), NULL)) ||
!(tempy = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 1), NULL))) {
if (!(tempx = GMPy_MPZ_From_Integer(args[0], NULL)) ||
!(tempy = GMPy_MPZ_From_Integer(args[1], NULL))) {

Py_XDECREF((PyObject*)tempx);
Py_XDECREF((PyObject*)tempy);
Expand All @@ -1595,18 +1592,19 @@ PyDoc_STRVAR(GMPy_doc_mpz_function_legendre,
"Return the Legendre symbol (x|y). y is assumed to be an odd prime.");

static PyObject *
GMPy_MPZ_Function_Legendre(PyObject *self, PyObject *args)
GMPy_MPZ_Function_Legendre(PyObject *self, PyObject * const *args,
Py_ssize_t nargs)
{
MPZ_Object *tempx = NULL, *tempy = NULL;
long res;

if (PyTuple_GET_SIZE(args) != 2) {
if (nargs != 2) {
TYPE_ERROR("legendre() requires 'mpz','mpz' arguments");
return NULL;
}

if (!(tempx = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 0), NULL)) ||
!(tempy = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 1), NULL))) {
if (!(tempx = GMPy_MPZ_From_Integer(args[0], NULL)) ||
!(tempy = GMPy_MPZ_From_Integer(args[1], NULL))) {

Py_XDECREF((PyObject*)tempx);
Py_XDECREF((PyObject*)tempy);
Expand All @@ -1631,18 +1629,19 @@ PyDoc_STRVAR(GMPy_doc_mpz_function_kronecker,
"Return the Kronecker-Jacobi symbol (x|y).");

static PyObject *
GMPy_MPZ_Function_Kronecker(PyObject *self, PyObject *args)
GMPy_MPZ_Function_Kronecker(PyObject *self, PyObject * const *args,
Py_ssize_t nargs)
{
MPZ_Object *tempx = NULL, *tempy = NULL;
long res;

if (PyTuple_GET_SIZE(args) != 2) {
if (nargs != 2) {
TYPE_ERROR("kronecker() requires 'mpz','mpz' arguments");
return NULL;
}

if (!(tempx = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 0), NULL)) ||
!(tempy = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 1), NULL))) {
if (!(tempx = GMPy_MPZ_From_Integer(args[0], NULL)) ||
!(tempy = GMPy_MPZ_From_Integer(args[1], NULL))) {

Py_XDECREF((PyObject*)tempx);
Py_XDECREF((PyObject*)tempy);
Expand Down
10 changes: 5 additions & 5 deletions src/gmpy2_mpz_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static PyObject * GMPy_MPZ_Method_IsSquare(PyObject *self, PyObject *other);
static PyObject * GMPy_MPZ_Method_IsDivisible(PyObject *self, PyObject *other);
static PyObject * GMPy_MPZ_Method_IsCongruent(PyObject *self, PyObject *const *args, Py_ssize_t nargs);
static PyObject * GMPy_MPZ_Method_IsPower(PyObject *self, PyObject *other);
static PyObject * GMPy_MPZ_Method_IsPrime(PyObject *self, PyObject *args);
static PyObject * GMPy_MPZ_Method_IsPrime(PyObject *self, PyObject *const *args, Py_ssize_t nargs);
static PyObject * GMPy_MPZ_Method_IsProbabPrime(PyObject *self, PyObject *const *args, Py_ssize_t nargs);
static PyObject * GMPy_MPZ_Method_IsEven(PyObject *self, PyObject *other);
static PyObject * GMPy_MPZ_Method_IsOdd(PyObject *self, PyObject *other);
Expand Down Expand Up @@ -85,15 +85,15 @@ static PyObject * GMPy_MPZ_Function_IsSquare(PyObject *self, PyObject *other);
static PyObject * GMPy_MPZ_Function_IsDivisible(PyObject *self, PyObject * const *args, Py_ssize_t nargs);
static PyObject * GMPy_MPZ_Function_IsCongruent(PyObject *self, PyObject * const *args, Py_ssize_t nargs);
static PyObject * GMPy_MPZ_Function_IsPower(PyObject *self, PyObject *other);
static PyObject * GMPy_MPZ_Function_IsPrime(PyObject *self, PyObject *args);
static PyObject * GMPy_MPZ_Function_IsPrime(PyObject *self, PyObject * const *args, Py_ssize_t nargs);
static PyObject * GMPy_MPZ_Function_IsProbabPrime(PyObject *self, PyObject *const *args, Py_ssize_t nargs);
static PyObject * GMPy_MPZ_Function_NextPrime(PyObject *self, PyObject *other);
#if (__GNU_MP_VERSION > 6) || (__GNU_MP_VERSION == 6 && __GNU_MP_VERSION_MINOR >= 3)
static PyObject * GMPy_MPZ_Function_PrevPrime(PyObject *self, PyObject *other);
#endif
static PyObject * GMPy_MPZ_Function_Jacobi(PyObject *self, PyObject *args);
static PyObject * GMPy_MPZ_Function_Legendre(PyObject *self, PyObject *args);
static PyObject * GMPy_MPZ_Function_Kronecker(PyObject *self, PyObject *args);
static PyObject * GMPy_MPZ_Function_Jacobi(PyObject *self, PyObject * const *args, Py_ssize_t nargs);
static PyObject * GMPy_MPZ_Function_Legendre(PyObject *self, PyObject * const *args, Py_ssize_t nargs);
static PyObject * GMPy_MPZ_Function_Kronecker(PyObject *self, PyObject * const *args, Py_ssize_t nargs);
static PyObject * GMPy_MPZ_Function_IsEven(PyObject *self, PyObject *other);
static PyObject * GMPy_MPZ_Function_IsOdd(PyObject *self, PyObject *other);

Expand Down

0 comments on commit a13038d

Please sign in to comment.