[Numpy-svn] [numpy/numpy] b5cf45: BUG: Don't signal FP exceptions in np.absolute

GitHub noreply at github.com
Tue Mar 7 07:08:01 EST 2017


  Branch: refs/heads/master
  Home:   https://github.com/numpy/numpy
  Commit: b5cf454008394b05df439e3160618da83e85775a
      https://github.com/numpy/numpy/commit/b5cf454008394b05df439e3160618da83e85775a
  Author: James Cowgill <james410 at cowgill.org.uk>
  Date:   2017-03-07 (Tue, 07 Mar 2017)

  Changed paths:
    M numpy/core/src/umath/loops.c.src
    M numpy/core/tests/test_umath.py

  Log Message:
  -----------
  BUG: Don't signal FP exceptions in np.absolute

Fixes #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.


  Commit: 2fe5a4757e840362b7158e8548e26ffc9ef8b562
      https://github.com/numpy/numpy/commit/2fe5a4757e840362b7158e8548e26ffc9ef8b562
  Author: Julian Taylor <juliantaylor108 at gmail.com>
  Date:   2017-03-07 (Tue, 07 Mar 2017)

  Changed paths:
    M numpy/core/src/umath/loops.c.src
    M numpy/core/tests/test_umath.py

  Log Message:
  -----------
  Merge pull request #8713 from jcowgill/fabs-fe-invalid

BUG: Don't signal FP exceptions in np.absolute


Compare: https://github.com/numpy/numpy/compare/66f1b8a5411e...2fe5a4757e84


More information about the Numpy-svn mailing list