[libcxx-commits] [libcxx] Simplify flip() for std::bitset (PR #120807)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Dec 21 04:22:08 PST 2024
https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/120807
>From 67a745e75930c67fba89d796dae7d3f15e35b20d Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Fri, 20 Dec 2024 18:18:00 -0500
Subject: [PATCH] Simplify flip() for std::bit_set
---
libcxx/include/bitset | 18 +-
.../bitset.members/flip_all.pass.cpp | 18 +-
.../template.bitset/bitset_test_cases.h | 255 ++++++++++--------
3 files changed, 152 insertions(+), 139 deletions(-)
diff --git a/libcxx/include/bitset b/libcxx/include/bitset
index 8b361824805571..6291bbd66472eb 100644
--- a/libcxx/include/bitset
+++ b/libcxx/include/bitset
@@ -320,18 +320,8 @@ __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT {
template <size_t _N_words, size_t _Size>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<_N_words, _Size>::flip() _NOEXCEPT {
- // do middle whole words
- size_type __n = _Size;
- __storage_pointer __p = __first_;
- 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;
- }
+ for (size_type __i = 0; __i < _N_words; ++__i)
+ __first_[__i] = ~__first_[__i];
}
template <size_t _N_words, size_t _Size>
@@ -513,9 +503,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_ = ~__first_;
}
template <size_t _Size>
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 <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>();
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<std::bitset<N> > get_test_cases();
template <>
TEST_CONSTEXPR_CXX23 inline std::vector<std::bitset<0> > get_test_cases<0>() {
- std::vector<std::bitset<0> > cases;
- cases.push_back(std::bitset<0>());
- return cases;
+ std::vector<std::bitset<0> > cases;
+ cases.push_back(std::bitset<0>());
+ return cases;
}
template <>
TEST_CONSTEXPR_CXX23 inline std::vector<std::bitset<1> > get_test_cases<1>() {
- std::vector<std::bitset<1> > cases;
- cases.push_back(std::bitset<1>("0"));
- cases.push_back(std::bitset<1>("1"));
- return cases;
+ std::vector<std::bitset<1> > cases;
+ cases.push_back(std::bitset<1>("0"));
+ cases.push_back(std::bitset<1>("1"));
+ return cases;
}
template <>
TEST_CONSTEXPR_CXX23 inline std::vector<std::bitset<2> > get_test_cases<2>() {
- std::vector<std::bitset<2> > 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<std::bitset<2> > 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<std::bitset<5> > get_test_cases<5>() {
+ std::vector<std::bitset<5> > 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<std::bitset<31> > get_test_cases<31>() {
- std::vector<std::bitset<31> > 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<std::bitset<31> > 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<std::bitset<32> > get_test_cases<32>() {
- std::vector<std::bitset<32> > 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<std::bitset<32> > 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<std::bitset<33> > get_test_cases<33>() {
- std::vector<std::bitset<33> > 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<std::bitset<33> > 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<std::bitset<63> > get_test_cases<63>() {
- std::vector<std::bitset<63> > 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<std::bitset<63> > 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<std::bitset<64> > get_test_cases<64>() {
- std::vector<std::bitset<64> > 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<std::bitset<64> > 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<std::bitset<65> > get_test_cases<65>() {
- std::vector<std::bitset<65> > 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<std::bitset<65> > 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<std::bitset<1000> > get_test_cases<1000>() {
- std::vector<std::bitset<1000> > 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<std::bitset<1000> > 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
More information about the libcxx-commits
mailing list