From 3929727cda68d05120e972a6c74d791e47066286 Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Sun, 23 Feb 2025 19:11:53 -0500 Subject: [PATCH 1/3] Simplify bitset::{any, all} --- libcxx/include/bitset | 56 +++++++------------------------------------ 1 file changed, 8 insertions(+), 48 deletions(-) diff --git a/libcxx/include/bitset b/libcxx/include/bitset index eee5a51a39e24..b5fc6218a6319 100644 --- a/libcxx/include/bitset +++ b/libcxx/include/bitset @@ -226,8 +226,12 @@ protected: return to_ullong(integral_constant < bool, _Size< sizeof(unsigned long long) * CHAR_BIT>()); } - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT; - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT; + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { + return std::find(__make_iter(0), __make_iter(_Size), false) == __make_iter(_Size); + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT { + return std::find(__make_iter(0), __make_iter(_Size), true) != __make_iter(_Size); + } _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT; template @@ -405,40 +409,6 @@ __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const { return __r; } -template -_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Size>::all() const _NOEXCEPT { - // do middle whole words - size_t __n = _Size; - __const_storage_pointer __p = __first_; - for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) - if (~*__p) - return false; - // do last partial word - if (__n > 0) { - __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); - if (~*__p & __m) - return false; - } - return true; -} - -template -_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Size>::any() const _NOEXCEPT { - // do middle whole words - size_t __n = _Size; - __const_storage_pointer __p = __first_; - for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) - if (*__p) - return true; - // do last partial word - if (__n > 0) { - __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); - if (*__p & __m) - return true; - } - return false; -} - template inline size_t __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT { size_t __h = 0; @@ -753,8 +723,8 @@ public: _LIBCPP_HIDE_FROM_ABI bool operator!=(const bitset& __rhs) const _NOEXCEPT; # endif _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool test(size_t __pos) const; - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT; - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT; + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { return __base::all(); } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT { return __base::any(); } _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool none() const _NOEXCEPT { return !any(); } _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator<<(size_t __pos) const _NOEXCEPT; _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator>>(size_t __pos) const _NOEXCEPT; @@ -939,16 +909,6 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool bitset<_Size>::test(siz return (*this)[__pos]; } -template -inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool bitset<_Size>::all() const _NOEXCEPT { - return __base::all(); -} - -template -inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool bitset<_Size>::any() const _NOEXCEPT { - return __base::any(); -} - template inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size> bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT { From dc5611de732cf07127588b76c2ccbb27eeaacfd5 Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Sat, 17 May 2025 11:45:37 -0400 Subject: [PATCH 2/3] Refactor any and all --- libcxx/include/bitset | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/libcxx/include/bitset b/libcxx/include/bitset index b5fc6218a6319..fd542cbb08279 100644 --- a/libcxx/include/bitset +++ b/libcxx/include/bitset @@ -144,6 +144,7 @@ template struct hash>; # include <__cstddef/ptrdiff_t.h> # include <__cstddef/size_t.h> # include <__functional/hash.h> +# include <__functional/identity.h> # include <__functional/unary_function.h> # include <__type_traits/is_char_like_type.h> # include @@ -227,10 +228,10 @@ protected: } _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { - return std::find(__make_iter(0), __make_iter(_Size), false) == __make_iter(_Size); + return __scan_bits(__bit_not()); } _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT { - return std::find(__make_iter(0), __make_iter(_Size), true) != __make_iter(_Size); + return __scan_bits(std::__identity()); } _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT; @@ -250,6 +251,12 @@ protected: } private: + struct __bit_not { + template + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp operator()(const _Tp& __x) const _NOEXCEPT { + return ~__x; + } + }; # ifdef _LIBCPP_CXX03_LANG void __init(unsigned long long __v, false_type) _NOEXCEPT; _LIBCPP_HIDE_FROM_ABI void __init(unsigned long long __v, true_type) _NOEXCEPT; @@ -260,6 +267,23 @@ private: _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type) const; _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type, false_type) const; _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type, true_type) const; + + template + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool __scan_bits(_Proj __proj) const _NOEXCEPT { + size_t __n = _Size; + __const_storage_pointer __p = __first_; + // do middle whole words + for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) + if (__proj(*__p)) + return _Early_return; + // do last partial word + if (__n > 0) { + __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); + if (__proj(*__p) & __m) + return _Early_return; + } + return !_Early_return; + } }; template From f26d0142f66ab75683330e22880375331e856df1 Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Wed, 21 May 2025 15:27:02 -0400 Subject: [PATCH 3/3] remove _Early_return parameter --- libcxx/include/bitset | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/libcxx/include/bitset b/libcxx/include/bitset index fd542cbb08279..fdf28baace977 100644 --- a/libcxx/include/bitset +++ b/libcxx/include/bitset @@ -227,11 +227,9 @@ protected: return to_ullong(integral_constant < bool, _Size< sizeof(unsigned long long) * CHAR_BIT>()); } - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { - return __scan_bits(__bit_not()); - } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { return !__scan_bits(__bit_not()); } _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT { - return __scan_bits(std::__identity()); + return __scan_bits(std::__identity()); } _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT; @@ -268,21 +266,21 @@ private: _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type, false_type) const; _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type, true_type) const; - template + template _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool __scan_bits(_Proj __proj) const _NOEXCEPT { size_t __n = _Size; __const_storage_pointer __p = __first_; // do middle whole words for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) if (__proj(*__p)) - return _Early_return; + return true; // do last partial word if (__n > 0) { __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); if (__proj(*__p) & __m) - return _Early_return; + return true; } - return !_Early_return; + return false; } };