@@ -86,19 +86,17 @@ floor_mod(T a, T b)
8686}
8787
8888/*
89- * One loop per real dtype. T is the storage/compute type; discont is read
90- * as npy_longdouble only for the longdouble loop, npy_double otherwise.
91- * npy_half has no native arithmetic, so it gets its own loop below.
89+ * One loop per (T, D) pair. T is the storage/compute type; D is discont's
90+ * type, either T itself (NEP 50 weak-scalar match) or a wider float type
91+ * for explicitly-typed/cross-kind discont. npy_half has no native
92+ * arithmetic, so it gets its own loop below.
9293 */
93- template <typename T>
94+ template <typename T, typename D >
9495static int
9596unwrap_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
9697 npy_intp const dimensions[], npy_intp const strides[],
9798 NpyAuxData *NPY_UNUSED(auxdata))
9899{
99- using D = std::conditional_t<std::is_same_v<T, npy_longdouble>,
100- npy_longdouble, npy_double>;
101-
102100 npy_intp n_outer = dimensions[0];
103101 npy_intp n = dimensions[1];
104102 if (n == 0) {
@@ -162,6 +160,7 @@ unwrap_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
162160static inline float h2f(npy_half h) { return npy_half_to_float(h); }
163161static inline npy_half f2h(float f) { return npy_float_to_half(f); }
164162
163+ template <typename D>
165164static int
166165unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
167166 npy_intp const dimensions[], npy_intp const strides[],
@@ -181,7 +180,15 @@ unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
181180
182181 for (npy_intp k = 0; k < n_outer; k++,
183182 p_o += s_p, disc_o += s_disc, per_o += s_per, out_o += s_out) {
184- npy_double discont = *(const npy_double *)disc_o;
183+ /* npy_half discont is read through h2f, matching the NEP 50 weak-
184+ * scalar case where discont is compared at half precision */
185+ double discont;
186+ if constexpr (std::is_same_v<D, npy_half>) {
187+ discont = h2f(*(const npy_half *)disc_o);
188+ }
189+ else {
190+ discont = *(const D *)disc_o;
191+ }
185192 npy_half period = *(const npy_half *)per_o;
186193 npy_half interval_high = f2h(h2f(period) / 2.0f);
187194 npy_half interval_low = f2h(-h2f(interval_high));
@@ -218,7 +225,7 @@ unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
218225
219226static int
220227add_unwrap_loop(PyObject *ufunc, int typenum,
221- PyArrayMethod_StridedLoop *loop, int discont_typenum = NPY_DOUBLE )
228+ PyArrayMethod_StridedLoop *loop, int discont_typenum)
222229{
223230 PyArray_DTypeMeta *dt = PyArray_DTypeFromTypeNum(typenum);
224231 if (dt == NULL) {
@@ -272,28 +279,37 @@ init_unwrap_ufunc(PyObject *umath)
272279 struct Loop {
273280 int typenum;
274281 PyArrayMethod_StridedLoop *loop;
282+ int discont_typenum;
275283 };
276284
285+ /*
286+ * float dtypes get two loops: discont at native precision (the NEP 50
287+ * weak-scalar case, where the python wrapper matches discont to p's
288+ * dtype) and at wider precision (explicitly-typed/cross-kind discont).
289+ * Integer dtypes only get the wider loop -- NEP 50 native-int discont
290+ * is not implemented, so the wrapper always widens for those.
291+ */
277292 Loop loops[] = {
278- {NPY_HALF, unwrap_half_loop},
279- {NPY_FLOAT, unwrap_loop<npy_float>},
280- {NPY_DOUBLE, unwrap_loop<npy_double>},
281- {NPY_LONGDOUBLE, unwrap_loop<npy_longdouble>},
282- {NPY_BYTE, unwrap_loop<npy_byte>},
283- {NPY_SHORT, unwrap_loop<npy_short>},
284- {NPY_INT, unwrap_loop<npy_int>},
285- {NPY_LONG, unwrap_loop<npy_long>},
286- {NPY_LONGLONG, unwrap_loop<npy_longlong>},
293+ {NPY_HALF, unwrap_half_loop<npy_half>, NPY_HALF},
294+ {NPY_HALF, unwrap_half_loop<npy_double>, NPY_DOUBLE},
295+ {NPY_FLOAT, unwrap_loop<npy_float, npy_float>, NPY_FLOAT},
296+ {NPY_FLOAT, unwrap_loop<npy_float, npy_double>, NPY_DOUBLE},
297+ {NPY_DOUBLE, unwrap_loop<npy_double, npy_double>, NPY_DOUBLE},
298+ {NPY_LONGDOUBLE, unwrap_loop<npy_longdouble, npy_longdouble>,
299+ NPY_LONGDOUBLE},
300+ {NPY_BYTE, unwrap_loop<npy_byte, npy_double>, NPY_DOUBLE},
301+ {NPY_SHORT, unwrap_loop<npy_short, npy_double>, NPY_DOUBLE},
302+ {NPY_INT, unwrap_loop<npy_int, npy_double>, NPY_DOUBLE},
303+ {NPY_LONG, unwrap_loop<npy_long, npy_double>, NPY_DOUBLE},
304+ {NPY_LONGLONG, unwrap_loop<npy_longlong, npy_double>, NPY_DOUBLE},
287305 /* object dtype falls back to the python implementation instead */
288306 // {NPY_OBJECT, object_unwrap_loop},
289307 };
290308
291309 int res = 0;
292310 for (const auto& entry : loops) {
293- int discont_typenum = entry.typenum == NPY_LONGDOUBLE
294- ? NPY_LONGDOUBLE : NPY_DOUBLE;
295311 if (add_unwrap_loop(ufunc, entry.typenum, entry.loop,
296- discont_typenum) < 0) {
312+ entry. discont_typenum) < 0) {
297313 res = -1;
298314 break;
299315 }
0 commit comments