]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Use memcmp to optimize std::bitset::_M_is_equal() [PR113807]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 8 Feb 2024 15:46:08 +0000 (15:46 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 1 Aug 2024 20:56:56 +0000 (21:56 +0100)
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

index ccd6d19f7a40113645aa209ef647f951a175e002..e5d677ff059c5d4e50225da0aa7167ad360e6f15 100644 (file)
@@ -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>