Skip to content

Commit d6b9421

Browse files
committed
Simplify __bitset::__init
1 parent 61aab82 commit d6b9421

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

libcxx/include/bitset

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -256,26 +256,16 @@ inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset() _NOEXCEPT
256256

257257
template <size_t _N_words, size_t _Size>
258258
void __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT {
259-
__storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
260-
size_t __sz = _Size;
261-
for (size_t __i = 0; __i < sizeof(__t) / sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word)
262-
if (__sz < __bits_per_word)
263-
__t[__i] = static_cast<__storage_type>(__v) & (1ULL << __sz) - 1;
264-
else
265-
__t[__i] = static_cast<__storage_type>(__v);
266-
267-
std::copy(__t, __t + sizeof(__t) / sizeof(__t[0]), __first_);
268-
std::fill(
269-
__first_ + sizeof(__t) / sizeof(__t[0]), __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));
259+
const size_t __n_words = std::min((sizeof(unsigned long long) - 1) / sizeof(__storage_type) + 1, _N_words);
260+
for (size_t __i = 0; __i < __n_words; ++__i, __v >>= __bits_per_word)
261+
__first_[__i] = static_cast<__storage_type>(__v);
262+
std::fill(__first_ + __n_words, __first_ + _N_words, __storage_type(0));
270263
}
271264

272265
template <size_t _N_words, size_t _Size>
273266
inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT {
274267
__first_[0] = __v;
275-
if (_Size < __bits_per_word)
276-
__first_[0] &= (1ULL << _Size) - 1;
277-
278-
std::fill(__first_ + 1, __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));
268+
std::fill(__first_ + 1, __first_ + _N_words, __storage_type(0));
279269
}
280270

281271
# endif // _LIBCPP_CXX03_LANG
@@ -618,7 +608,8 @@ public:
618608

619609
// 23.3.5.1 constructors:
620610
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
621-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT : __base(__v) {}
611+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT
612+
: __base(sizeof(unsigned long long) * CHAR_BIT <= _Size ? __v : __v & ((1ULL << _Size) - 1)) {}
622613
template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>
623614
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
624615
const _CharT* __str,

0 commit comments

Comments
 (0)