[committed] libstdc++: Use memcmp to optimize std::bitset::_M_is_equal() [PR113807]

Jonathan Wakely jwakely@redhat.com
Thu Aug 1 21:11:44 GMT 2024


Tested x86_64-linux. Pushed to trunk.

-- >8 --

As noted in the PR the compiler doesn't seem able to do this on its own,
so we get better code at all optimization levels by using memcmp.

libstdc++-v3/ChangeLog:

	PR libstdc++/113807
	* include/std/bitset (bitset::_M_is_equal()): Use memcmp to
	optimize operator==.
---
 libstdc++-v3/include/std/bitset | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/libstdc++-v3/include/std/bitset b/libstdc++-v3/include/std/bitset
index ccd6d19f7a4..e5d677ff059 100644
--- a/libstdc++-v3/include/std/bitset
+++ b/libstdc++-v3/include/std/bitset
@@ -205,10 +205,16 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       _GLIBCXX14_CONSTEXPR bool
       _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT
       {
-	for (size_t __i = 0; __i < _Nw; ++__i)
-	  if (_M_w[__i] != __x._M_w[__i])
-	    return false;
-	return true;
+#if __cplusplus >= 201402L
+	if (__builtin_is_constant_evaluated())
+	  {
+	    for (size_t __i = 0; __i < _Nw; ++__i)
+	      if (_M_w[__i] != __x._M_w[__i])
+		return false;
+	    return true;
+	  }
+#endif
+	return !__builtin_memcmp(_M_w, __x._M_w, _Nw * sizeof(_WordT));
       }
 
       template<size_t _Nb>
-- 
2.45.2



More information about the Libstdc++ mailing list