Use __builtin_isnan/isinf/isfinite in complex
authorHal Finkel <hfinkel@anl.gov>
Sat, 1 Oct 2016 20:38:31 +0000 (20:38 +0000)
committerHal Finkel <hfinkel@anl.gov>
Sat, 1 Oct 2016 20:38:31 +0000 (20:38 +0000)
commitae22f0b2423192c919ad3c17627a5d40cfcdc5dc
treeef5d46a25c42fc28a7633ea2515fea54929d59ea
parent630dd6ff0280a2111bb651ff743fc28676373761
Use __builtin_isnan/isinf/isfinite in complex

The libc-provided isnan/isinf/isfinite macro implementations are specifically
designed to function correctly, even in the presence of -ffast-math (or, more
specifically, -ffinite-math-only). As such, on most implementation, these
either always turn into external function calls (e.g. glibc) or are
specifically function calls when FINITE_MATH_ONLY is defined (e.g. Darwin).

Our implementation of complex arithmetic makes heavy use of isnan/isinf/isfinite
to deal with corner cases involving non-finite quantities. This was problematic
in two respects:

  1. On systems where these are always function calls (e.g. Linux/glibc), there was a
     performance penalty
  2. When compiling with -ffast-math, there was a significant performance
     penalty (in fact, on Darwin and systems with similar implementations, the code
     may in fact be slower than not using -ffast-math, because the inline
     definitions provided by libc become unavailable to prevent the checks from
     being optimized out).

Eliding these inf/nan checks in -ffast-math mode is consistent with what
happens with libstdc++, and in my experience, what users expect. This is
critical to getting high-performance code when using complex<T>. This change
replaces uses of those functions on basic floating-point types with calls to
__builtin_isnan/isinf/isfinite, which Clang will always expand inline. When
using -ffast-math (or -ffinite-math-only), the optimizer will remove the checks
as expected.

Differential Revision: https://reviews.llvm.org/D18639

llvm-svn: 283051
libcxx/include/cmath
libcxx/include/complex