Skip to content

Commit d6bfafe

Browse files
committed
Simplify __bitset::__init
1 parent f53abc2 commit d6bfafe

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

254254
template <size_t _N_words, size_t _Size>
255255
void __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT {
256-
__storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
257-
size_t __sz = _Size;
258-
for (size_t __i = 0; __i < sizeof(__t) / sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word)
259-
if (__sz < __bits_per_word)
260-
__t[__i] = static_cast<__storage_type>(__v) & (1ULL << __sz) - 1;
261-
else
262-
__t[__i] = static_cast<__storage_type>(__v);
263-
264-
std::copy(__t, __t + sizeof(__t) / sizeof(__t[0]), __first_);
265-
std::fill(
266-
__first_ + sizeof(__t) / sizeof(__t[0]), __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));
256+
const size_t __ull_words = sizeof(unsigned long long) / sizeof(__storage_type);
257+
for (size_t __i = 0; __i < __ull_words; ++__i, __v >>= __bits_per_word)
258+
__first_[__i] = static_cast<__storage_type>(__v);
259+
std::fill(__first_ + __ull_words, __first_ + _N_words, __storage_type(0));
267260
}
268261

269262
template <size_t _N_words, size_t _Size>
270263
inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT {
271264
__first_[0] = __v;
272-
if (_Size < __bits_per_word)
273-
__first_[0] &= (1ULL << _Size) - 1;
274-
275-
std::fill(__first_ + 1, __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));
265+
std::fill(__first_ + 1, __first_ + _N_words, __storage_type(0));
276266
}
277267

278268
# endif // _LIBCPP_CXX03_LANG
@@ -623,7 +613,8 @@ public:
623613

624614
// 23.3.5.1 constructors:
625615
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
626-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT : __base(__v) {}
616+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT
617+
: __base(sizeof(unsigned long long) * CHAR_BIT <= _Size ? __v : __v & ((1ULL << _Size) - 1)) {}
627618
template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>
628619
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
629620
const _CharT* __str,

0 commit comments

Comments
 (0)