Skip to content

Commit df81dc4

Browse files
committed
Simplify __bitset::__init
1 parent 385752c commit df81dc4

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
@@ -273,26 +273,16 @@ inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset() _NOEXCEPT
273273

274274
template <size_t _N_words, size_t _Size>
275275
void __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT {
276-
__storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
277-
size_t __sz = _Size;
278-
for (size_t __i = 0; __i < sizeof(__t) / sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word)
279-
if (__sz < __bits_per_word)
280-
__t[__i] = static_cast<__storage_type>(__v) & (1ULL << __sz) - 1;
281-
else
282-
__t[__i] = static_cast<__storage_type>(__v);
283-
284-
std::copy(__t, __t + sizeof(__t) / sizeof(__t[0]), __first_);
285-
std::fill(
286-
__first_ + sizeof(__t) / sizeof(__t[0]), __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));
276+
const size_t __n_words = std::min((sizeof(unsigned long long) - 1) / sizeof(__storage_type) + 1, _N_words);
277+
for (size_t __i = 0; __i < __n_words; ++__i, __v >>= __bits_per_word)
278+
__first_[__i] = static_cast<__storage_type>(__v);
279+
std::fill(__first_ + __n_words, __first_ + _N_words, __storage_type(0));
287280
}
288281

289282
template <size_t _N_words, size_t _Size>
290283
inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT {
291284
__first_[0] = __v;
292-
if (_Size < __bits_per_word)
293-
__first_[0] &= (1ULL << _Size) - 1;
294-
295-
std::fill(__first_ + 1, __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));
285+
std::fill(__first_ + 1, __first_ + _N_words, __storage_type(0));
296286
}
297287

298288
# endif // _LIBCPP_CXX03_LANG
@@ -658,7 +648,8 @@ public:
658648

659649
// 23.3.5.1 constructors:
660650
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
661-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT : __base(__v) {}
651+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT
652+
: __base(sizeof(unsigned long long) * CHAR_BIT <= _Size ? __v : __v & ((1ULL << _Size) - 1)) {}
662653
template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>
663654
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
664655
const _CharT* __str,

0 commit comments

Comments
 (0)