public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Matthias Kretz <m.kretz@gsi.de>
To: <gcc-patches@gcc.gnu.org>, <libstdc++@gcc.gnu.org>
Subject: Re: [PATCH 14/16] Implement hmin and hmax
Date: Mon, 1 Feb 2021 11:23:44 +0100	[thread overview]
Message-ID: <3529880.ebMzRN9Arp@excalibur> (raw)
In-Reply-To: <6999407.PJNiXcIEje@excalibur>

[-- Attachment #1: Type: text/plain, Size: 2129 bytes --]

On Mittwoch, 27. Januar 2021 21:42:50 CET Matthias Kretz wrote:
> --- a/libstdc++-v3/include/experimental/bits/simd.h
> +++ b/libstdc++-v3/include/experimental/bits/simd.h
> @@ -204,6 +204,27 @@ template <size_t _Np>
>  template <size_t _X>
>    using _SizeConstant = integral_constant<size_t, _X>;
> 
> +namespace __detail {
> +  struct _Minimum {
> +    template <typename _Tp>
> +      _GLIBCXX_SIMD_INTRINSIC constexpr
> +      _Tp
> +      operator()(_Tp __a, _Tp __b) const {

Reviewing my own patch :) This needs line breaks before { for namespace, 
struct, and operator(). And another line break before the next struct. New 
patch attached.

From: Matthias Kretz <kretz@kde.org>

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.

-- 
──────────────────────────────────────────────────────────────────────────
 Dr. Matthias Kretz                           https://mattkretz.github.io
 GSI Helmholtz Centre for Heavy Ion Research               https://gsi.de
 std::experimental::simd              https://github.com/VcDevel/std-simd
──────────────────────────────────────────────────────────────────────────

[-- Attachment #2: 0014-Implement-hmin-and-hmax.patch --]
[-- Type: text/x-patch, Size: 5359 bytes --]

diff --git a/libstdc++-v3/include/experimental/bits/simd.h b/libstdc++-v3/include/experimental/bits/simd.h
index 14179491f9d..a90cb3b2d98 100644
--- a/libstdc++-v3/include/experimental/bits/simd.h
+++ b/libstdc++-v3/include/experimental/bits/simd.h
@@ -204,6 +204,33 @@ template <size_t _Np>
 template <size_t _X>
   using _SizeConstant = integral_constant<size_t, _X>;
 
+namespace __detail
+{
+  struct _Minimum
+  {
+    template <typename _Tp>
+      _GLIBCXX_SIMD_INTRINSIC constexpr
+      _Tp
+      operator()(_Tp __a, _Tp __b) const
+      {
+	using std::min;
+	return min(__a, __b);
+      }
+  };
+
+  struct _Maximum
+  {
+    template <typename _Tp>
+      _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 <typename _Fp, size_t... _I>
@@ -3408,7 +3435,7 @@ template <typename _Tp, typename _Ap>
 
 // }}}1
 // reductions [simd.reductions] {{{1
-  template <typename _Tp, typename _Abi, typename _BinaryOperation = plus<>>
+template <typename _Tp, typename _Abi, typename _BinaryOperation = plus<>>
   _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _Tp
   reduce(const simd<_Tp, _Abi>& __v,
 	 _BinaryOperation __binary_op = _BinaryOperation())
@@ -3454,6 +3481,61 @@ template <typename _M, typename _V>
   reduce(const const_where_expression<_M, _V>& __x, bit_xor<> __binary_op)
   { return reduce(__x, 0, __binary_op); }
 
+template <typename _Tp, typename _Abi>
+  _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _Tp
+  hmin(const simd<_Tp, _Abi>& __v) noexcept
+  {
+    return _Abi::_SimdImpl::_S_reduce(__v, __detail::_Minimum());
+  }
+
+template <typename _Tp, typename _Abi>
+  _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR _Tp
+  hmax(const simd<_Tp, _Abi>& __v) noexcept
+  {
+    return _Abi::_SimdImpl::_S_reduce(__v, __detail::_Maximum());
+  }
+
+template <typename _M, typename _V>
+  _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 <typename _M, typename _V>
+  _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 <typename _Tp, typename _Ap>
diff --git a/libstdc++-v3/include/experimental/bits/simd_scalar.h b/libstdc++-v3/include/experimental/bits/simd_scalar.h
index 7680bc39c30..7e480ecdb37 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 <typename _Tp, typename _BinaryOperation>
     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 9d897d5ccd6..02df68fafbc 100644
--- a/libstdc++-v3/testsuite/experimental/simd/tests/reductions.cc
+++ b/libstdc++-v3/testsuite/experimental/simd/tests/reductions.cc
@@ -57,6 +57,8 @@ template <typename V>
     }
 
     {
+      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 <typename V>
 					}),
 	      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<V>({}, {1000}, [](V x) {

  reply	other threads:[~2021-02-01 10:23 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-27 20:36 [PATCH 00/16] stdx::simd fixes and testsuite improvements Matthias Kretz
2021-01-27 20:41 ` [PATCH 02/16] Fix NEON intrinsic types usage Matthias Kretz
2021-01-27 20:42 ` [PATCH 03/16] Support -mlong-double-64 on PPC Matthias Kretz
2021-01-27 20:42 ` [PATCH 04/16] Fix simd_mask<double> on POWER w/o POWER8 Matthias Kretz
2021-01-27 20:42 ` [PATCH 05/16] Fix several check-simd interaction issues Matthias Kretz
2021-01-27 20:42 ` [PATCH 06/16] Fix DRIVEROPTS and TESTFLAGS processing Matthias Kretz
2021-01-27 20:42 ` [PATCH 07/16] Fix incorrect display of old test summaries Matthias Kretz
2021-01-27 20:42 ` [PATCH 08/16] Immediate feedback with -v Matthias Kretz
2021-01-27 20:42 ` [PATCH 09/16] Fix mask reduction of simd_mask<double> on POWER7 Matthias Kretz
2021-01-27 20:42 ` [PATCH 10/16] Skip testing hypot3 for long double on PPC Matthias Kretz
2021-01-27 20:42 ` [PATCH 11/16] Abort test after 1000 lines of output Matthias Kretz
2021-01-27 20:42 ` [PATCH 12/16] Support timeout and timeout-factor options Matthias Kretz
2021-01-27 20:42 ` [PATCH 13/16] Improve test codegen for interpreting assembly Matthias Kretz
2021-02-02 15:02   ` Jonathan Wakely
2021-01-27 20:42 ` [PATCH 14/16] Implement hmin and hmax Matthias Kretz
2021-02-01 10:23   ` Matthias Kretz [this message]
2021-01-27 20:42 ` [PATCH 15/16] Work around test failures using -mno-tree-vrp Matthias Kretz
2021-01-27 20:42 ` [PATCH 16/16] Improve "find_first/last_set" for NEON Matthias Kretz
2021-02-03 15:52 ` [PATCH 00/16] stdx::simd fixes and testsuite improvements Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3529880.ebMzRN9Arp@excalibur \
    --to=m.kretz@gsi.de \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).