|
32 | 32 | * |
33 | 33 | * The operands are, in order, the values `p` (core `(n)`), the scalars |
34 | 34 | * `discont` and `period`, and the output (core `(n)`). The Python wrapper in |
35 | | - * numpy/lib/_function_base_impl.py fills the `discont`/`period` defaults, |
36 | | - * resolves the common dtype and casts the scalars to it. |
| 35 | + * numpy/lib/_function_base_impl.py only fills the `discont`/`period` |
| 36 | + * defaults; dtype resolution happens in unwrap_promoter below. |
37 | 37 | * |
38 | 38 | * gufunc strided-loop layout for signature (n),(),()->(n): |
39 | 39 | * dimensions[0] outer (broadcast) loop count |
@@ -223,6 +223,76 @@ unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[], |
223 | 223 | } |
224 | 224 |
|
225 | 225 |
|
| 226 | +/* |
| 227 | + * T = common_dtype(p, period), i.e. np.result_type(p, period). discont gets |
| 228 | + * T's own precision when that's what it naturally promotes to (the NEP 50 |
| 229 | + * weak-scalar case, for the float dtypes with a native loop), otherwise it |
| 230 | + * widens to double (or longdouble when T is longdouble). |
| 231 | + */ |
| 232 | +static int |
| 233 | +unwrap_promoter(PyObject *NPY_UNUSED(ufunc), |
| 234 | + PyArray_DTypeMeta *const op_dtypes[], |
| 235 | + PyArray_DTypeMeta *const NPY_UNUSED(signature[]), |
| 236 | + PyArray_DTypeMeta *new_op_dtypes[]) |
| 237 | +{ |
| 238 | + PyArray_DTypeMeta *T = PyArray_CommonDType(op_dtypes[0], op_dtypes[2]); |
| 239 | + if (T == NULL) { |
| 240 | + /* no common type for p and period; fail via the loop lookup below */ |
| 241 | + PyErr_Clear(); |
| 242 | + T = op_dtypes[0]; |
| 243 | + Py_INCREF(T); |
| 244 | + } |
| 245 | + |
| 246 | + PyArray_DTypeMeta *natural_D = PyArray_CommonDType(T, op_dtypes[1]); |
| 247 | + if (natural_D == NULL) { |
| 248 | + PyErr_Clear(); |
| 249 | + natural_D = T; |
| 250 | + Py_INCREF(natural_D); |
| 251 | + } |
| 252 | + |
| 253 | + PyArray_DTypeMeta *D; |
| 254 | + if (T == &PyArray_LongDoubleDType) { |
| 255 | + D = &PyArray_LongDoubleDType; |
| 256 | + } |
| 257 | + else if (natural_D == T && |
| 258 | + (T == &PyArray_HalfDType || T == &PyArray_FloatDType || |
| 259 | + T == &PyArray_DoubleDType)) { |
| 260 | + D = T; |
| 261 | + } |
| 262 | + else { |
| 263 | + D = &PyArray_DoubleDType; |
| 264 | + } |
| 265 | + Py_DECREF(natural_D); |
| 266 | + |
| 267 | + Py_INCREF(T); |
| 268 | + new_op_dtypes[0] = T; |
| 269 | + Py_INCREF(D); |
| 270 | + new_op_dtypes[1] = D; |
| 271 | + Py_INCREF(T); |
| 272 | + new_op_dtypes[2] = T; |
| 273 | + new_op_dtypes[3] = T; |
| 274 | + return 0; |
| 275 | +} |
| 276 | + |
| 277 | +static int |
| 278 | +add_unwrap_promoter(PyObject *ufunc) |
| 279 | +{ |
| 280 | + PyObject *DType_tuple = PyTuple_Pack(4, Py_None, Py_None, Py_None, Py_None); |
| 281 | + if (DType_tuple == NULL) { |
| 282 | + return -1; |
| 283 | + } |
| 284 | + PyObject *capsule = PyCapsule_New((void *)unwrap_promoter, |
| 285 | + "numpy._ufunc_promoter", NULL); |
| 286 | + if (capsule == NULL) { |
| 287 | + Py_DECREF(DType_tuple); |
| 288 | + return -1; |
| 289 | + } |
| 290 | + int res = PyUFunc_AddPromoter(ufunc, DType_tuple, capsule); |
| 291 | + Py_DECREF(capsule); |
| 292 | + Py_DECREF(DType_tuple); |
| 293 | + return res; |
| 294 | +} |
| 295 | + |
226 | 296 | static int |
227 | 297 | add_unwrap_loop(PyObject *ufunc, int typenum, |
228 | 298 | PyArrayMethod_StridedLoop *loop, int discont_typenum) |
@@ -284,10 +354,9 @@ init_unwrap_ufunc(PyObject *umath) |
284 | 354 |
|
285 | 355 | /* |
286 | 356 | * 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. |
| 357 | + * weak-scalar case) and at wider precision (explicitly-typed/cross-kind |
| 358 | + * discont). Integer dtypes only get the wider loop -- unwrap_promoter |
| 359 | + * always widens discont for those. |
291 | 360 | */ |
292 | 361 | Loop loops[] = { |
293 | 362 | {NPY_HALF, unwrap_half_loop<npy_half>, NPY_HALF}, |
@@ -315,6 +384,10 @@ init_unwrap_ufunc(PyObject *umath) |
315 | 384 | } |
316 | 385 | } |
317 | 386 |
|
| 387 | + if (res == 0 && add_unwrap_promoter(ufunc) < 0) { |
| 388 | + res = -1; |
| 389 | + } |
| 390 | + |
318 | 391 | Py_DECREF(ufunc); |
319 | 392 | return res; |
320 | 393 | } |
0 commit comments