From: Matthias Kretz Date: Wed, 3 Feb 2021 15:49:30 +0000 (+0000) Subject: libstc++: Implement hmin and hmax X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=4b940ccee19496d8f494b7ad0b850f08c177d5bf;p=gcc.git libstc++: Implement hmin and hmax From 9.7.4 in Parallelism TS 2. For some reason I overlooked these two functions. Implement them via call to _S_reduce. libstdc++-v3/ChangeLog: * include/experimental/bits/simd.h: Add __detail::_Minimum and __detail::_Maximum to use them as _BinaryOperation to _S_reduce. Add hmin and hmax overloads for simd and const_where_expression. * include/experimental/bits/simd_scalar.h (_SimdImplScalar::_S_reduce): Make unused _BinaryOperation parameter const-ref to allow calling _S_reduce with an rvalue. * testsuite/experimental/simd/tests/reductions.cc: Add tests for hmin and hmax. Since the compiler statically determined that all tests pass, repeat the test after a call to make_value_unknown. --- diff --git a/libstdc++-v3/include/experimental/bits/simd.h b/libstdc++-v3/include/experimental/bits/simd.h index becd1d6a4bb..c452778832f 100644 --- a/libstdc++-v3/include/experimental/bits/simd.h +++ b/libstdc++-v3/include/experimental/bits/simd.h @@ -204,6 +204,33 @@ template template using _SizeConstant = integral_constant; +namespace __detail +{ + struct _Minimum + { + template + _GLIBCXX_SIMD_INTRINSIC constexpr + _Tp + operator()(_Tp __a, _Tp __b) const + { + using std::min; + return min(__a, __b); + } + }; + + struct _Maximum + { + template + _GLIBCXX_SIMD_INTRINSIC constexpr + _Tp + operator()(_Tp __a, _Tp __b) const + { + using std::max; + return max(__a, __b); + } + }; +} // namespace __detail + // unrolled/pack execution helpers // __execute_n_times{{{ template @@ -3408,7 +3435,7 @@ template // }}}1 // reductions [simd.reductions] {{{1 - template > +template > _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _Tp reduce(const simd<_Tp, _Abi>& __v, _BinaryOperation __binary_op = _BinaryOperation()) @@ -3454,6 +3481,61 @@ template reduce(const const_where_expression<_M, _V>& __x, bit_xor<> __binary_op) { return reduce(__x, 0, __binary_op); } +template + _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _Tp + hmin(const simd<_Tp, _Abi>& __v) noexcept + { + return _Abi::_SimdImpl::_S_reduce(__v, __detail::_Minimum()); + } + +template + _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _Tp + hmax(const simd<_Tp, _Abi>& __v) noexcept + { + return _Abi::_SimdImpl::_S_reduce(__v, __detail::_Maximum()); + } + +template + _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR + typename _V::value_type + hmin(const const_where_expression<_M, _V>& __x) noexcept + { + using _Tp = typename _V::value_type; + constexpr _Tp __id_elem = +#ifdef __FINITE_MATH_ONLY__ + __finite_max_v<_Tp>; +#else + __value_or<__infinity, _Tp>(__finite_max_v<_Tp>); +#endif + _V __tmp = __id_elem; + _V::_Impl::_S_masked_assign(__data(__get_mask(__x)), __data(__tmp), + __data(__get_lvalue(__x))); + return _V::abi_type::_SimdImpl::_S_reduce(__tmp, __detail::_Minimum()); + } + +template + _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR + typename _V::value_type + hmax(const const_where_expression<_M, _V>& __x) noexcept + { + using _Tp = typename _V::value_type; + constexpr _Tp __id_elem = +#ifdef __FINITE_MATH_ONLY__ + __finite_min_v<_Tp>; +#else + [] { + if constexpr (__value_exists_v<__infinity, _Tp>) + return -__infinity_v<_Tp>; + else + return __finite_min_v<_Tp>; + }(); +#endif + _V __tmp = __id_elem; + _V::_Impl::_S_masked_assign(__data(__get_mask(__x)), __data(__tmp), + __data(__get_lvalue(__x))); + return _V::abi_type::_SimdImpl::_S_reduce(__tmp, __detail::_Maximum()); + } + // }}}1 // algorithms [simd.alg] {{{ template diff --git a/libstdc++-v3/include/experimental/bits/simd_scalar.h b/libstdc++-v3/include/experimental/bits/simd_scalar.h index d5a90ef2292..48e13f6c719 100644 --- a/libstdc++-v3/include/experimental/bits/simd_scalar.h +++ b/libstdc++-v3/include/experimental/bits/simd_scalar.h @@ -182,7 +182,7 @@ struct _SimdImplScalar // _S_reduce {{{2 template static constexpr inline _Tp - _S_reduce(const simd<_Tp, simd_abi::scalar>& __x, _BinaryOperation&) + _S_reduce(const simd<_Tp, simd_abi::scalar>& __x, const _BinaryOperation&) { return __x._M_data; } // _S_min, _S_max {{{2 diff --git a/libstdc++-v3/testsuite/experimental/simd/tests/reductions.cc b/libstdc++-v3/testsuite/experimental/simd/tests/reductions.cc index 51aad92c862..1f20961825b 100644 --- a/libstdc++-v3/testsuite/experimental/simd/tests/reductions.cc +++ b/libstdc++-v3/testsuite/experimental/simd/tests/reductions.cc @@ -57,6 +57,8 @@ template } { + COMPARE(hmin(V(1)), T(1)); + COMPARE(hmax(V(1)), T(1)); const V z([](T i) { return i + 1; }); COMPARE(std::experimental::reduce(z, [](auto a, auto b) { @@ -79,6 +81,25 @@ template }), T(V::size() == 1 ? 117 : 2)) << "z: " << z; + COMPARE(hmin(z), T(1)); + COMPARE(hmax(z), T(V::size())); + if (V::size() > 1) + { + COMPARE(hmin(where(z > 1, z)), T(2)); + COMPARE(hmax(where(z > 1, z)), T(V::size())); + } + COMPARE(hmin(where(z < 4, z)), T(1)); + COMPARE(hmax(where(z < 4, z)), std::min(T(V::size()), T(3))); + const V zz = make_value_unknown(z); + COMPARE(hmin(zz), T(1)); + COMPARE(hmax(zz), T(V::size())); + if (V::size() > 1) + { + COMPARE(hmin(where(zz > 1, zz)), T(2)); + COMPARE(hmax(where(zz > 1, zz)), T(V::size())); + } + COMPARE(hmin(where(zz < 4, zz)), T(1)); + COMPARE(hmax(where(zz < 4, zz)), std::min(T(V::size()), T(3))); } test_values({}, {1000}, [](V x) {