Skip to content

Commit f95a580

Browse files
committed
Improve bitset::to_ullong Implementation
1 parent ba63150 commit f95a580

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_reference>
141142
# include <__config>
@@ -382,8 +383,9 @@ __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
382383
unsigned long long __r = __first_[0];
383384
_LIBCPP_DIAGNOSTIC_PUSH
384385
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
385-
for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
386-
__r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
386+
size_t __n_words = std::min<size_t>(_N_words, sizeof(unsigned long long) / sizeof(__storage_type));
387+
for (size_t __i = 1; __i < __n_words; ++__i)
388+
__r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT * __i);
387389
_LIBCPP_DIAGNOSTIC_POP
388390
return __r;
389391
}

0 commit comments

Comments
 (0)