public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Extend std::equal memcmp optimization to std::byte [PR101485]
@ 2024-06-28 19:24 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2024-06-28 19:24 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Tested x86_64-linux. Pushed to trunk.

-- >8 --

We optimize std::equal to memcmp for integers and pointers, which means
that std::byte comparisons generate bigger code than char comparisons.

We can't use memcmp for arbitrary enum types, because they could have an
overloaded operator== that has custom semantics, but we know that
std::byte doesn't do that.

libstdc++-v3/ChangeLog:

	PR libstdc++/101485
	* include/bits/stl_algobase.h (__equal_aux1): Check for
	std::byte as well.
	* testsuite/25_algorithms/equal/101485.cc: New test.
---
 libstdc++-v3/include/bits/stl_algobase.h             |  6 +++++-
 libstdc++-v3/testsuite/25_algorithms/equal/101485.cc | 11 +++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 libstdc++-v3/testsuite/25_algorithms/equal/101485.cc

diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index 57ff2f7cb08..dec1e4c79d8 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -1257,7 +1257,11 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
       typedef typename iterator_traits<_II1>::value_type _ValueType1;
       const bool __simple = ((__is_integer<_ValueType1>::__value
 #if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer)
-			      || __is_pointer(_ValueType1)
+				|| __is_pointer(_ValueType1)
+#endif
+#if __glibcxx_byte && __glibcxx_type_trait_variable_templates
+				// bits/cpp_type_traits.h declares std::byte
+				|| is_same_v<_ValueType1, byte>
 #endif
 			     ) && __memcmpable<_II1, _II2>::__value);
       return std::__equal<__simple>::equal(__first1, __last1, __first2);
diff --git a/libstdc++-v3/testsuite/25_algorithms/equal/101485.cc b/libstdc++-v3/testsuite/25_algorithms/equal/101485.cc
new file mode 100644
index 00000000000..1fbb40acae9
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/equal/101485.cc
@@ -0,0 +1,11 @@
+// { dg-options "-O0" }
+// { dg-do compile { target c++17 } }
+// { dg-final { scan-assembler "memcmp" } }
+
+#include <algorithm>
+#include <cstddef>
+
+bool eq(std::byte const* p, std::byte const* q, unsigned n)
+{
+  return std::equal(p, p + n, q);
+}
-- 
2.45.2


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-06-28 19:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-28 19:24 [committed] libstdc++: Extend std::equal memcmp optimization to std::byte [PR101485] Jonathan Wakely

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).