99#define PY_SSIZE_T_CLEAN
1010#include < Python.h>
1111
12- #include < limits>
1312#include < type_traits>
1413
1514#include " numpy/halffloat.h"
@@ -63,19 +62,18 @@ floor_mod(npy_longdouble a, npy_longdouble b)
6362#endif
6463
6564/* no integer npy_remainder to call; mirrors @TYPE@_remainder in
66- * loops_modulo.dispatch.c.src, including the T_MIN / -1 guard */
65+ * loops_modulo.dispatch.c.src, including the T_MIN / -1 guard (which sets
66+ * no floating point status there either -- only the quotient overflows,
67+ * and this only ever computes the remainder) */
6768template <typename T>
68- static inline std::enable_if_t <std::is_integral_v<T>, T>
69+ static inline std::enable_if_t <std::is_integral_v<T> && std::is_signed_v<T> , T>
6970floor_mod (T a, T b)
7071{
7172 if (b == 0 ) {
7273 npy_set_floatstatus_divbyzero ();
7374 return 0 ;
7475 }
7576 if (b == -1 ) {
76- if (a == std::numeric_limits<T>::min ()) {
77- npy_set_floatstatus_divbyzero ();
78- }
7977 return 0 ;
8078 }
8179 T rem = a % b;
@@ -86,17 +84,18 @@ floor_mod(T a, T b)
8684}
8785
8886/*
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.
87+ * One loop per real dtype. T is the storage/compute type; discont is read
88+ * as T itself for the float dtypes, npy_double for the integer ones.
89+ * npy_half has no native arithmetic, so it gets its own loop below.
9390 */
94- template <typename T, typename D >
91+ template <typename T>
9592static int
9693unwrap_loop (PyArrayMethod_Context *NPY_UNUSED (context), char *const data[],
9794 npy_intp const dimensions[], npy_intp const strides[],
9895 NpyAuxData *NPY_UNUSED(auxdata))
9996{
97+ using D = std::conditional_t <std::is_floating_point_v<T>, T, npy_double>;
98+
10099 npy_intp n_outer = dimensions[0 ];
101100 npy_intp n = dimensions[1 ];
102101 if (n == 0 ) {
@@ -160,7 +159,6 @@ unwrap_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
160159static inline float h2f (npy_half h) { return npy_half_to_float (h); }
161160static inline npy_half f2h (float f) { return npy_float_to_half (f); }
162161
163- template <typename D>
164162static int
165163unwrap_half_loop (PyArrayMethod_Context *NPY_UNUSED (context), char *const data[],
166164 npy_intp const dimensions[], npy_intp const strides[],
@@ -180,15 +178,7 @@ unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
180178
181179 for (npy_intp k = 0 ; k < n_outer; k++,
182180 p_o += s_p, disc_o += s_disc, per_o += s_per, out_o += s_out) {
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- }
181+ double discont = h2f (*(const npy_half *)disc_o);
192182 npy_half period = *(const npy_half *)per_o;
193183 npy_half interval_high = f2h (h2f (period) / 2 .0f );
194184 npy_half interval_low = f2h (-h2f (interval_high));
@@ -224,13 +214,15 @@ unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
224214
225215
226216static int
227- add_unwrap_loop (PyObject *ufunc, int typenum,
228- PyArrayMethod_StridedLoop *loop, int discont_typenum)
217+ add_unwrap_loop (PyObject *ufunc, int typenum, PyArrayMethod_StridedLoop *loop)
229218{
230219 PyArray_DTypeMeta *dt = PyArray_DTypeFromTypeNum (typenum);
231220 if (dt == NULL ) {
232221 return -1 ;
233222 }
223+ /* discont is read at typenum's own precision for float dtypes, double
224+ * for the integer ones -- matches unwrap_loop's own D derivation */
225+ int discont_typenum = PyTypeNum_ISFLOAT (typenum) ? typenum : NPY_DOUBLE ;
234226 PyArray_DTypeMeta *disc_dt = PyArray_DTypeFromTypeNum (discont_typenum);
235227 if (disc_dt == NULL ) {
236228 Py_DECREF (dt);
@@ -274,36 +266,24 @@ init_unwrap_ufunc(PyObject *umath)
274266 struct Loop {
275267 int typenum;
276268 PyArrayMethod_StridedLoop *loop;
277- int discont_typenum;
278269 };
279270
280- /*
281- * float dtypes get two loops: discont at native precision (the NEP 50
282- * weak-scalar case, where the python wrapper matches discont to p's
283- * dtype) and at wider precision (explicitly-typed/cross-kind discont).
284- * Integer dtypes only get the wider loop -- NEP 50 native-int discont
285- * is not implemented, so the wrapper always widens for those.
286- */
287271 Loop loops[] = {
288- {NPY_HALF , unwrap_half_loop<npy_half>, NPY_HALF },
289- {NPY_HALF , unwrap_half_loop<npy_double>, NPY_DOUBLE },
290- {NPY_FLOAT , unwrap_loop<npy_float, npy_float>, NPY_FLOAT },
291- {NPY_FLOAT , unwrap_loop<npy_float, npy_double>, NPY_DOUBLE },
292- {NPY_DOUBLE , unwrap_loop<npy_double, npy_double>, NPY_DOUBLE },
293- {NPY_LONGDOUBLE , unwrap_loop<npy_longdouble, npy_longdouble>,
294- NPY_LONGDOUBLE },
295- {NPY_BYTE , unwrap_loop<npy_byte, npy_double>, NPY_DOUBLE },
296- {NPY_SHORT , unwrap_loop<npy_short, npy_double>, NPY_DOUBLE },
297- {NPY_INT , unwrap_loop<npy_int, npy_double>, NPY_DOUBLE },
298- {NPY_LONG , unwrap_loop<npy_long, npy_double>, NPY_DOUBLE },
299- {NPY_LONGLONG , unwrap_loop<npy_longlong, npy_double>, NPY_DOUBLE },
272+ {NPY_HALF , unwrap_half_loop},
273+ {NPY_FLOAT , unwrap_loop<npy_float>},
274+ {NPY_DOUBLE , unwrap_loop<npy_double>},
275+ {NPY_LONGDOUBLE , unwrap_loop<npy_longdouble>},
276+ {NPY_BYTE , unwrap_loop<npy_byte>},
277+ {NPY_SHORT , unwrap_loop<npy_short>},
278+ {NPY_INT , unwrap_loop<npy_int>},
279+ {NPY_LONG , unwrap_loop<npy_long>},
280+ {NPY_LONGLONG , unwrap_loop<npy_longlong>},
300281 /* object dtype falls back to the python implementation instead */
301282 // {NPY_OBJECT, object_unwrap_loop},
302283 };
303284
304285 for (const auto & entry : loops) {
305- if (add_unwrap_loop (ufunc, entry.typenum , entry.loop ,
306- entry.discont_typenum ) < 0 ) {
286+ if (add_unwrap_loop (ufunc, entry.typenum , entry.loop ) < 0 ) {
307287 Py_DECREF (ufunc);
308288 return -1 ;
309289 }
0 commit comments