From 6fed773a80b1b4dc39435ef63e4b3f9fd82ee87e Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Fri, 20 Dec 2024 18:18:00 -0500 Subject: [PATCH] Simplify flip() for std::bit_set --- libcxx/include/bitset | 12 +- .../bitset.members/flip_all.pass.cpp | 18 +- .../template.bitset/bitset_test_cases.h | 255 ++++++++++-------- 3 files changed, 152 insertions(+), 133 deletions(-) diff --git a/libcxx/include/bitset b/libcxx/include/bitset index 919d2a0f07e096..fc6b0b958c0bc8 100644 --- a/libcxx/include/bitset +++ b/libcxx/include/bitset @@ -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 @@ -514,9 +510,7 @@ __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT { template 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 diff --git a/libcxx/test/std/utilities/template.bitset/bitset.members/flip_all.pass.cpp b/libcxx/test/std/utilities/template.bitset/bitset.members/flip_all.pass.cpp index 79fa505e63cd3d..55401367cd08fc 100644 --- a/libcxx/test/std/utilities/template.bitset/bitset.members/flip_all.pass.cpp +++ b/libcxx/test/std/utilities/template.bitset/bitset.members/flip_all.pass.cpp @@ -18,19 +18,21 @@ template TEST_CONSTEXPR_CXX23 void test_flip_all() { - std::vector > const cases = get_test_cases(); - for (std::size_t c = 0; c != cases.size(); ++c) { - std::bitset v1 = cases[c]; - std::bitset v2 = v1; - v2.flip(); - for (std::size_t i = 0; i < v1.size(); ++i) - assert(v2[i] == ~v1[i]); - } + std::vector > const cases = get_test_cases(); + for (std::size_t c = 0; c != cases.size(); ++c) { + std::bitset v1 = cases[c]; + std::bitset 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>(); diff --git a/libcxx/test/std/utilities/template.bitset/bitset_test_cases.h b/libcxx/test/std/utilities/template.bitset/bitset_test_cases.h index dada9bcca8f486..1ca19ec469e2c9 100644 --- a/libcxx/test/std/utilities/template.bitset/bitset_test_cases.h +++ b/libcxx/test/std/utilities/template.bitset/bitset_test_cases.h @@ -20,161 +20,184 @@ TEST_CONSTEXPR_CXX23 std::vector > get_test_cases(); template <> TEST_CONSTEXPR_CXX23 inline std::vector > get_test_cases<0>() { - std::vector > cases; - cases.push_back(std::bitset<0>()); - return cases; + std::vector > cases; + cases.push_back(std::bitset<0>()); + return cases; } template <> TEST_CONSTEXPR_CXX23 inline std::vector > get_test_cases<1>() { - std::vector > cases; - cases.push_back(std::bitset<1>("0")); - cases.push_back(std::bitset<1>("1")); - return cases; + std::vector > cases; + cases.push_back(std::bitset<1>("0")); + cases.push_back(std::bitset<1>("1")); + return cases; } template <> TEST_CONSTEXPR_CXX23 inline std::vector > get_test_cases<2>() { - std::vector > cases; - cases.push_back(std::bitset<2>("00")); - cases.push_back(std::bitset<2>("01")); - cases.push_back(std::bitset<2>("10")); - cases.push_back(std::bitset<2>("11")); - return cases; + std::vector > cases; + cases.push_back(std::bitset<2>("00")); + cases.push_back(std::bitset<2>("01")); + cases.push_back(std::bitset<2>("10")); + cases.push_back(std::bitset<2>("11")); + return cases; +} + +template <> +TEST_CONSTEXPR_CXX23 inline std::vector > get_test_cases<5>() { + std::vector > cases; + cases.push_back(std::bitset<5>("00000")); + cases.push_back(std::bitset<5>("00001")); + cases.push_back(std::bitset<5>("10000")); + cases.push_back(std::bitset<5>("00010")); + cases.push_back(std::bitset<5>("01000")); + cases.push_back(std::bitset<5>("00011")); + cases.push_back(std::bitset<5>("11000")); + cases.push_back(std::bitset<5>("00100")); + cases.push_back(std::bitset<5>("11011")); + cases.push_back(std::bitset<5>("00101")); + cases.push_back(std::bitset<5>("10100")); + cases.push_back(std::bitset<5>("00110")); + cases.push_back(std::bitset<5>("01100")); + cases.push_back(std::bitset<5>("00111")); + cases.push_back(std::bitset<5>("11100")); + cases.push_back(std::bitset<5>("11111")); + return cases; } template <> TEST_CONSTEXPR_CXX23 inline std::vector > get_test_cases<31>() { - std::vector > cases; - cases.push_back(std::bitset<31>("0000000000000000000000000000000")); - cases.push_back(std::bitset<31>("0000000000000000000000000000001")); - cases.push_back(std::bitset<31>("1000000000000000000000000000000")); - cases.push_back(std::bitset<31>("1000000000000000000000000000001")); - cases.push_back(std::bitset<31>("1000000000000000000001000000001")); - cases.push_back(std::bitset<31>("0000000000000000111111111111111")); - cases.push_back(std::bitset<31>("1000000000000000111111111111111")); - cases.push_back(std::bitset<31>("1111111111111111000000000000000")); - cases.push_back(std::bitset<31>("1111111111111111000000000000001")); - cases.push_back(std::bitset<31>("1010101010101010101010101010101")); - cases.push_back(std::bitset<31>("0101010101010101010101010101010")); - cases.push_back(std::bitset<31>("1111111111111111111111111111111")); - return cases; + std::vector > cases; + cases.push_back(std::bitset<31>("0000000000000000000000000000000")); + cases.push_back(std::bitset<31>("0000000000000000000000000000001")); + cases.push_back(std::bitset<31>("1000000000000000000000000000000")); + cases.push_back(std::bitset<31>("1000000000000000000000000000001")); + cases.push_back(std::bitset<31>("1000000000000000000001000000001")); + cases.push_back(std::bitset<31>("0000000000000000111111111111111")); + cases.push_back(std::bitset<31>("1000000000000000111111111111111")); + cases.push_back(std::bitset<31>("1111111111111111000000000000000")); + cases.push_back(std::bitset<31>("1111111111111111000000000000001")); + cases.push_back(std::bitset<31>("1010101010101010101010101010101")); + cases.push_back(std::bitset<31>("0101010101010101010101010101010")); + cases.push_back(std::bitset<31>("1111111111111111111111111111111")); + return cases; } template <> TEST_CONSTEXPR_CXX23 inline std::vector > get_test_cases<32>() { - std::vector > cases; - cases.push_back(std::bitset<32>("00000000000000000000000000000000")); - cases.push_back(std::bitset<32>("00000000000000000000000000000001")); - cases.push_back(std::bitset<32>("10000000000000000000000000000000")); - cases.push_back(std::bitset<32>("10000000000000000000000000000001")); - cases.push_back(std::bitset<32>("10000000000000000000111000000001")); - cases.push_back(std::bitset<32>("00000000000000001111111111111111")); - cases.push_back(std::bitset<32>("10000000000000001111111111111111")); - cases.push_back(std::bitset<32>("11111111111111110000000000000000")); - cases.push_back(std::bitset<32>("11111111111111110000000000000001")); - cases.push_back(std::bitset<32>("10101010101010101010101010101010")); - cases.push_back(std::bitset<32>("01010101010101010101010101010101")); - cases.push_back(std::bitset<32>("11111111111111111111111111111111")); - return cases; + std::vector > cases; + cases.push_back(std::bitset<32>("00000000000000000000000000000000")); + cases.push_back(std::bitset<32>("00000000000000000000000000000001")); + cases.push_back(std::bitset<32>("10000000000000000000000000000000")); + cases.push_back(std::bitset<32>("10000000000000000000000000000001")); + cases.push_back(std::bitset<32>("10000000000000000000111000000001")); + cases.push_back(std::bitset<32>("00000000000000001111111111111111")); + cases.push_back(std::bitset<32>("10000000000000001111111111111111")); + cases.push_back(std::bitset<32>("11111111111111110000000000000000")); + cases.push_back(std::bitset<32>("11111111111111110000000000000001")); + cases.push_back(std::bitset<32>("10101010101010101010101010101010")); + cases.push_back(std::bitset<32>("01010101010101010101010101010101")); + cases.push_back(std::bitset<32>("11111111111111111111111111111111")); + return cases; } template <> TEST_CONSTEXPR_CXX23 inline std::vector > get_test_cases<33>() { - std::vector > cases; - cases.push_back(std::bitset<33>("000000000000000000000000000000000")); - cases.push_back(std::bitset<33>("000000000000000000000000000000001")); - cases.push_back(std::bitset<33>("100000000000000000000000000000000")); - cases.push_back(std::bitset<33>("100000000000000000000000000000001")); - cases.push_back(std::bitset<33>("100000000000000000001110000000001")); - cases.push_back(std::bitset<33>("000000000000000011111111111111111")); - cases.push_back(std::bitset<33>("100000000000000011111111111111111")); - cases.push_back(std::bitset<33>("111111111111111100000000000000000")); - cases.push_back(std::bitset<33>("111111111111111100000000000000001")); - cases.push_back(std::bitset<33>("101010101010101010101010101010101")); - cases.push_back(std::bitset<33>("010101010101010101010101010101010")); - cases.push_back(std::bitset<33>("111111111111111111111111111111111")); - return cases; + std::vector > cases; + cases.push_back(std::bitset<33>("000000000000000000000000000000000")); + cases.push_back(std::bitset<33>("000000000000000000000000000000001")); + cases.push_back(std::bitset<33>("100000000000000000000000000000000")); + cases.push_back(std::bitset<33>("100000000000000000000000000000001")); + cases.push_back(std::bitset<33>("100000000000000000001110000000001")); + cases.push_back(std::bitset<33>("000000000000000011111111111111111")); + cases.push_back(std::bitset<33>("100000000000000011111111111111111")); + cases.push_back(std::bitset<33>("111111111111111100000000000000000")); + cases.push_back(std::bitset<33>("111111111111111100000000000000001")); + cases.push_back(std::bitset<33>("101010101010101010101010101010101")); + cases.push_back(std::bitset<33>("010101010101010101010101010101010")); + cases.push_back(std::bitset<33>("111111111111111111111111111111111")); + return cases; } template <> TEST_CONSTEXPR_CXX23 inline std::vector > get_test_cases<63>() { - std::vector > cases; - cases.push_back(std::bitset<63>("000000000000000000000000000000000000000000000000000000000000000")); - cases.push_back(std::bitset<63>("000000000000000000000000000000000000000000000000000000000000001")); - cases.push_back(std::bitset<63>("100000000000000000000000000000000000000000000000000000000000000")); - cases.push_back(std::bitset<63>("100000000000000000000000000000000000000000000000000000000000001")); - cases.push_back(std::bitset<63>("100000000000000000000000001111100000000000000000000000000000001")); - cases.push_back(std::bitset<63>("000000000000000000000000000000001111111111111111111111111111111")); - cases.push_back(std::bitset<63>("100000000000000000000000000000001111111111111111111111111111111")); - cases.push_back(std::bitset<63>("111111111111111111111111111111110000000000000000000000000000000")); - cases.push_back(std::bitset<63>("111111111111111111111111111111110000000000000000000000000000001")); - cases.push_back(std::bitset<63>("101010101010101010101010101010101010101010101010101010101010101")); - cases.push_back(std::bitset<63>("010101010101010101010101010101010101010101010101010101010101010")); - cases.push_back(std::bitset<63>("111111111111111111111111111111111111111111111111111111111111111")); - return cases; + std::vector > cases; + cases.push_back(std::bitset<63>("000000000000000000000000000000000000000000000000000000000000000")); + cases.push_back(std::bitset<63>("000000000000000000000000000000000000000000000000000000000000001")); + cases.push_back(std::bitset<63>("100000000000000000000000000000000000000000000000000000000000000")); + cases.push_back(std::bitset<63>("100000000000000000000000000000000000000000000000000000000000001")); + cases.push_back(std::bitset<63>("100000000000000000000000001111100000000000000000000000000000001")); + cases.push_back(std::bitset<63>("000000000000000000000000000000001111111111111111111111111111111")); + cases.push_back(std::bitset<63>("100000000000000000000000000000001111111111111111111111111111111")); + cases.push_back(std::bitset<63>("111111111111111111111111111111110000000000000000000000000000000")); + cases.push_back(std::bitset<63>("111111111111111111111111111111110000000000000000000000000000001")); + cases.push_back(std::bitset<63>("101010101010101010101010101010101010101010101010101010101010101")); + cases.push_back(std::bitset<63>("010101010101010101010101010101010101010101010101010101010101010")); + cases.push_back(std::bitset<63>("111111111111111111111111111111111111111111111111111111111111111")); + return cases; } template <> TEST_CONSTEXPR_CXX23 inline std::vector > get_test_cases<64>() { - std::vector > cases; - cases.push_back(std::bitset<64>("0000000000000000000000000000000000000000000000000000000000000000")); - cases.push_back(std::bitset<64>("0000000000000000000000000000000000000000000000000000000000000001")); - cases.push_back(std::bitset<64>("1000000000000000000000000000000000000000000000000000000000000000")); - cases.push_back(std::bitset<64>("1000000000000000000000000000000000000000000000000000000000000001")); - cases.push_back(std::bitset<64>("1000000000000000000000000011111000000000000000000000000000000001")); - cases.push_back(std::bitset<64>("0000000000000000000000000000000011111111111111111111111111111111")); - cases.push_back(std::bitset<64>("1000000000000000000000000000000011111111111111111111111111111111")); - cases.push_back(std::bitset<64>("1111111111111111111111111111111100000000000000000000000000000000")); - cases.push_back(std::bitset<64>("1111111111111111111111111111111100000000000000000000000000000001")); - cases.push_back(std::bitset<64>("1010101010101010101010101010101010101010101010101010101010101010")); - cases.push_back(std::bitset<64>("0101010101010101010101010101010101010101010101010101010101010101")); - cases.push_back(std::bitset<64>("1111111111111111111111111111111111111111111111111111111111111111")); - return cases; + std::vector > cases; + cases.push_back(std::bitset<64>("0000000000000000000000000000000000000000000000000000000000000000")); + cases.push_back(std::bitset<64>("0000000000000000000000000000000000000000000000000000000000000001")); + cases.push_back(std::bitset<64>("1000000000000000000000000000000000000000000000000000000000000000")); + cases.push_back(std::bitset<64>("1000000000000000000000000000000000000000000000000000000000000001")); + cases.push_back(std::bitset<64>("1000000000000000000000000011111000000000000000000000000000000001")); + cases.push_back(std::bitset<64>("0000000000000000000000000000000011111111111111111111111111111111")); + cases.push_back(std::bitset<64>("1000000000000000000000000000000011111111111111111111111111111111")); + cases.push_back(std::bitset<64>("1111111111111111111111111111111100000000000000000000000000000000")); + cases.push_back(std::bitset<64>("1111111111111111111111111111111100000000000000000000000000000001")); + cases.push_back(std::bitset<64>("1010101010101010101010101010101010101010101010101010101010101010")); + cases.push_back(std::bitset<64>("0101010101010101010101010101010101010101010101010101010101010101")); + cases.push_back(std::bitset<64>("1111111111111111111111111111111111111111111111111111111111111111")); + return cases; } template <> TEST_CONSTEXPR_CXX23 inline std::vector > get_test_cases<65>() { - std::vector > cases; - cases.push_back(std::bitset<65>("00000000000000000000000000000000000000000000000000000000000000000")); - cases.push_back(std::bitset<65>("00000000000000000000000000000000000000000000000000000000000000001")); - cases.push_back(std::bitset<65>("10000000000000000000000000000000000000000000000000000000000000000")); - cases.push_back(std::bitset<65>("10000000000000000000000000000000000000000000000000000000000000001")); - cases.push_back(std::bitset<65>("10000000000000000000000000011111000000000000000000000000000000001")); - cases.push_back(std::bitset<65>("00000000000000000000000000000000011111111111111111111111111111111")); - cases.push_back(std::bitset<65>("10000000000000000000000000000000011111111111111111111111111111111")); - cases.push_back(std::bitset<65>("11111111111111111111111111111111000000000000000000000000000000000")); - cases.push_back(std::bitset<65>("11111111111111111111111111111111000000000000000000000000000000001")); - cases.push_back(std::bitset<65>("10101010101010101010101010101010101010101010101010101010101010101")); - cases.push_back(std::bitset<65>("01010101010101010101010101010101010101010101010101010101010101010")); - cases.push_back(std::bitset<65>("11111111111111111111111111111111111111111111111111111111111111111")); - return cases; + std::vector > cases; + cases.push_back(std::bitset<65>("00000000000000000000000000000000000000000000000000000000000000000")); + cases.push_back(std::bitset<65>("00000000000000000000000000000000000000000000000000000000000000001")); + cases.push_back(std::bitset<65>("10000000000000000000000000000000000000000000000000000000000000000")); + cases.push_back(std::bitset<65>("10000000000000000000000000000000000000000000000000000000000000001")); + cases.push_back(std::bitset<65>("10000000000000000000000000011111000000000000000000000000000000001")); + cases.push_back(std::bitset<65>("00000000000000000000000000000000011111111111111111111111111111111")); + cases.push_back(std::bitset<65>("10000000000000000000000000000000011111111111111111111111111111111")); + cases.push_back(std::bitset<65>("11111111111111111111111111111111000000000000000000000000000000000")); + cases.push_back(std::bitset<65>("11111111111111111111111111111111000000000000000000000000000000001")); + cases.push_back(std::bitset<65>("10101010101010101010101010101010101010101010101010101010101010101")); + cases.push_back(std::bitset<65>("01010101010101010101010101010101010101010101010101010101010101010")); + cases.push_back(std::bitset<65>("11111111111111111111111111111111111111111111111111111111111111111")); + return cases; } TEST_CONSTEXPR_CXX23 inline std::string str_repeat(std::string s, unsigned int n) { - std::string res = s; - for (; n != 0; --n) - res += s; - return res; + std::string res = s; + for (; n != 0; --n) + res += s; + return res; } template <> TEST_CONSTEXPR_CXX23 inline std::vector > get_test_cases<1000>() { - std::vector > cases; - cases.push_back(std::bitset<1000>(std::string(1000, '0'))); - cases.push_back(std::bitset<1000>(std::string(999, '0') + std::string(1, '1'))); - cases.push_back(std::bitset<1000>(std::string(1, '1') + std::string(999, '0'))); - cases.push_back(std::bitset<1000>(std::string(1, '1') + std::string(998, '0') + std::string(1, '1'))); - cases.push_back(std::bitset<1000>(std::string(1, '1') + std::string(400, '0') + std::string(99, '1') + std::string(499, '0') + std::string(1, '1'))); - cases.push_back(std::bitset<1000>(std::string(500, '0') + std::string(500, '1'))); - cases.push_back(std::bitset<1000>(std::string(1, '1') + std::string(499, '0') + std::string(500, '1'))); - cases.push_back(std::bitset<1000>(std::string(500, '1') + std::string(500, '0'))); - cases.push_back(std::bitset<1000>(std::string(500, '1') + std::string(499, '0') + std::string(1, '1'))); - cases.push_back(std::bitset<1000>(str_repeat("10", 500))); - cases.push_back(std::bitset<1000>(str_repeat("01", 500))); - cases.push_back(std::bitset<1000>(std::string(1000, '1'))); - - return cases; + std::vector > cases; + cases.push_back(std::bitset<1000>(std::string(1000, '0'))); + cases.push_back(std::bitset<1000>(std::string(999, '0') + std::string(1, '1'))); + cases.push_back(std::bitset<1000>(std::string(1, '1') + std::string(999, '0'))); + cases.push_back(std::bitset<1000>(std::string(1, '1') + std::string(998, '0') + std::string(1, '1'))); + cases.push_back(std::bitset<1000>(std::string(1, '1') + std::string(400, '0') + std::string(99, '1') + + std::string(499, '0') + std::string(1, '1'))); + cases.push_back(std::bitset<1000>(std::string(500, '0') + std::string(500, '1'))); + cases.push_back(std::bitset<1000>(std::string(1, '1') + std::string(499, '0') + std::string(500, '1'))); + cases.push_back(std::bitset<1000>(std::string(500, '1') + std::string(500, '0'))); + cases.push_back(std::bitset<1000>(std::string(500, '1') + std::string(499, '0') + std::string(1, '1'))); + cases.push_back(std::bitset<1000>(str_repeat("10", 500))); + cases.push_back(std::bitset<1000>(str_repeat("01", 500))); + cases.push_back(std::bitset<1000>(std::string(1000, '1'))); + + return cases; } #endif // !LIBCPP_TEST_BITSET_TEST_CASES_H