Skip to content

Commit d14095b

Browse files
committed
Apply @frederick-vs-ja suggestions to support 16-bit platforms
1 parent c1dfd79 commit d14095b

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

libcxx/include/bitset

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,23 @@ inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned lon
281281
template <size_t _N_words, size_t _Size>
282282
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
283283
# ifndef _LIBCPP_CXX03_LANG
284-
# if __SIZEOF_SIZE_T__ == 8
285-
: __first_{__v}
286-
# elif __SIZEOF_SIZE_T__ == 4
284+
# if (__SIZEOF_LONG_LONG__ + __SIZEOF_SIZE_T__ - 1) / __SIZEOF_SIZE_T__ == 1
285+
: __first_{static_cast<__storage_type>(__v)}
286+
# elif (__SIZEOF_LONG_LONG__ + __SIZEOF_SIZE_T__ - 1) / __SIZEOF_SIZE_T__ == 2
287+
: __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
288+
# elif (__SIZEOF_LONG_LONG__ + __SIZEOF_SIZE_T__ - 1) / __SIZEOF_SIZE_T__ == 4
289+
# if _N_words == 2
290+
: __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
291+
# elif _N_words == 3
287292
: __first_{static_cast<__storage_type>(__v),
288-
_Size >= 2 * __bits_per_word
289-
? static_cast<__storage_type>(__v >> __bits_per_word)
290-
: static_cast<__storage_type>((__v >> __bits_per_word) &
291-
(__storage_type(1) << (_Size - __bits_per_word)) - 1)}
293+
static_cast<__storage_type>(__v >> __bits_per_word),
294+
static_cast<__storage_type>(__v >> (__bits_per_word * 2))}
295+
# else
296+
: __first_{static_cast<__storage_type>(__v),
297+
static_cast<__storage_type>(__v >> __bits_per_word),
298+
static_cast<__storage_type>(__v >> (__bits_per_word * 2)),
299+
static_cast<__storage_type>(__v >> (__bits_per_word * 3))}
300+
# endif
292301
# else
293302
# error This constructor has not been ported to this platform
294303
# endif
@@ -379,13 +388,13 @@ __bitset<_N_words, _Size>::to_ullong(true_type, false_type) const {
379388
template <size_t _N_words, size_t _Size>
380389
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
381390
__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
382-
unsigned long long __r = __first_[0];
391+
unsigned long long __r = static_cast<unsigned long long>(__first_[0]);
383392
_LIBCPP_DIAGNOSTIC_PUSH
384393
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
385394
const size_t __ull_words = sizeof(unsigned long long) / sizeof(__storage_type);
386-
const size_t __n_words = _N_words < __ull_words ? _N_words : __ull_words;
395+
const size_t __n_words = _N_words < __ull_words ? _N_words : __ull_words;
387396
for (size_t __i = 1; __i < __n_words; ++__i)
388-
__r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT * __i);
397+
__r |= static_cast<unsigned long long>(__first_[__i]) << (__bits_per_word * __i);
389398
_LIBCPP_DIAGNOSTIC_POP
390399
return __r;
391400
}
@@ -626,7 +635,8 @@ public:
626635

627636
// 23.3.5.1 constructors:
628637
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
629-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT : __base(__v) {}
638+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT
639+
: __base(sizeof(unsigned long long) * CHAR_BIT <= _Size ? __v : __v & ((1ULL << _Size) - 1)) {}
630640
template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>
631641
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
632642
const _CharT* __str,

0 commit comments

Comments
 (0)