]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Improve copysign(simd) codegen
authorMatthias Kretz <m.kretz@gsi.de>
Thu, 24 Jun 2021 13:20:13 +0000 (14:20 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 24 Jun 2021 13:20:13 +0000 (14:20 +0100)
This also resolves a test failure on aarch64 with -ffast-math and
fixed_size<N> with large N.

Signed-off-by: Matthias Kretz <m.kretz@gsi.de>
libstdc++-v3/ChangeLog:

* include/experimental/bits/simd.h: Add missing operator~
overload for simd<floating-point> to __float_bitwise_operators.
* include/experimental/bits/simd_builtin.h
(_SimdImplBuiltin::_S_complement): Bitcast to int (and back) to
implement complement for floating-point vectors.
* include/experimental/bits/simd_fixed_size.h
(_SimdImplFixedSize::_S_copysign): New function, forwarding to
copysign implementation of _SimdTuple members.
* include/experimental/bits/simd_math.h (copysign): Call
_SimdImpl::_S_copysign for fixed_size arguments. Simplify
generic copysign implementation using the new ~ operator.

libstdc++-v3/include/experimental/bits/simd.h
libstdc++-v3/include/experimental/bits/simd_builtin.h
libstdc++-v3/include/experimental/bits/simd_fixed_size.h
libstdc++-v3/include/experimental/bits/simd_math.h

index 59ddf3cc9581994c3387ec9ec4030d6a7e05a987..163f1b574e20af96156ed644bf89a5ea7589c14a 100644 (file)
@@ -5189,6 +5189,12 @@ template <typename _Tp, typename _Ap>
     return {__private_init,
            _Ap::_SimdImpl::_S_bit_and(__data(__a), __data(__b))};
   }
+
+template <typename _Tp, typename _Ap>
+  _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
+  enable_if_t<is_floating_point_v<_Tp>, simd<_Tp, _Ap>>
+  operator~(const simd<_Tp, _Ap>& __a)
+  { return {__private_init, _Ap::_SimdImpl::_S_complement(__data(__a))}; }
 } // namespace __float_bitwise_operators }}}
 
 _GLIBCXX_SIMD_END_NAMESPACE
index e986ee916208ad9a25d19ad6286e886172c3e086..8cd338e313f077a6f289ea40a35a200f7eaad19b 100644 (file)
@@ -1632,7 +1632,12 @@ template <typename _Abi>
     template <typename _Tp, size_t _Np>
       _GLIBCXX_SIMD_INTRINSIC static constexpr _SimdWrapper<_Tp, _Np>
       _S_complement(_SimdWrapper<_Tp, _Np> __x) noexcept
-      { return ~__x._M_data; }
+      {
+       if constexpr (is_floating_point_v<_Tp>)
+         return __vector_bitcast<_Tp>(~__vector_bitcast<__int_for_sizeof_t<_Tp>>(__x));
+       else
+         return ~__x._M_data;
+      }
 
     // _S_unary_minus {{{2
     template <typename _Tp, size_t _Np>
index 2722055c899d71d50ebe13b6eafaed9c7596384f..7c2c1df77c84a4e85d5b41c47749e9c677f79e82 100644 (file)
@@ -1663,7 +1663,7 @@ template <int _Np>
     _GLIBCXX_SIMD_APPLY_ON_TUPLE(_Tp, ldexp)
     _GLIBCXX_SIMD_APPLY_ON_TUPLE(_Tp, fmod)
     _GLIBCXX_SIMD_APPLY_ON_TUPLE(_Tp, remainder)
-    // copysign in simd_math.h
+    _GLIBCXX_SIMD_APPLY_ON_TUPLE(_Tp, copysign)
     _GLIBCXX_SIMD_APPLY_ON_TUPLE(_Tp, nextafter)
     _GLIBCXX_SIMD_APPLY_ON_TUPLE(_Tp, fdim)
     _GLIBCXX_SIMD_APPLY_ON_TUPLE(_Tp, fmax)
index 4799803a20042847e53ea7373ddebdc23d48d9b8..d954e761eee24ea8d37cae70092f6f52943ad87d 100644 (file)
@@ -1304,6 +1304,8 @@ template <typename _Tp, typename _Abi>
   {
     if constexpr (simd_size_v<_Tp, _Abi> == 1)
       return std::copysign(__x[0], __y[0]);
+    else if constexpr (__is_fixed_size_abi_v<_Abi>)
+      return {__private_init, _Abi::_SimdImpl::_S_copysign(__data(__x), __data(__y))};
     else if constexpr (is_same_v<_Tp, long double> && sizeof(_Tp) == 12)
       // Remove this case once __bit_cast is implemented via __builtin_bit_cast.
       // It is necessary, because __signmask below cannot be computed at compile
@@ -1315,7 +1317,7 @@ template <typename _Tp, typename _Abi>
        using _V = simd<_Tp, _Abi>;
        using namespace std::experimental::__float_bitwise_operators;
        _GLIBCXX_SIMD_USE_CONSTEXPR_API auto __signmask = _V(1) ^ _V(-1);
-       return (__x & (__x ^ __signmask)) | (__y & __signmask);
+       return (__x & ~__signmask) | (__y & __signmask);
       }
   }