New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: Don't signal FP exceptions in np.absolute #8713
Conversation
|
This would have significant performance impact on non x86 as the function is not inlined. |
|
the change makes sense, but we compile with clang and windows compilers, how come we never saw this outside of mips? |
Ah, I didn't check that - I was just testing with plan
I can reproduce the bug on Clang x86. The test suite was ignoring FP exceptions before which may be why noone saw it. |
Fixes numpy#8686 This PR centers around this piece of code in `numpy/core/src/umath/loops.c.src`: ```c UNARY_LOOP { const @type@ in1 = *(@type@ *)ip1; const @type@ tmp = in1 > 0 ? in1 : -in1; /* add 0 to clear -0.0 */ *((@type@ *)op1) = tmp + 0; } ``` If in1 is `NaN`, the C99 standard requires that the comparison `in1 > 0` signals `FE_INVALID`, but the usual semantics for the absolute function are that no FP exceptions should be generated (eg compare to C `fabs` and Python `abs`). This was probably never noticed due to a bug in GCC x86 where all floating point comparisons do not signal exceptions, however Clang on x86 and GCC on other architectures (including ARM and MIPS) do signal an FP exception here. Fix by clearing the floating point exceptions after the loop has finished. The alternative of rewriting the loop to use `npy_fabs` instead would also work but has performance issues because that function is not inlined. The `test_abs_neg_blocked` is adjusted not to ignore `FE_INVALID` errors because now both absolute and negate should never produce an FP exceptions.
0f9b305
to
b5cf454
Compare
|
Sorry for the delay - I've updated the PR to use |
|
thanks |
|
@charris while I haven't seen it myself it appears to cause warnings with clang, so it might be worthwhile to still put this into 1.12.1, should be regression safe. |
|
math functions are now inlined (gh-8754) so one could now use npy_fabs for better performance on some arches |
Fixes #8686
This PR centers around this piece of code in
numpy/core/src/umath/loops.c.src:If in1 is
NaN, the C99 standard requires that the comparisonin1 > 0signalsFE_INVALID, but the usual semantics for the absolute function are that no FP exceptions should be generated (eg compare to Cfabsand Pythonabs). This was probably never noticed due to a bug in GCC x86 where all floating point comparisons do not signal exceptions, however Clang on x86 and GCC on other architectures (including ARM and MIPS) do signal an FP exception here.Fix by rewriting the loop to use
npy_fabsinstead. Also fix a similar occurrence insimd.inc.src. Thetest_abs_neg_blockedis adjusted not to ignoreFE_INVALIDerrors because now both absolute and negate should never produce any FP exceptions.