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: [PATCH 04/11] libstdc++: Make use of __builtin_bit_cast
Date: Tue, 8 Jun 2021 14:11:37 +0200	[thread overview]
Message-ID: <3553838.ebMzRN9Arp@excalibur> (raw)
In-Reply-To: <270527782.u9WJ3AIrlG@excalibur>

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



From: Matthias Kretz <kretz@kde.org>

The __bit_cast function was a hack to achieve what __builtin_bit_cast
can do, therefore use __builtin_bit_cast if possible. However,
__builtin_bit_cast cannot be used to cast from/to fixed_size_simd, since
it isn't trivially copyable (in the language sense — in principle it
is). Therefore add __proposed::simd_bit_cast to enable the use case
required in the test framework.

Signed-off-by: Matthias Kretz <m.kretz@gsi.de>

libstdc++-v3/ChangeLog:

	* include/experimental/bits/simd.h (__bit_cast): Implement via
	__builtin_bit_cast #if available.
	(__proposed::simd_bit_cast): Add overloads for simd and
	simd_mask, which use __builtin_bit_cast (or __bit_cast #if not
	available), which return an object of the requested type with
	the same bits as the argument.
	* include/experimental/bits/simd_math.h: Use simd_bit_cast
	instead of __bit_cast to allow casts to fixed_size_simd.
	* testsuite/experimental/simd/tests/bits/test_values.h: Switch
	from __bit_cast to __proposed::simd_bit_cast since the former
	will not cast fixed_size objects anymore.
---
 libstdc++-v3/include/experimental/bits/simd.h | 40 ++++++++++++++++++-
 .../include/experimental/bits/simd_math.h     |  8 ++--
 .../simd/tests/bits/test_values.h             |  8 ++--
 3 files changed, 46 insertions(+), 10 deletions(-)


--
──────────────────────────────────────────────────────────────────────────
 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: 0004-libstdc-Make-use-of-__builtin_bit_cast.patch --]
[-- Type: text/x-patch, Size: 4429 bytes --]

diff --git a/libstdc++-v3/include/experimental/bits/simd.h b/libstdc++-v3/include/experimental/bits/simd.h
index 163f1b574e2..5d243f22434 100644
--- a/libstdc++-v3/include/experimental/bits/simd.h
+++ b/libstdc++-v3/include/experimental/bits/simd.h
@@ -1598,7 +1598,9 @@ template <typename _To, typename _From>
   _GLIBCXX_SIMD_INTRINSIC constexpr _To
   __bit_cast(const _From __x)
   {
-    // TODO: implement with / replace by __builtin_bit_cast ASAP
+#if __has_builtin(__builtin_bit_cast)
+    return __builtin_bit_cast(_To, __x);
+#else
     static_assert(sizeof(_To) == sizeof(_From));
     constexpr bool __to_is_vectorizable
       = is_arithmetic_v<_To> || is_enum_v<_To>;
@@ -1629,6 +1631,7 @@ template <typename _To, typename _From>
 			 reinterpret_cast<const char*>(&__x), sizeof(_To));
 	return __r;
       }
+#endif
   }
 
 // }}}
@@ -2900,6 +2903,41 @@ template <typename _Tp, typename _Up, typename _Ap,
     return {__private_init, _RM::abi_type::_MaskImpl::template _S_convert<
 			      typename _RM::simd_type::value_type>(__x)};
   }
+
+template <typename _To, typename _Up, typename _Abi>
+  _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
+  _To
+  simd_bit_cast(const simd<_Up, _Abi>& __x)
+  {
+    using _Tp = typename _To::value_type;
+    using _ToMember = typename _SimdTraits<_Tp, typename _To::abi_type>::_SimdMember;
+    using _From = simd<_Up, _Abi>;
+    using _FromMember = typename _SimdTraits<_Up, _Abi>::_SimdMember;
+    // with concepts, the following should be constraints
+    static_assert(sizeof(_To) == sizeof(_From));
+    static_assert(is_trivially_copyable_v<_Tp> && is_trivially_copyable_v<_Up>);
+    static_assert(is_trivially_copyable_v<_ToMember> && is_trivially_copyable_v<_FromMember>);
+#if __has_builtin(__builtin_bit_cast)
+    return {__private_init, __builtin_bit_cast(_ToMember, __data(__x))};
+#else
+    return {__private_init, __bit_cast<_ToMember>(__data(__x))};
+#endif
+  }
+
+template <typename _To, typename _Up, typename _Abi>
+  _GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONSTEXPR
+  _To
+  simd_bit_cast(const simd_mask<_Up, _Abi>& __x)
+  {
+    using _From = simd_mask<_Up, _Abi>;
+    static_assert(sizeof(_To) == sizeof(_From));
+    static_assert(is_trivially_copyable_v<_To> && is_trivially_copyable_v<_From>);
+#if __has_builtin(__builtin_bit_cast)
+    return __builtin_bit_cast(_To, __x);
+#else
+    return __bit_cast<_To>(__x);
+#endif
+  }
 } // namespace __proposed
 
 // simd_cast {{{2
diff --git a/libstdc++-v3/include/experimental/bits/simd_math.h b/libstdc++-v3/include/experimental/bits/simd_math.h
index d954e761eee..3ade293fcbf 100644
--- a/libstdc++-v3/include/experimental/bits/simd_math.h
+++ b/libstdc++-v3/include/experimental/bits/simd_math.h
@@ -700,11 +700,9 @@ template <typename _Tp, typename _Abi>
 	// (inf and NaN are excluded by -ffinite-math-only)
 	const auto __iszero_inf_nan = __x == 0;
 #else
-	const auto __as_int
-	  = __bit_cast<rebind_simd_t<__int_for_sizeof_t<_Tp>, _V>>(abs(__x));
-	const auto __inf
-	  = __bit_cast<rebind_simd_t<__int_for_sizeof_t<_Tp>, _V>>(
-	    _V(__infinity_v<_Tp>));
+	using _Ip = __int_for_sizeof_t<_Tp>;
+	const auto __as_int = simd_bit_cast<rebind_simd_t<_Ip, _V>>(abs(__x));
+	const auto __inf = simd_bit_cast<rebind_simd_t<_Ip, _V>>(_V(__infinity_v<_Tp>));
 	const auto __iszero_inf_nan = static_simd_cast<typename _V::mask_type>(
 	  __as_int == 0 || __as_int >= __inf);
 #endif
diff --git a/libstdc++-v3/testsuite/experimental/simd/tests/bits/test_values.h b/libstdc++-v3/testsuite/experimental/simd/tests/bits/test_values.h
index b69bd0b704d..67aa870659b 100644
--- a/libstdc++-v3/testsuite/experimental/simd/tests/bits/test_values.h
+++ b/libstdc++-v3/testsuite/experimental/simd/tests/bits/test_values.h
@@ -221,11 +221,11 @@ template <class V>
     if constexpr (sizeof(T) <= sizeof(double))
       {
 	using I = rebind_simd_t<__int_for_sizeof_t<T>, V>;
-	const I abs_x = __bit_cast<I>(abs(x));
-	const I min = __bit_cast<I>(V(std::__norm_min_v<T>));
-	const I max = __bit_cast<I>(V(std::__finite_max_v<T>));
+	const I abs_x = simd_bit_cast<I>(abs(x));
+	const I min = simd_bit_cast<I>(V(std::__norm_min_v<T>));
+	const I max = simd_bit_cast<I>(V(std::__finite_max_v<T>));
 	return static_simd_cast<typename V::mask_type>(
-		 __bit_cast<I>(x) == 0 || (abs_x >= min && abs_x <= max));
+		 simd_bit_cast<I>(x) == 0 || (abs_x >= min && abs_x <= max));
       }
     else
       {

  parent reply	other threads:[~2021-06-08 12:11 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-08 12:10 [PATCH 00/11] stdx::simd optimizations, corrections, and cleanups Matthias Kretz
2021-06-08 12:11 ` [PATCH 01/11] libstdc++: Improve copysign codegen Matthias Kretz
2021-06-08 12:11 ` [PATCH 02/11] libstdc++: Remove dead code Matthias Kretz
2021-06-08 12:11 ` [PATCH 03/11] libstdc++: Improve fixed_size codegen Matthias Kretz
2021-06-08 12:11 ` Matthias Kretz [this message]
2021-06-11 10:53   ` [PATCH 04/11 v2] libstdc++: Make use of __builtin_bit_cast Matthias Kretz
2021-06-24 14:01     ` [PATCH 04/11 v3] " Matthias Kretz
2021-06-24 14:08       ` Jakub Jelinek
2021-06-24 14:11         ` Jonathan Wakely
2021-06-24 14:12           ` Jonathan Wakely
2021-06-24 14:21           ` Jakub Jelinek
2021-06-24 14:34             ` Jonathan Wakely
2021-06-24 14:40               ` Jonathan Wakely
2021-06-24 14:44                 ` Jakub Jelinek
2021-06-25 11:23       ` Jonathan Wakely
2021-06-08 12:11 ` [PATCH 05/11] libstdc++: Remove incorrect fabs overload Matthias Kretz
2021-06-08 12:11 ` [PATCH 06/11] libstdc++: Minor simd_math cleanups Matthias Kretz
2021-06-08 12:11 ` [PATCH 07/11] libstdc++: Fix condition when AVX512F ldexp implementation is used Matthias Kretz
2021-06-08 12:11 ` [PATCH 08/11] libstdc++: Avoid raising fp exceptions in trunc, floor, and ceil Matthias Kretz
2021-06-08 12:11 ` [PATCH 09/11] libstdc++: Ensure unrolled loops inline the lambda Matthias Kretz
2021-06-08 12:12 ` [PATCH 10/11] libstdc++: Fix internal names: add missing underscores Matthias Kretz
2021-06-08 12:12 ` [PATCH 11/11] libstdc++: Fix ODR issues with different -m flags Matthias Kretz
2021-06-09 12:22   ` Richard Biener
2021-06-09 12:53     ` Matthias Kretz
2021-06-09 13:22       ` Richard Biener
2021-11-15  8:57   ` Matthias Kretz
2022-01-14 21:30     ` Jonathan Wakely
2022-01-17  0:08       ` Jonathan Wakely
2021-06-24 13:42 ` [PATCH 00/11] stdx::simd optimizations, corrections, and cleanups 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=3553838.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).