Skip to content

Commit 9c12db6

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

File tree

1 file changed

+69
-14
lines changed

1 file changed

+69
-14
lines changed

libcxx/include/bitset

Lines changed: 69 additions & 14 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;
@@ -299,6 +301,36 @@ inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long
299301
# endif
300302
}
301303

304+
// template <size_t _N_words, size_t _Size>
305+
// inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
306+
// # ifndef _LIBCPP_CXX03_LANG
307+
// # if (__SIZEOF_LONG_LONG__ + __SIZEOF_SIZE_T__ - 1) / __SIZEOF_SIZE_T__ == 1
308+
// : __first_{static_cast<__storage_type>(__v)}
309+
// # elif (__SIZEOF_LONG_LONG__ + __SIZEOF_SIZE_T__ - 1) / __SIZEOF_SIZE_T__ == 2
310+
// : __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
311+
// # elif (__SIZEOF_LONG_LONG__ + __SIZEOF_SIZE_T__ - 1) / __SIZEOF_SIZE_T__ == 4
312+
// # if _N_words == 2
313+
// : __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
314+
// # elif _N_words == 3
315+
// : __first_{static_cast<__storage_type>(__v),
316+
// static_cast<__storage_type>(__v >> __bits_per_word),
317+
// static_cast<__storage_type>(__v >> (__bits_per_word * 2))}
318+
// # else
319+
// : __first_{static_cast<__storage_type>(__v),
320+
// static_cast<__storage_type>(__v >> __bits_per_word),
321+
// static_cast<__storage_type>(__v >> (__bits_per_word * 2)),
322+
// static_cast<__storage_type>(__v >> (__bits_per_word * 3))}
323+
// # endif
324+
// # else
325+
// # error This constructor has not been ported to this platform
326+
// # endif
327+
// # endif
328+
// {
329+
// # ifdef _LIBCPP_CXX03_LANG
330+
// __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
331+
// # endif
332+
// }
333+
302334
template <size_t _N_words, size_t _Size>
303335
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
304336
__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT {
@@ -344,15 +376,36 @@ __bitset<_N_words, _Size>::to_ulong(false_type) const {
344376
if (__i != __e)
345377
__throw_overflow_error("bitset to_ulong overflow error");
346378

347-
return __first_[0];
379+
return to_ulong(true_type());
348380
}
349381

350382
template <size_t _N_words, size_t _Size>
351383
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
352384
__bitset<_N_words, _Size>::to_ulong(true_type) const {
353385
return __first_[0];
386+
// return to_ulong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long)>());
354387
}
355388

389+
// template <size_t _N_words, size_t _Size>
390+
// inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
391+
// __bitset<_N_words, _Size>::to_ulong(true_type, false_type) const {
392+
// return static_cast<unsigned long>(__first_[0]);
393+
// }
394+
395+
// template <size_t _N_words, size_t _Size>
396+
// _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
397+
// __bitset<_N_words, _Size>::to_ulong(true_type, true_type) const {
398+
// unsigned long __r = static_cast<unsigned long>(__first_[0]);
399+
// _LIBCPP_DIAGNOSTIC_PUSH
400+
// _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
401+
// const size_t __ul_words = sizeof(unsigned long) / sizeof(__storage_type);
402+
// const size_t __n_words = _N_words < __ul_words ? _N_words : __ul_words;
403+
// for (size_t __i = 1; __i < __n_words; ++__i)
404+
// __r |= static_cast<unsigned long>(__first_[__i]) << (__bits_per_word * __i);
405+
// _LIBCPP_DIAGNOSTIC_POP
406+
// return __r;
407+
// }
408+
356409
template <size_t _N_words, size_t _Size>
357410
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
358411
__bitset<_N_words, _Size>::to_ullong(false_type) const {
@@ -373,19 +426,21 @@ __bitset<_N_words, _Size>::to_ullong(true_type) const {
373426
template <size_t _N_words, size_t _Size>
374427
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
375428
__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const {
376-
return __first_[0];
429+
return static_cast<unsigned long long>(__first_[0]);
377430
}
378431

379432
template <size_t _N_words, size_t _Size>
380433
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
381434
__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
382-
unsigned long long __r = __first_[0];
435+
unsigned long long __r = static_cast<unsigned long long>(__first_[0]);
383436
_LIBCPP_DIAGNOSTIC_PUSH
384437
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
385-
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;
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);
438+
// const size_t __ull_words = sizeof(unsigned long long) / sizeof(__storage_type);
439+
// const size_t __n_words = _N_words < __ull_words ? _N_words : __ull_words;
440+
// for (size_t __i = 1; __i < __n_words; ++__i)
441+
// __r |= static_cast<unsigned long long>(__first_[__i]) << (__bits_per_word * __i);
442+
for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
443+
__r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
389444
_LIBCPP_DIAGNOSTIC_POP
390445
return __r;
391446
}
@@ -494,8 +549,7 @@ inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset() _NOEXCEPT : __first_(0)
494549

495550
template <size_t _Size>
496551
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)) {}
552+
: __first_(static_cast<__storage_type>(__v)) {}
499553

500554
template <size_t _Size>
501555
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
@@ -524,12 +578,12 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<1, _Siz
524578

525579
template <size_t _Size>
526580
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _Size>::to_ulong() const {
527-
return __first_;
581+
return static_cast<unsigned long>(__first_);
528582
}
529583

530584
template <size_t _Size>
531585
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __bitset<1, _Size>::to_ullong() const {
532-
return __first_;
586+
return static_cast<unsigned long long>(__first_);
533587
}
534588

535589
template <size_t _Size>
@@ -595,8 +649,8 @@ protected:
595649

596650
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT {}
597651

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; }
652+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const { return 0UL; }
653+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const { return 0ULL; }
600654

601655
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { return true; }
602656
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT { return false; }
@@ -626,7 +680,8 @@ public:
626680

627681
// 23.3.5.1 constructors:
628682
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
629-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT : __base(__v) {}
683+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT
684+
: __base(sizeof(unsigned long long) * CHAR_BIT <= _Size ? __v : __v & ((1ULL << _Size) - 1)) {}
630685
template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>
631686
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
632687
const _CharT* __str,

0 commit comments

Comments
 (0)