Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
63 changes: 56 additions & 7 deletions src/csrc/umath/binary_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ quad_ldexp_resolve_descriptors(PyObject *self, PyArray_DTypeMeta *const dtypes[]
Py_INCREF(given_descrs[0]);
loop_descrs[0] = given_descrs[0];

// Input 1: Use NPY_INTP (int64 on 64-bit, int32 on 32-bit) to match platform integer size
// This ensures we can handle the full range of PyArray_PyLongDType without data loss
// Input 1: Use NPY_INTP to match the registered PyArray_IntpDType
loop_descrs[1] = PyArray_DescrFromType(NPY_INTP);

// Output: QuadPrecDType with same backend as input
Expand Down Expand Up @@ -408,7 +407,7 @@ create_quad_ldexp_ufunc(PyObject *numpy, const char *ufunc_name)
return -1;
}

PyArray_DTypeMeta *dtypes[3] = {&QuadPrecDType, &PyArray_PyLongDType, &QuadPrecDType};
PyArray_DTypeMeta *dtypes[3] = {&QuadPrecDType, &PyArray_IntpDType, &QuadPrecDType};

PyType_Slot slots[] = {
{NPY_METH_resolve_descriptors, (void *)&quad_ldexp_resolve_descriptors},
Expand All @@ -429,28 +428,33 @@ create_quad_ldexp_ufunc(PyObject *numpy, const char *ufunc_name)
};

if (PyUFunc_AddLoopFromSpec(ufunc, &Spec) < 0) {
Py_DECREF(ufunc);
return -1;
}

PyObject *promoter_capsule =
PyCapsule_New((void *)&quad_ufunc_promoter, "numpy._ufunc_promoter", NULL);
PyCapsule_New((void *)&quad_ldexp_promoter, "numpy._ufunc_promoter", NULL);
if (promoter_capsule == NULL) {
Py_DECREF(ufunc);
return -1;
}

PyObject *DTypes = PyTuple_Pack(3, &PyArrayDescr_Type, &PyArray_PyLongDType, &PyArrayDescr_Type);
PyObject *DTypes = PyTuple_Pack(3, &QuadPrecDType, &PyArrayDescr_Type, &PyArrayDescr_Type);
if (DTypes == 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(ufunc);
return -1;
}

if (PyUFunc_AddPromoter(ufunc, DTypes, promoter_capsule) < 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(DTypes);
Py_DECREF(ufunc);
return -1;
}
Py_DECREF(promoter_capsule);
Py_DECREF(DTypes);
Py_DECREF(ufunc);
return 0;
}

Expand Down Expand Up @@ -486,29 +490,52 @@ create_quad_binary_2out_ufunc(PyObject *numpy, const char *ufunc_name)
};

if (PyUFunc_AddLoopFromSpec(ufunc, &Spec) < 0) {
Py_DECREF(ufunc);
return -1;
}

PyObject *promoter_capsule =
PyCapsule_New((void *)&quad_ufunc_promoter, "numpy._ufunc_promoter", NULL);
if (promoter_capsule == NULL) {
Py_DECREF(ufunc);
return -1;
}

PyObject *DTypes = PyTuple_Pack(4, &PyArrayDescr_Type, &PyArrayDescr_Type,
// Register promoter for (QuadPrecDType, Any, Any, Any)
PyObject *DTypes = PyTuple_Pack(4, &QuadPrecDType, &PyArrayDescr_Type,
&PyArrayDescr_Type, &PyArrayDescr_Type);
if (DTypes == 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(ufunc);
return -1;
}

if (PyUFunc_AddPromoter(ufunc, DTypes, promoter_capsule) < 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(DTypes);
Py_DECREF(ufunc);
return -1;
}
Py_DECREF(DTypes);

// Register promoter for (Any, QuadPrecDType, Any, Any)
DTypes = PyTuple_Pack(4, &PyArrayDescr_Type, &QuadPrecDType,
&PyArrayDescr_Type, &PyArrayDescr_Type);
if (DTypes == 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(ufunc);
return -1;
}

if (PyUFunc_AddPromoter(ufunc, DTypes, promoter_capsule) < 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(DTypes);
Py_DECREF(ufunc);
return -1;
}
Py_DECREF(promoter_capsule);
Py_DECREF(DTypes);
Py_DECREF(ufunc);
return 0;
}

Expand Down Expand Up @@ -542,28 +569,50 @@ create_quad_binary_ufunc(PyObject *numpy, const char *ufunc_name)
};

if (PyUFunc_AddLoopFromSpec(ufunc, &Spec) < 0) {
Py_DECREF(ufunc);
return -1;
}

PyObject *promoter_capsule =
PyCapsule_New((void *)&quad_ufunc_promoter, "numpy._ufunc_promoter", NULL);
if (promoter_capsule == NULL) {
Py_DECREF(ufunc);
return -1;
}

PyObject *DTypes = PyTuple_Pack(3, &PyArrayDescr_Type, &PyArrayDescr_Type, &PyArrayDescr_Type);
// Register promoter for (QuadPrecDType, Any, Any)
PyObject *DTypes = PyTuple_Pack(3, &QuadPrecDType, &PyArrayDescr_Type, &PyArrayDescr_Type);
if (DTypes == 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(ufunc);
return -1;
}

if (PyUFunc_AddPromoter(ufunc, DTypes, promoter_capsule) < 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(DTypes);
Py_DECREF(ufunc);
return -1;
}
Py_DECREF(DTypes);

// Register promoter for (Any, QuadPrecDType, Any)
DTypes = PyTuple_Pack(3, &PyArrayDescr_Type, &QuadPrecDType, &PyArrayDescr_Type);
if (DTypes == 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(ufunc);
return -1;
}

if (PyUFunc_AddPromoter(ufunc, DTypes, promoter_capsule) < 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(DTypes);
Py_DECREF(ufunc);
return -1;
}
Py_DECREF(promoter_capsule);
Py_DECREF(DTypes);
Py_DECREF(ufunc);
return 0;
}

Expand Down
15 changes: 12 additions & 3 deletions src/csrc/umath/comparison_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,17 @@ quad_reduce_comp_strided_loop_unaligned(PyArrayMethod_Context *context, char *co


NPY_NO_EXPORT int
comparison_ufunc_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[],
PyArray_DTypeMeta *signature[], PyArray_DTypeMeta *new_op_dtypes[])
comparison_ufunc_promoter(PyObject *ufunc_obj, PyArray_DTypeMeta *const op_dtypes[],
Copy link
Copy Markdown
Member Author

@SwayamInSync SwayamInSync Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same simplification thing here as well + noticed a bug of dtype registration not matching with registered loop

PyArray_DTypeMeta *const signature[], PyArray_DTypeMeta *new_op_dtypes[])
{
PyArray_DTypeMeta *new_signature[NPY_MAXARGS];
memcpy(new_signature, signature, 3 * sizeof(PyArray_DTypeMeta *));
new_signature[2] = NULL;
int res = quad_ufunc_promoter(ufunc, op_dtypes, new_signature, new_op_dtypes);
int res = quad_ufunc_promoter(ufunc_obj, op_dtypes, new_signature, new_op_dtypes);
if (res < 0) {
return -1;
}
Py_INCREF(&PyArray_BoolDType);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

Py_XSETREF(new_op_dtypes[2], &PyArray_BoolDType);
return 0;
}
Expand Down Expand Up @@ -301,6 +302,7 @@ create_quad_comparison_ufunc(PyObject *numpy, const char *ufunc_name)
};

if (PyUFunc_AddLoopFromSpec(ufunc, &Spec) < 0) {
Py_DECREF(ufunc);
return -1;
}

Expand All @@ -326,25 +328,29 @@ create_quad_comparison_ufunc(PyObject *numpy, const char *ufunc_name)
};

if (PyUFunc_AddLoopFromSpec(ufunc, &Spec_reduce) < 0) {
Py_DECREF(ufunc);
return -1;
}

PyObject *promoter_capsule =
PyCapsule_New((void *)&comparison_ufunc_promoter, "numpy._ufunc_promoter", NULL);
if (promoter_capsule == NULL) {
Py_DECREF(ufunc);
return -1;
}

// Register promoter for (QuadPrecDType, Any, Bool) - needed for mixed-type comparisons
PyObject *DTypes = PyTuple_Pack(3, &QuadPrecDType, &PyArrayDescr_Type, &PyArray_BoolDType);
if (DTypes == 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(ufunc);
return -1;
}

if (PyUFunc_AddPromoter(ufunc, DTypes, promoter_capsule) < 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(DTypes);
Py_DECREF(ufunc);
return -1;
}
Py_DECREF(DTypes);
Expand All @@ -353,16 +359,19 @@ create_quad_comparison_ufunc(PyObject *numpy, const char *ufunc_name)
DTypes = PyTuple_Pack(3, &PyArrayDescr_Type, &QuadPrecDType, &PyArray_BoolDType);
if (DTypes == 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(ufunc);
return -1;
}

if (PyUFunc_AddPromoter(ufunc, DTypes, promoter_capsule) < 0) {
Py_DECREF(promoter_capsule);
Py_DECREF(DTypes);
Py_DECREF(ufunc);
return -1;
}
Py_DECREF(promoter_capsule);
Py_DECREF(DTypes);
Py_DECREF(ufunc);

return 0;
}
Expand Down
16 changes: 14 additions & 2 deletions src/csrc/umath/matmul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@ init_matmul_ops(PyObject *numpy)
return -1;
}

PyObject *DTypes = PyTuple_Pack(3, &PyArrayDescr_Type, &PyArrayDescr_Type, &PyArrayDescr_Type);
// Register promoter for (QuadPrecDType, Any, Any)
PyObject *DTypes = PyTuple_Pack(3, &QuadPrecDType, &PyArrayDescr_Type, &PyArrayDescr_Type);
if (DTypes == NULL) {
Py_DECREF(promoter_capsule);
Py_DECREF(ufunc);
Expand All @@ -441,10 +442,21 @@ init_matmul_ops(PyObject *numpy)
if (PyUFunc_AddPromoter(ufunc, DTypes, promoter_capsule) < 0) {
PyErr_Clear();
}
else {
Py_DECREF(DTypes);

// Register promoter for (Any, QuadPrecDType, Any)
DTypes = PyTuple_Pack(3, &PyArrayDescr_Type, &QuadPrecDType, &PyArrayDescr_Type);
if (DTypes == NULL) {
Py_DECREF(promoter_capsule);
Py_DECREF(ufunc);
return -1;
}

if (PyUFunc_AddPromoter(ufunc, DTypes, promoter_capsule) < 0) {
PyErr_Clear();
}
Py_DECREF(DTypes);

Py_DECREF(promoter_capsule);
Py_DECREF(ufunc);

Expand Down
8 changes: 8 additions & 0 deletions src/csrc/umath/unary_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ create_quad_unary_ufunc(PyObject *numpy, const char *ufunc_name)
};

if (PyUFunc_AddLoopFromSpec(ufunc, &Spec) < 0) {
Py_DECREF(ufunc);
return -1;
}

Py_DECREF(ufunc);
return 0;
}

Expand Down Expand Up @@ -261,9 +263,11 @@ create_quad_logical_not_ufunc(PyObject *numpy, const char *ufunc_name)
};

if (PyUFunc_AddLoopFromSpec(ufunc, &Spec) < 0) {
Py_DECREF(ufunc);
return -1;
}

Py_DECREF(ufunc);
return 0;
}

Expand Down Expand Up @@ -402,9 +406,11 @@ create_quad_unary_2out_ufunc(PyObject *numpy, const char *ufunc_name)
};

if (PyUFunc_AddLoopFromSpec(ufunc, &Spec) < 0) {
Py_DECREF(ufunc);
return -1;
}

Py_DECREF(ufunc);
return 0;
}

Expand Down Expand Up @@ -561,9 +567,11 @@ create_quad_frexp_ufunc(PyObject *numpy, const char *ufunc_name)
};

if (PyUFunc_AddLoopFromSpec(ufunc, &Spec) < 0) {
Py_DECREF(ufunc);
return -1;
}

Py_DECREF(ufunc);
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions src/csrc/umath/unary_props.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ create_quad_unary_prop_ufunc(PyObject *numpy, const char *ufunc_name)
};

if (PyUFunc_AddLoopFromSpec(ufunc, &Spec) < 0) {
Py_DECREF(ufunc);
return -1;
}

Py_DECREF(ufunc);
return 0;
}

Expand Down
30 changes: 27 additions & 3 deletions src/include/umath/promoters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#include "../dtype.h"

inline int
quad_ufunc_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[],
PyArray_DTypeMeta *signature[], PyArray_DTypeMeta *new_op_dtypes[])
quad_ufunc_promoter(PyObject *ufunc_obj, PyArray_DTypeMeta *const op_dtypes[],
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we change the promoter registration to ensure one type remains quaddtype, we can very much simplify this current code ( no need to call PyArray_PromoteDTypeSequence, which requires casting away the const-ness) so if that sounds uncomfortable I made a new PR which removes this (have to add here to ensure the proper build)

PyArray_DTypeMeta *const signature[], PyArray_DTypeMeta *new_op_dtypes[])
{
PyUFuncObject *ufunc = (PyUFuncObject *)ufunc_obj;
int nin = ufunc->nin;
int nargs = ufunc->nargs;
PyArray_DTypeMeta *common = NULL;
Expand Down Expand Up @@ -56,7 +57,7 @@ quad_ufunc_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[],
}
// If no common output dtype, use standard promotion for inputs
if (common == NULL) {
common = PyArray_PromoteDTypeSequence(nin, op_dtypes);
common = PyArray_PromoteDTypeSequence(nin, const_cast<PyArray_DTypeMeta **>(op_dtypes));
if (common == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
PyErr_Clear(); // Do not propagate normal promotion errors
Expand Down Expand Up @@ -87,4 +88,27 @@ quad_ufunc_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[],
}


inline int
quad_ldexp_promoter(PyObject *ufunc_obj, PyArray_DTypeMeta *const op_dtypes[],
PyArray_DTypeMeta *const signature[], PyArray_DTypeMeta *new_op_dtypes[])
{
Py_INCREF(&QuadPrecDType);
new_op_dtypes[0] = &QuadPrecDType;

// Promote the exponent to PyArray_IntpDType (unless signature specifies otherwise)
if (signature[1] != NULL) {
Py_INCREF(signature[1]);
new_op_dtypes[1] = signature[1];
}
else {
Py_INCREF(&PyArray_IntpDType);
new_op_dtypes[1] = &PyArray_IntpDType;
}

Py_INCREF(&QuadPrecDType);
new_op_dtypes[2] = &QuadPrecDType;

return 0;
}

#endif
Loading
Loading