[PATCH] D18639: Use __builtin_isnan/isinf/isfinite in complex

Hal Finkel via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 31 00:16:44 PDT 2016


hfinkel created this revision.
hfinkel added reviewers: mclow.lists, EricWF, chandlerc.
hfinkel added a subscriber: cfe-commits.
Herald added a subscriber: mcrosier.

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 make heavy use of isnan/isinf/isfinite to deal with corner cases involving non-finite quantities. This is problematic in two respects:

 1. On systems where these are always function calls (e.g. Linux/glibc), there is a performance penalty
 2. When compiling with -ffast-math, there is 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 patch 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.

http://reviews.llvm.org/D18639

Files:
  include/cmath
  include/complex

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18639.52178.patch
Type: text/x-patch
Size: 16913 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160331/759a902f/attachment-0001.bin>


More information about the cfe-commits mailing list