[libcxx-commits] [libcxx] [libc++] Remove unnecessary division and modulo operations in bitset (PR #121312)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Mar 26 08:56:52 PDT 2025


================
@@ -464,10 +464,14 @@ protected:
     return __const_reference(&__first_, __storage_type(1) << __pos);
   }
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __iterator __make_iter(size_t __pos) _NOEXCEPT {
----------------
ldionne wrote:

I am tempted to see whether we can introduce `__begin()` and `__end()` functions instead, and get rid of `__make_iter` entirely. I'd imagine something like

```
iterator __begin() { return /* __make_iter(0); */ }
iterator __end() { return /* __make_iter(_Size); */ }

// and then here's an example usage
// OLD:
std::copy_backward(__base::__make_iter(0), __base::__make_iter(_Size - __pos), __base::__make_iter(_Size));

// NEW:
std::copy_backward(__begin(), __end() - __pos, __end());
```

Now the main question IMO is whether `__end() - __pos` produces equivalent code. I think we'll end up calling https://github.com/llvm/llvm-project/blob/52f941adbc2815487a0582ffedf3fb8cebe9cedd/libcxx/include/__bit_reference#L368, which might not produce efficient code. I still think we should investigate that since that looks like the way we'd want to write our code at a high level.

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


More information about the libcxx-commits mailing list