|
14 | 14 |
|
15 | 15 | #include <numpy/arrayobject.h>
|
16 | 16 | #include <numpy/npy_3kcompat.h>
|
| 17 | +#include <numpy/npy_math.h> |
17 | 18 | #include <numpy/ufuncobject.h>
|
18 | 19 |
|
19 | 20 | #include "dual.h"
|
20 |
| -#include "sf_error.h" |
| 21 | +#include "error.h" |
21 | 22 | #include "third_party/kokkos/mdspan.hpp"
|
22 | 23 |
|
| 24 | +/* PyUFunc_getfperr gets bits for current floating point error (fpe) status codes so we |
| 25 | + * can check for floating point errors and make proper calls to set_error in ufunc loops. |
| 26 | + * Define a wrapper so it can be given C linkage within this C++ header. */ |
| 27 | +extern "C" int wrap_PyUFunc_getfperr() { return PyUFunc_getfperr(); } |
| 28 | + |
23 | 29 | namespace xsf {
|
24 | 30 | namespace numpy {
|
| 31 | + |
| 32 | + void set_error_check_fpe(const char *func_name) { |
| 33 | + int status = wrap_PyUFunc_getfperr(); |
| 34 | + if (status & NPY_FPE_DIVIDEBYZERO) { |
| 35 | + xsf::set_error(func_name, SF_ERROR_SINGULAR, "floating point division by zero"); |
| 36 | + } |
| 37 | + if (status & NPY_FPE_OVERFLOW) { |
| 38 | + xsf::set_error(func_name, SF_ERROR_UNDERFLOW, "floating point underflow"); |
| 39 | + } |
| 40 | + if (status & NPY_FPE_UNDERFLOW) { |
| 41 | + xsf::set_error(func_name, SF_ERROR_OVERFLOW, "floating point overflow"); |
| 42 | + } |
| 43 | + if (status & NPY_FPE_INVALID) { |
| 44 | + xsf::set_error(func_name, SF_ERROR_DOMAIN, "floating point invalid value"); |
| 45 | + } |
| 46 | + } |
| 47 | + |
25 | 48 | namespace detail {
|
26 | 49 |
|
27 | 50 | // This is std::accumulate, but that is not constexpr until C++20
|
@@ -778,7 +801,7 @@ namespace numpy {
|
778 | 801 | }
|
779 | 802 |
|
780 | 803 | const char *name = static_cast<ufunc_data<Func> *>(data)->name;
|
781 |
| - sf_error_check_fpe(name); |
| 804 | + set_error_check_fpe(name); |
782 | 805 | }
|
783 | 806 | };
|
784 | 807 |
|
@@ -811,7 +834,7 @@ namespace numpy {
|
811 | 834 | }
|
812 | 835 |
|
813 | 836 | const char *name = static_cast<ufunc_data<Func> *>(data)->name;
|
814 |
| - sf_error_check_fpe(name); |
| 837 | + set_error_check_fpe(name); |
815 | 838 | }
|
816 | 839 | };
|
817 | 840 |
|
|
0 commit comments