[libcxx-commits] [libcxx] [libc++] Simplify vector<bool>::flip() and add new tests (PR #119607)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 12 07:55:53 PST 2024


================
@@ -1049,18 +1049,14 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::resize(size_type __
 
 template <class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::flip() _NOEXCEPT {
-  // do middle whole words
+  // Process the whole words in the front
   size_type __n         = __size_;
   __storage_pointer __p = __begin_;
   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;
-  }
+  // Process the last partial word, if it exists
----------------
winner245 wrote:

Thank you for your clarification. I agree that unconditionally flipping the unused bits in the last word would simplify the code further. My current implementation keeps those bits intact—they are neither zeroed nor changed in any way, which seems 100% safe to me. However, your suggestion makes sense as well, considering those unused bits shouldn't be used by others in any way. I'll proceed with updating the implementation to flip the entire word and ensure it passes all CI checks.

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


More information about the libcxx-commits mailing list