Skip to content

Commit df5ae22

Browse files
committed
Improve bitset::to_ullong Implementation
1 parent 2a19efe commit df5ae22

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

libcxx/include/bitset

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ template <size_t N> struct hash<std::bitset<N>>;
136136
# include <__algorithm/fill.h>
137137
# include <__algorithm/fill_n.h>
138138
# include <__algorithm/find.h>
139+
# include <__algorithm/min.h>
139140
# include <__assert>
140141
# include <__bit/countr.h>
141142
# include <__bit/invert_if.h>
@@ -415,8 +416,9 @@ __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
415416
unsigned long long __r = __first_[0];
416417
_LIBCPP_DIAGNOSTIC_PUSH
417418
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
418-
for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
419-
__r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
419+
size_t __n_words = std::min<size_t>(_N_words, sizeof(unsigned long long) / sizeof(__storage_type));
420+
for (size_t __i = 1; __i < __n_words; ++__i)
421+
__r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT * __i);
420422
_LIBCPP_DIAGNOSTIC_POP
421423
return __r;
422424
}

0 commit comments

Comments
 (0)