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

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Mar 18 08:59:48 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Peng Liu (winner245)

<details>
<summary>Changes</summary>

The PR removes the unnecessary division and modulo operations in the one-word specialization `__bitset<1, _Size>`. The reason is that for the one-word specialization, we have `__pos < __bits_per_word` (as `__bitset<1, _Size>` is an implementation detail only used by the public `bitset`). So `__pos / __bits_per_word == 0` and `__pos / __pos % __bits_per_word == __pos`. 

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


1 Files Affected:

- (modified) libcxx/include/bitset (+2-2) 


``````````diff
diff --git a/libcxx/include/bitset b/libcxx/include/bitset
index a20842985b3d5..31e960cc8797b 100644
--- a/libcxx/include/bitset
+++ b/libcxx/include/bitset
@@ -465,10 +465,10 @@ protected:
     return __const_reference(&__first_, __storage_type(1) << __pos);
   }
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __iterator __make_iter(size_t __pos) _NOEXCEPT {
-    return __iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
+    return __iterator(&__first_, __pos);
   }
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __const_iterator __make_iter(size_t __pos) const _NOEXCEPT {
-    return __const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
+    return __const_iterator(&__first_, __pos);
   }
 
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset& __v) _NOEXCEPT;

``````````

</details>


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


More information about the libcxx-commits mailing list