Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify flip() for std::bitset #120807

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions libcxx/include/bitset
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

template <size_t _N_words, size_t _Size>
Expand Down Expand Up @@ -514,9 +510,7 @@ __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT {

template <size_t _Size>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<1, _Size>::flip() _NOEXCEPT {
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
__first_ = ~__first_;
__first_ &= __m;
__first_ ^= ~__storage_type(0) >> (__bits_per_word - _Size);
}

template <size_t _Size>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@

template <std::size_t N>
TEST_CONSTEXPR_CXX23 void test_flip_all() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
std::bitset<N> v1 = cases[c];
std::bitset<N> v2 = v1;
v2.flip();
for (std::size_t i = 0; i < v1.size(); ++i)
assert(v2[i] == ~v1[i]);
}
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
std::bitset<N> v1 = cases[c];
std::bitset<N> v2 = v1;
v2.flip();
for (std::size_t i = 0; i < v1.size(); ++i)
assert(v2[i] == ~v1[i]);
}
}

TEST_CONSTEXPR_CXX23 bool test() {
test_flip_all<0>();
test_flip_all<1>();
test_flip_all<2>();
test_flip_all<5>();
test_flip_all<31>();
test_flip_all<32>();
test_flip_all<33>();
Expand Down
Loading
Loading