8000 ENH: Fast paths for richcompare by ganesh-k13 · Pull Request #17970 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Fast paths for richcompare #17970

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

Closed
wants to merge 13 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ENH: Enhanced scalar fastpaths
  • Loading branch information
ganesh-k13 committed Feb 10, 2021
commit 708dd751976c768d3677d6c91fa4e154ee61457d
42 changes: 31 additions & 11 deletions numpy/core/src/umath/scalarmath.c.src
6901
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,10 @@ descr_from_basic_scalar(PyObject *obj, PyArray_Descr **descr)
*descr = PyArray_DescrFromType(NPY_DOUBLE);
return 1;
}
else if (PyBool_Check(obj)) {
*descr = PyArray_DescrFromType(NPY_BOOL);
return 1;
}
else if (PyLong_CheckExact(obj)) {
if (PyLong_AsLong(obj) == -1 && PyErr_Occurred()) {
PyErr_Clear();
Expand All @@ -610,6 +614,8 @@ descr_from_basic_scalar(PyObject *obj, PyArray_Descr **descr)
return -1;
}

#define PyDescr_PythonCRepresentable(d) (d->type_num == NPY_LONGDOUBLE || \
d->type_num > NPY_CDOUBLE)

/**
* This function attempts to compare NumPy or Python scalars
Expand All @@ -618,37 +624,51 @@ descr_from_basic_scalar(PyObject *obj, PyArray_Descr **descr)
*
* @param self The first object which will be converted to scalar item
* @param other The second object to compare to
* @param other The value of comparison operator
* @param cmp_op The value of comparison operator
* @returns Py_False or Py_True if richcompare is successfull
* , otherwise NULL.
*/
static PyObject*
do_richcompare_on_scalars(PyObject *self, PyObject *other, int cmp_op) {
PyObject *item_self, *ret=NULL;
PyObject *cmp_item_self, *cmp_item_other, *ret=NULL;
PyArray_Descr *self_descr=NULL, *other_descr=NULL;
void *data_self;
void *data_self, *data_other;
int pyscalar_other, pyscalar_self;
int is_complex_operands, is_equality_operator;
int is_complex_operands, is_equality_operator, is_python_scalar;

pyscalar_self = descr_from_basic_scalar(self, &self_descr);
pyscalar_other = descr_from_basic_scalar(other, &other_descr);

if (pyscalar_self >= 0 && pyscalar_other >= 0) {
/*
* If either of the operands are complex AND the operator is not equality,
* python's built-in richcompare cannot be used as it is not supported
* If either of the operands are complex and operator is not equality,
* or the operands are not representable as a native type,
* python's built-in richcompare cannot be used as it is not supported.
*/
is_complex_operands = (PyTypeNum_ISCOMPLEX(self_descr->type_num) ||
PyTypeNum_ISCOMPLEX(other_descr->type_num));
is_equality_operator = (cmp_op != Py_EQ && cmp_op != Py_NE);
is_python_scalar = PyDescr_PythonCRepresentable(self_descr) ||
PyDescr_PythonCRepresentable(other_descr);

if (!(is_complex_operands && is_equality_operator)) {
if (!(is_complex_operands && is_equality_operator) && !is_python_scalar) {
data_self = scalar_value(self, NULL);
item_self = self_descr->f->getitem(data_self, NULL);
if (item_self != NULL) {
ret = PyObject_RichCompare(item_self, other, cmp_op);
data_other = scalar_value(other, NULL);
cmp_item_self = pyscalar_self == 0 ? self_descr->f->getitem(data_self, NULL):
self;
cmp_item_other = pyscalar_other == 0 ? other_descr->f->getitem(data_other, NULL):
other;

if (cmp_item_self != NULL && cmp_item_other != NULL) {
ret = PyObject_RichCompare(cmp_item_self, cmp_item_other, cmp_op);
}

if (pyscalar_self == 0) {
Py_XDECREF(cmp_item_self);
}
if (pyscalar_other == 0) {
Py_XDECREF(cmp_item_other);
}
Py_XDECREF(item_self);
}
}
Py_XDECREF(self_descr);
Expand Down
0