[libcxx-commits] [libcxx] [libc++] Simplify __bitset::__init (PR #121357)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Dec 30 12:31:55 PST 2024
https://github.com/winner245 created https://github.com/llvm/llvm-project/pull/121357
This PR slightly simplifies the code in `__bitset::__init` by eliminating the usage of a local array and avoiding redundant computations of the array size.
>From ba1ff894ca75304488e0c11bef847427d0323354 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Mon, 30 Dec 2024 15:28:49 -0500
Subject: [PATCH] Simplify __bitset::__init
---
libcxx/include/bitset | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/libcxx/include/bitset b/libcxx/include/bitset
index 919d2a0f07e096..6e1d07e1b5b26c 100644
--- a/libcxx/include/bitset
+++ b/libcxx/include/bitset
@@ -253,17 +253,15 @@ inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset() _NOEXCEPT
template <size_t _N_words, size_t _Size>
void __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT {
- __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
- size_t __sz = _Size;
- for (size_t __i = 0; __i < sizeof(__t) / sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word)
+ size_t __sz = _Size;
+ sizt_t __ull = sizeof(unsigned long long) / sizeof(__storage_type);
+ for (size_t __i = 0; __i < __ull; ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word)
if (__sz < __bits_per_word)
- __t[__i] = static_cast<__storage_type>(__v) & (1ULL << __sz) - 1;
+ __first_[__i] = static_cast<__storage_type>(__v) & (1ULL << __sz) - 1;
else
- __t[__i] = static_cast<__storage_type>(__v);
+ __first_[__i] = static_cast<__storage_type>(__v);
- std::copy(__t, __t + sizeof(__t) / sizeof(__t[0]), __first_);
- std::fill(
- __first_ + sizeof(__t) / sizeof(__t[0]), __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));
+ std::fill(__first_ + __ull, __first_ + _N_words, __storage_type(0));
}
template <size_t _N_words, size_t _Size>
@@ -272,7 +270,7 @@ inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned lon
if (_Size < __bits_per_word)
__first_[0] &= (1ULL << _Size) - 1;
- std::fill(__first_ + 1, __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));
+ std::fill(__first_ + 1, __first_ + _N_words), __storage_type(0));
}
# endif // _LIBCPP_CXX03_LANG
More information about the libcxx-commits
mailing list