[libcxx-commits] [libcxx] [libc++] Refactor bitset::{any, all} (PR #128445)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed May 21 09:33:20 PDT 2025


================
@@ -239,6 +250,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 <bool _Early_return, typename _Proj>
+  _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;
+  }
----------------
ldionne wrote:

I think you could get rid of the `_Early_return` parameter (which IMO is a misleading name since you return it all the time) and always return `true` instead. And then from the callers, you'd do:

```c++
_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());
}
```

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


More information about the libcxx-commits mailing list