Skip to content

Commit 5751c13

Browse files
committed
Improve bitset::to_ullong Implementation
1 parent 6230f1b commit 5751c13

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
@@ -133,6 +133,7 @@ template <size_t N> struct hash<std::bitset<N>>;
133133
# include <__algorithm/fill.h>
134134
# include <__algorithm/fill_n.h>
135135
# include <__algorithm/find.h>
136+
# include <__algorithm/min.h>
136137
# include <__assert>
137138
# include <__bit_reference>
138139
# include <__config>
@@ -381,8 +382,9 @@ __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
381382
unsigned long long __r = __first_[0];
382383
_LIBCPP_DIAGNOSTIC_PUSH
383384
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
384-
for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
385-
__r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
385+
size_t __n_words = std::min<size_t>(_N_words, sizeof(unsigned long long) / sizeof(__storage_type));
386+
for (size_t __i = 1; __i < __n_words; ++__i)
387+
__r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT * __i);
386388
_LIBCPP_DIAGNOSTIC_POP
387389
return __r;
388390
}

0 commit comments

Comments
 (0)