Skip to content

Commit 16a1772

Browse files
committed
MAINT: special: remove dependence of xsf::numpy on sf_error (#21802)
* MAINT: Remove dependence of xsf::numpy on scipy's sf_error * MAINT: Give set_error_check_fpe a more accurate comment * MAINT: Move C linkage function out of C++ namespace
1 parent b90819c commit 16a1772

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

scipy/special/xsf/numpy.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,37 @@
1414

1515
#include <numpy/arrayobject.h>
1616
#include <numpy/npy_3kcompat.h>
17+
#include <numpy/npy_math.h>
1718
#include <numpy/ufuncobject.h>
1819

1920
#include "dual.h"
20-
#include "sf_error.h"
21+
#include "error.h"
2122
#include "third_party/kokkos/mdspan.hpp"
2223

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+
2329
namespace xsf {
2430
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+
2548
namespace detail {
2649

2750
// This is std::accumulate, but that is not constexpr until C++20
@@ -778,7 +801,7 @@ namespace numpy {
778801
}
779802

780803
const char *name = static_cast<ufunc_data<Func> *>(data)->name;
781-
sf_error_check_fpe(name);
804+
set_error_check_fpe(name);
782805
}
783806
};
784807

@@ -811,7 +834,7 @@ namespace numpy {
811834
}
812835

813836
const char *name = static_cast<ufunc_data<Func> *>(data)->name;
814-
sf_error_check_fpe(name);
837+
set_error_check_fpe(name);
815838
}
816839
};
817840

0 commit comments

Comments
 (0)