Skip to content

Commit 06e85c2

Browse files
committed
Apply @frederick-vs-ja suggestions to support 16-bit platforms
1 parent 5c636ec commit 06e85c2

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

libcxx/include/bitset

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ private:
233233
# endif // _LIBCPP_CXX03_LANG
234234
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(false_type) const;
235235
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(true_type) const;
236+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(true_type, false_type) const;
237+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(true_type, true_type) const;
236238
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(false_type) const;
237239
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type) const;
238240
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type, false_type) const;
@@ -281,20 +283,15 @@ inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned lon
281283
template <size_t _N_words, size_t _Size>
282284
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
283285
# ifndef _LIBCPP_CXX03_LANG
284-
# if __SIZEOF_SIZE_T__ == 8
285-
: __first_{__v}
286-
# elif __SIZEOF_SIZE_T__ == 4
287-
: __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)}
292-
# else
293-
# error This constructor has not been ported to this platform
294-
# endif
286+
: __first_{static_cast<__storage_type>(__v)}
295287
# endif
296288
{
297-
# ifdef _LIBCPP_CXX03_LANG
289+
# ifndef _LIBCPP_CXX03_LANG
290+
const size_t __ull_words = sizeof(unsigned long long) / sizeof(__storage_type);
291+
const size_t __n_words = _N_words < __ull_words ? _N_words : __ull_words;
292+
for (size_t __i = 1; __i < __n_words; ++__i)
293+
__first_[__i] = static_cast<__storage_type>(__v >> __bits_per_word * __i);
294+
# else
298295
__init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
299296
# endif
300297
}
@@ -350,9 +347,29 @@ __bitset<_N_words, _Size>::to_ulong(false_type) const {
350347
template <size_t _N_words, size_t _Size>
351348
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
352349
__bitset<_N_words, _Size>::to_ulong(true_type) const {
350+
return to_ulong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long)>());
351+
}
352+
353+
template <size_t _N_words, size_t _Size>
354+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
355+
__bitset<_N_words, _Size>::to_ulong(true_type, false_type) const {
353356
return __first_[0];
354357
}
355358

359+
template <size_t _N_words, size_t _Size>
360+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
361+
__bitset<_N_words, _Size>::to_ulong(true_type, true_type) const {
362+
unsigned long __r = __first_[0];
363+
_LIBCPP_DIAGNOSTIC_PUSH
364+
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
365+
const size_t __ul_words = sizeof(unsigned long) / sizeof(__storage_type);
366+
const size_t __n_words = _N_words < __ul_words ? _N_words : __ul_words;
367+
for (size_t __i = 1; __i < __n_words; ++__i)
368+
__r |= static_cast<unsigned long>(__first_[__i]) << (__bits_per_word * __i);
369+
_LIBCPP_DIAGNOSTIC_POP
370+
return __r;
371+
}
372+
356373
template <size_t _N_words, size_t _Size>
357374
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
358375
__bitset<_N_words, _Size>::to_ullong(false_type) const {
@@ -383,9 +400,9 @@ __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
383400
_LIBCPP_DIAGNOSTIC_PUSH
384401
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
385402
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;
403+
const size_t __n_words = _N_words < __ull_words ? _N_words : __ull_words;
387404
for (size_t __i = 1; __i < __n_words; ++__i)
388-
__r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT * __i);
405+
__r |= static_cast<unsigned long long>(__first_[__i]) << (__bits_per_word * __i);
389406
_LIBCPP_DIAGNOSTIC_POP
390407
return __r;
391408
}
@@ -494,8 +511,7 @@ inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset() _NOEXCEPT : __first_(0)
494511

495512
template <size_t _Size>
496513
inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
497-
: __first_(_Size == __bits_per_word ? static_cast<__storage_type>(__v)
498-
: static_cast<__storage_type>(__v) & ((__storage_type(1) << _Size) - 1)) {}
514+
: __first_(static_cast<__storage_type>(__v)) {}
499515

500516
template <size_t _Size>
501517
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
@@ -524,12 +540,12 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<1, _Siz
524540

525541
template <size_t _Size>
526542
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _Size>::to_ulong() const {
527-
return __first_;
543+
return static_cast<unsigned long>(__first_);
528544
}
529545

530546
template <size_t _Size>
531547
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __bitset<1, _Size>::to_ullong() const {
532-
return __first_;
548+
return static_cast<unsigned long long>(__first_);
533549
}
534550

535551
template <size_t _Size>
@@ -595,8 +611,8 @@ protected:
595611

596612
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT {}
597613

598-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const { return 0; }
599-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const { return 0; }
614+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const { return 0UL; }
615+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const { return 0ULL; }
600616

601617
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { return true; }
602618
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT { return false; }
@@ -626,7 +642,8 @@ public:
626642

627643
// 23.3.5.1 constructors:
628644
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
629-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT : __base(__v) {}
645+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT
646+
: __base(sizeof(unsigned long long) * CHAR_BIT <= _Size ? __v : __v & (1ULL << _Size) - 1) {}
630647
template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>
631648
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
632649
const _CharT* __str,

0 commit comments

Comments
 (0)