[libcxx-commits] [libcxx] Simplify flip() for std::bitset (PR #120807)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 19 06:56:17 PST 2025


================
@@ -327,12 +327,8 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<_N_words, _Siz
   for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
     *__p = ~*__p;
   // do last partial word
-  if (__n > 0) {
-    __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
-    __storage_type __b = *__p & __m;
-    *__p &= ~__m;
-    *__p |= ~__b & __m;
-  }
+  if (__n > 0)
+    *__p ^= (__storage_type(1) << __n) - 1;
----------------
philnik777 wrote:

Do we care to not flip the entire word?

Answer: it looks like `std::hash` assumes that the last bits are always zeroed. Otherwise we would hash the same bitset differently.

Could you add a comment that the trailing padding bits being zero is part of the ABI?

https://github.com/llvm/llvm-project/pull/120807


More information about the libcxx-commits mailing list