[libcxx-commits] [libcxx] [libc++] Remove unnecessary division and modulo operations in bitset (PR #121312)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Mar 19 15:47:55 PDT 2025
https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/121312
>From 5fa4511bca837d753b6dfe2d64bb8769123117e8 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Sun, 29 Dec 2024 21:36:48 -0500
Subject: [PATCH 1/2] Remove unnecessary division and modulo operations in
bitset
---
libcxx/include/bitset | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/bitset b/libcxx/include/bitset
index 669a3947a87af..f7c05a8de3da3 100644
--- a/libcxx/include/bitset
+++ b/libcxx/include/bitset
@@ -464,10 +464,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;
>From 0f76a81a8040a1508a415c193c0493b62fcc9a14 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Wed, 19 Mar 2025 13:40:37 -0400
Subject: [PATCH 2/2] Add _LIBCPP_ASSERT_INTERNAL
---
libcxx/include/__bit_reference | 6 +++++-
libcxx/include/bitset | 8 ++++++--
2 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/libcxx/include/__bit_reference b/libcxx/include/__bit_reference
index f5c22fc0a3ade..ead4e9a6f2bf9 100644
--- a/libcxx/include/__bit_reference
+++ b/libcxx/include/__bit_reference
@@ -329,6 +329,7 @@ public:
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator*() const _NOEXCEPT {
+ _LIBCPP_ASSERT_INTERNAL(__ctz_ < __bits_per_word, "Dereferencing an invalid __bit_iterator.");
return __conditional_t<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >(
__seg_, __storage_type(1) << __ctz_);
}
@@ -453,7 +454,10 @@ private:
_LIBCPP_HIDE_FROM_ABI
_LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT
: __seg_(__s),
- __ctz_(__ctz) {}
+ __ctz_(__ctz) {
+ _LIBCPP_ASSERT_VALID_INPUT_RANGE(
+ __ctz_ < __bits_per_word, "__bit_iterator initialized with an invalid number of trailing zeros.");
+ }
friend typename _Cp::__self;
diff --git a/libcxx/include/bitset b/libcxx/include/bitset
index f7c05a8de3da3..9106080ec1020 100644
--- a/libcxx/include/bitset
+++ b/libcxx/include/bitset
@@ -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 {
- return __iterator(&__first_, __pos);
+ // Allow the == case to accommodate the past-the-end iterator.
+ _LIBCPP_ASSERT_INTERNAL(__pos <= __bits_per_word, "Out of bounds access in the single-word bitset implementation.");
+ return __pos != __bits_per_word ? __iterator(&__first_, __pos) : __iterator(&__first_ + 1, 0);
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __const_iterator __make_iter(size_t __pos) const _NOEXCEPT {
- return __const_iterator(&__first_, __pos);
+ // Allow the == case to accommodate the past-the-end iterator.
+ _LIBCPP_ASSERT_INTERNAL(__pos <= __bits_per_word, "Out of bounds access in the single-word bitset implementation.");
+ return __pos != __bits_per_word ? __const_iterator(&__first_, __pos) : __const_iterator(&__first_ + 1, 0);
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset& __v) _NOEXCEPT;
More information about the libcxx-commits
mailing list