[libcxx] r283051 - Use __builtin_isnan/isinf/isfinite in complex

Hal Finkel via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 1 13:38:32 PDT 2016


Author: hfinkel
Date: Sat Oct  1 15:38:31 2016
New Revision: 283051

URL: http://llvm.org/viewvc/llvm-project?rev=283051&view=rev
Log:
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

Modified:
    libcxx/trunk/include/cmath
    libcxx/trunk/include/complex

Modified: libcxx/trunk/include/cmath
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/cmath?rev=283051&r1=283050&r2=283051&view=diff
==============================================================================
--- libcxx/trunk/include/cmath (original)
+++ libcxx/trunk/include/cmath Sat Oct  1 15:38:31 2016
@@ -558,6 +558,66 @@ hypot(_A1 __lcpp_x, _A2 __lcpp_y, _A3 __
 }
 #endif
 
+template <class _A1>
+_LIBCPP_ALWAYS_INLINE
+typename enable_if<is_floating_point<_A1>::value, bool>::type
+__libcpp_isnan(_A1 __lcpp_x) _NOEXCEPT
+{
+#if __has_builtin(__builtin_isnan)
+    return __builtin_isnan(__lcpp_x);
+#else
+    return isnan(__lcpp_x);
+#endif
+}
+
+template <class _A1>
+_LIBCPP_ALWAYS_INLINE
+typename enable_if<!is_floating_point<_A1>::value, bool>::type
+__libcpp_isnan(_A1 __lcpp_x) _NOEXCEPT
+{
+    return isnan(__lcpp_x);
+}
+
+template <class _A1>
+_LIBCPP_ALWAYS_INLINE
+typename enable_if<is_floating_point<_A1>::value, bool>::type
+__libcpp_isinf(_A1 __lcpp_x) _NOEXCEPT
+{
+#if __has_builtin(__builtin_isinf)
+    return __builtin_isinf(__lcpp_x);
+#else
+    return isinf(__lcpp_x);
+#endif
+}
+
+template <class _A1>
+_LIBCPP_ALWAYS_INLINE
+typename enable_if<!is_floating_point<_A1>::value, bool>::type
+__libcpp_isinf(_A1 __lcpp_x) _NOEXCEPT
+{
+    return isinf(__lcpp_x);
+}
+
+template <class _A1>
+_LIBCPP_ALWAYS_INLINE
+typename enable_if<is_floating_point<_A1>::value, bool>::type
+__libcpp_isfinite(_A1 __lcpp_x) _NOEXCEPT
+{
+#if __has_builtin(__builtin_isfinite)
+    return __builtin_isfinite(__lcpp_x);
+#else
+    return isfinite(__lcpp_x);
+#endif
+}
+
+template <class _A1>
+_LIBCPP_ALWAYS_INLINE
+typename enable_if<!is_floating_point<_A1>::value, bool>::type
+__libcpp_isfinite(_A1 __lcpp_x) _NOEXCEPT
+{
+    return isfinite(__lcpp_x);
+}
+
 _LIBCPP_END_NAMESPACE_STD
 
 #endif  // _LIBCPP_CMATH

Modified: libcxx/trunk/include/complex
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/complex?rev=283051&r1=283050&r2=283051&view=diff
==============================================================================
--- libcxx/trunk/include/complex (original)
+++ libcxx/trunk/include/complex Sat Oct  1 15:38:31 2016
@@ -599,39 +599,39 @@ operator*(const complex<_Tp>& __z, const
     _Tp __bc = __b * __c;
     _Tp __x = __ac - __bd;
     _Tp __y = __ad + __bc;
-    if (isnan(__x) && isnan(__y))
+    if (__libcpp_isnan(__x) && __libcpp_isnan(__y))
     {
         bool __recalc = false;
-        if (isinf(__a) || isinf(__b))
+        if (__libcpp_isinf(__a) || __libcpp_isinf(__b))
         {
-            __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
-            __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
-            if (isnan(__c))
+            __a = copysign(__libcpp_isinf(__a) ? _Tp(1) : _Tp(0), __a);
+            __b = copysign(__libcpp_isinf(__b) ? _Tp(1) : _Tp(0), __b);
+            if (__libcpp_isnan(__c))
                 __c = copysign(_Tp(0), __c);
-            if (isnan(__d))
+            if (__libcpp_isnan(__d))
                 __d = copysign(_Tp(0), __d);
             __recalc = true;
         }
-        if (isinf(__c) || isinf(__d))
+        if (__libcpp_isinf(__c) || __libcpp_isinf(__d))
         {
-            __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
-            __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
-            if (isnan(__a))
+            __c = copysign(__libcpp_isinf(__c) ? _Tp(1) : _Tp(0), __c);
+            __d = copysign(__libcpp_isinf(__d) ? _Tp(1) : _Tp(0), __d);
+            if (__libcpp_isnan(__a))
                 __a = copysign(_Tp(0), __a);
-            if (isnan(__b))
+            if (__libcpp_isnan(__b))
                 __b = copysign(_Tp(0), __b);
             __recalc = true;
         }
-        if (!__recalc && (isinf(__ac) || isinf(__bd) ||
-                          isinf(__ad) || isinf(__bc)))
+        if (!__recalc && (__libcpp_isinf(__ac) || __libcpp_isinf(__bd) ||
+                          __libcpp_isinf(__ad) || __libcpp_isinf(__bc)))
         {
-            if (isnan(__a))
+            if (__libcpp_isnan(__a))
                 __a = copysign(_Tp(0), __a);
-            if (isnan(__b))
+            if (__libcpp_isnan(__b))
                 __b = copysign(_Tp(0), __b);
-            if (isnan(__c))
+            if (__libcpp_isnan(__c))
                 __c = copysign(_Tp(0), __c);
-            if (isnan(__d))
+            if (__libcpp_isnan(__d))
                 __d = copysign(_Tp(0), __d);
             __recalc = true;
         }
@@ -674,7 +674,7 @@ operator/(const complex<_Tp>& __z, const
     _Tp __c = __w.real();
     _Tp __d = __w.imag();
     _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
-    if (isfinite(__logbw))
+    if (__libcpp_isfinite(__logbw))
     {
         __ilogbw = static_cast<int>(__logbw);
         __c = scalbn(__c, -__ilogbw);
@@ -683,24 +683,24 @@ operator/(const complex<_Tp>& __z, const
     _Tp __denom = __c * __c + __d * __d;
     _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
     _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
-    if (isnan(__x) && isnan(__y))
+    if (__libcpp_isnan(__x) && __libcpp_isnan(__y))
     {
-        if ((__denom == _Tp(0)) && (!isnan(__a) || !isnan(__b)))
+        if ((__denom == _Tp(0)) && (!__libcpp_isnan(__a) || !__libcpp_isnan(__b)))
         {
             __x = copysign(_Tp(INFINITY), __c) * __a;
             __y = copysign(_Tp(INFINITY), __c) * __b;
         }
-        else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
+        else if ((__libcpp_isinf(__a) || __libcpp_isinf(__b)) && __libcpp_isfinite(__c) && __libcpp_isfinite(__d))
         {
-            __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
-            __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
+            __a = copysign(__libcpp_isinf(__a) ? _Tp(1) : _Tp(0), __a);
+            __b = copysign(__libcpp_isinf(__b) ? _Tp(1) : _Tp(0), __b);
             __x = _Tp(INFINITY) * (__a * __c + __b * __d);
             __y = _Tp(INFINITY) * (__b * __c - __a * __d);
         }
-        else if (isinf(__logbw) && __logbw > _Tp(0) && isfinite(__a) && isfinite(__b))
+        else if (__libcpp_isinf(__logbw) && __logbw > _Tp(0) && __libcpp_isfinite(__a) && __libcpp_isfinite(__b))
         {
-            __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
-            __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
+            __c = copysign(__libcpp_isinf(__c) ? _Tp(1) : _Tp(0), __c);
+            __d = copysign(__libcpp_isinf(__d) ? _Tp(1) : _Tp(0), __d);
             __x = _Tp(0) * (__a * __c + __b * __d);
             __y = _Tp(0) * (__b * __c - __a * __d);
         }
@@ -910,9 +910,9 @@ inline _LIBCPP_INLINE_VISIBILITY
 _Tp
 norm(const complex<_Tp>& __c)
 {
-    if (isinf(__c.real()))
+    if (__libcpp_isinf(__c.real()))
         return abs(__c.real());
-    if (isinf(__c.imag()))
+    if (__libcpp_isinf(__c.imag()))
         return abs(__c.imag());
     return __c.real() * __c.real() + __c.imag() * __c.imag();
 }
@@ -955,7 +955,7 @@ complex<_Tp>
 proj(const complex<_Tp>& __c)
 {
     std::complex<_Tp> __r = __c;
-    if (isinf(__c.real()) || isinf(__c.imag()))
+    if (__libcpp_isinf(__c.real()) || __libcpp_isinf(__c.imag()))
         __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
     return __r;
 }
@@ -969,7 +969,7 @@ typename enable_if
 >::type
 proj(_Tp __re)
 {
-    if (isinf(__re))
+    if (__libcpp_isinf(__re))
         __re = abs(__re);
     return complex<_Tp>(__re);
 }
@@ -987,32 +987,31 @@ proj(_Tp __re)
     return _ComplexType(__re);
 }
 
-
 // polar
 
 template<class _Tp>
 complex<_Tp>
 polar(const _Tp& __rho, const _Tp& __theta = _Tp(0))
 {
-    if (isnan(__rho) || signbit(__rho))
+    if (__libcpp_isnan(__rho) || signbit(__rho))
         return complex<_Tp>(_Tp(NAN), _Tp(NAN));
-    if (isnan(__theta))
+    if (__libcpp_isnan(__theta))
     {
-        if (isinf(__rho))
+        if (__libcpp_isinf(__rho))
             return complex<_Tp>(__rho, __theta);
         return complex<_Tp>(__theta, __theta);
     }
-    if (isinf(__theta))
+    if (__libcpp_isinf(__theta))
     {
-        if (isinf(__rho))
+        if (__libcpp_isinf(__rho))
             return complex<_Tp>(__rho, _Tp(NAN));
         return complex<_Tp>(_Tp(NAN), _Tp(NAN));
     }
     _Tp __x = __rho * cos(__theta);
-    if (isnan(__x))
+    if (__libcpp_isnan(__x))
         __x = 0;
     _Tp __y = __rho * sin(__theta);
-    if (isnan(__y))
+    if (__libcpp_isnan(__y))
         __y = 0;
     return complex<_Tp>(__x, __y);
 }
@@ -1043,13 +1042,13 @@ template<class _Tp>
 complex<_Tp>
 sqrt(const complex<_Tp>& __x)
 {
-    if (isinf(__x.imag()))
+    if (__libcpp_isinf(__x.imag()))
         return complex<_Tp>(_Tp(INFINITY), __x.imag());
-    if (isinf(__x.real()))
+    if (__libcpp_isinf(__x.real()))
     {
         if (__x.real() > _Tp(0))
-            return complex<_Tp>(__x.real(), isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
-        return complex<_Tp>(isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
+            return complex<_Tp>(__x.real(), __libcpp_isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
+        return complex<_Tp>(__libcpp_isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
     }
     return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
 }
@@ -1061,21 +1060,21 @@ complex<_Tp>
 exp(const complex<_Tp>& __x)
 {
     _Tp __i = __x.imag();
-    if (isinf(__x.real()))
+    if (__libcpp_isinf(__x.real()))
     {
         if (__x.real() < _Tp(0))
         {
-            if (!isfinite(__i))
+            if (!__libcpp_isfinite(__i))
                 __i = _Tp(1);
         }
-        else if (__i == 0 || !isfinite(__i))
+        else if (__i == 0 || !__libcpp_isfinite(__i))
         {
-            if (isinf(__i))
+            if (__libcpp_isinf(__i))
                 __i = _Tp(NAN);
             return complex<_Tp>(__x.real(), __i);
         }
     }
-    else if (isnan(__x.real()) && __x.imag() == 0)
+    else if (__libcpp_isnan(__x.real()) && __x.imag() == 0)
         return __x;
     _Tp __e = exp(__x.real());
     return complex<_Tp>(__e * cos(__i), __e * sin(__i));
@@ -1133,23 +1132,23 @@ complex<_Tp>
 asinh(const complex<_Tp>& __x)
 {
     const _Tp __pi(atan2(+0., -0.));
-    if (isinf(__x.real()))
+    if (__libcpp_isinf(__x.real()))
     {
-        if (isnan(__x.imag()))
+        if (__libcpp_isnan(__x.imag()))
             return __x;
-        if (isinf(__x.imag()))
+        if (__libcpp_isinf(__x.imag()))
             return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
         return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
     }
-    if (isnan(__x.real()))
+    if (__libcpp_isnan(__x.real()))
     {
-        if (isinf(__x.imag()))
+        if (__libcpp_isinf(__x.imag()))
             return complex<_Tp>(__x.imag(), __x.real());
         if (__x.imag() == 0)
             return __x;
         return complex<_Tp>(__x.real(), __x.real());
     }
-    if (isinf(__x.imag()))
+    if (__libcpp_isinf(__x.imag()))
         return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
     complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) + _Tp(1)));
     return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
@@ -1162,11 +1161,11 @@ complex<_Tp>
 acosh(const complex<_Tp>& __x)
 {
     const _Tp __pi(atan2(+0., -0.));
-    if (isinf(__x.real()))
+    if (__libcpp_isinf(__x.real()))
     {
-        if (isnan(__x.imag()))
+        if (__libcpp_isnan(__x.imag()))
             return complex<_Tp>(abs(__x.real()), __x.imag());
-        if (isinf(__x.imag()))
+        if (__libcpp_isinf(__x.imag()))
         {
             if (__x.real() > 0)
                 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
@@ -1177,13 +1176,13 @@ acosh(const complex<_Tp>& __x)
             return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
         return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
     }
-    if (isnan(__x.real()))
+    if (__libcpp_isnan(__x.real()))
     {
-        if (isinf(__x.imag()))
+        if (__libcpp_isinf(__x.imag()))
             return complex<_Tp>(abs(__x.imag()), __x.real());
         return complex<_Tp>(__x.real(), __x.real());
     }
-    if (isinf(__x.imag()))
+    if (__libcpp_isinf(__x.imag()))
         return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
     complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
     return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
@@ -1196,21 +1195,21 @@ complex<_Tp>
 atanh(const complex<_Tp>& __x)
 {
     const _Tp __pi(atan2(+0., -0.));
-    if (isinf(__x.imag()))
+    if (__libcpp_isinf(__x.imag()))
     {
         return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
     }
-    if (isnan(__x.imag()))
+    if (__libcpp_isnan(__x.imag()))
     {
-        if (isinf(__x.real()) || __x.real() == 0)
+        if (__libcpp_isinf(__x.real()) || __x.real() == 0)
             return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
         return complex<_Tp>(__x.imag(), __x.imag());
     }
-    if (isnan(__x.real()))
+    if (__libcpp_isnan(__x.real()))
     {
         return complex<_Tp>(__x.real(), __x.real());
     }
-    if (isinf(__x.real()))
+    if (__libcpp_isinf(__x.real()))
     {
         return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
     }
@@ -1228,11 +1227,11 @@ template<class _Tp>
 complex<_Tp>
 sinh(const complex<_Tp>& __x)
 {
-    if (isinf(__x.real()) && !isfinite(__x.imag()))
+    if (__libcpp_isinf(__x.real()) && !__libcpp_isfinite(__x.imag()))
         return complex<_Tp>(__x.real(), _Tp(NAN));
-    if (__x.real() == 0 && !isfinite(__x.imag()))
+    if (__x.real() == 0 && !__libcpp_isfinite(__x.imag()))
         return complex<_Tp>(__x.real(), _Tp(NAN));
-    if (__x.imag() == 0 && !isfinite(__x.real()))
+    if (__x.imag() == 0 && !__libcpp_isfinite(__x.real()))
         return __x;
     return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
 }
@@ -1243,13 +1242,13 @@ template<class _Tp>
 complex<_Tp>
 cosh(const complex<_Tp>& __x)
 {
-    if (isinf(__x.real()) && !isfinite(__x.imag()))
+    if (__libcpp_isinf(__x.real()) && !__libcpp_isfinite(__x.imag()))
         return complex<_Tp>(abs(__x.real()), _Tp(NAN));
-    if (__x.real() == 0 && !isfinite(__x.imag()))
+    if (__x.real() == 0 && !__libcpp_isfinite(__x.imag()))
         return complex<_Tp>(_Tp(NAN), __x.real());
     if (__x.real() == 0 && __x.imag() == 0)
         return complex<_Tp>(_Tp(1), __x.imag());
-    if (__x.imag() == 0 && !isfinite(__x.real()))
+    if (__x.imag() == 0 && !__libcpp_isfinite(__x.real()))
         return complex<_Tp>(abs(__x.real()), __x.imag());
     return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
 }
@@ -1260,19 +1259,19 @@ template<class _Tp>
 complex<_Tp>
 tanh(const complex<_Tp>& __x)
 {
-    if (isinf(__x.real()))
+    if (__libcpp_isinf(__x.real()))
     {
-        if (!isfinite(__x.imag()))
+        if (!__libcpp_isfinite(__x.imag()))
             return complex<_Tp>(_Tp(1), _Tp(0));
         return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
     }
-    if (isnan(__x.real()) && __x.imag() == 0)
+    if (__libcpp_isnan(__x.real()) && __x.imag() == 0)
         return __x;
     _Tp __2r(_Tp(2) * __x.real());
     _Tp __2i(_Tp(2) * __x.imag());
     _Tp __d(cosh(__2r) + cos(__2i));
     _Tp __2rsh(sinh(__2r));
-    if (isinf(__2rsh) && isinf(__d))
+    if (__libcpp_isinf(__2rsh) && __libcpp_isinf(__d))
         return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
                             __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
     return  complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
@@ -1295,11 +1294,11 @@ complex<_Tp>
 acos(const complex<_Tp>& __x)
 {
     const _Tp __pi(atan2(+0., -0.));
-    if (isinf(__x.real()))
+    if (__libcpp_isinf(__x.real()))
     {
-        if (isnan(__x.imag()))
+        if (__libcpp_isnan(__x.imag()))
             return complex<_Tp>(__x.imag(), __x.real());
-        if (isinf(__x.imag()))
+        if (__libcpp_isinf(__x.imag()))
         {
             if (__x.real() < _Tp(0))
                 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
@@ -1309,13 +1308,13 @@ acos(const complex<_Tp>& __x)
             return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
         return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
     }
-    if (isnan(__x.real()))
+    if (__libcpp_isnan(__x.real()))
     {
-        if (isinf(__x.imag()))
+        if (__libcpp_isinf(__x.imag()))
             return complex<_Tp>(__x.real(), -__x.imag());
         return complex<_Tp>(__x.real(), __x.real());
     }
-    if (isinf(__x.imag()))
+    if (__libcpp_isinf(__x.imag()))
         return complex<_Tp>(__pi/_Tp(2), -__x.imag());
     if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
         return complex<_Tp>(__pi/_Tp(2), -__x.imag());




More information about the cfe-commits mailing list