Skip to content

Commit ce12a11

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

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

libcxx/include/bitset

Lines changed: 52 additions & 19 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,16 +283,27 @@ 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
286+
# if (__SIZEOF_LONG_LONG__ + __SIZEOF_SIZE_T__ - 1) / __SIZEOF_SIZE_T__ == 1
287+
: __first_{static_cast<__storage_type>(__v)}
288+
# elif (__SIZEOF_LONG_LONG__ + __SIZEOF_SIZE_T__ - 1) / __SIZEOF_SIZE_T__ == 2
287289
: __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)}
290+
static_cast<__storage_type>(__v >> (sizeof(__storage_type) * CHAR_BIT))}
291+
# elif (__SIZEOF_LONG_LONG__ + __SIZEOF_SIZE_T__ - 1) / __SIZEOF_SIZE_T__ == 4
292+
# if _N_words == 2
293+
: __first_{static_cast<__storage_type>(__v),
294+
static_cast<__storage_type>(__v >> (sizeof(__storage_type) * CHAR_BIT))}
295+
# elif _N_words == 3
296+
: __first_{static_cast<__storage_type>(__v),
297+
static_cast<__storage_type>(__v >> (sizeof(__storage_type) * CHAR_BIT)),
298+
static_cast<__storage_type>(__v >> (sizeof(__storage_type) * CHAR_BIT * 2))}
299+
# else
300+
: __first_{static_cast<__storage_type>(__v),
301+
static_cast<__storage_type>(__v >> (sizeof(__storage_type) * CHAR_BIT)),
302+
static_cast<__storage_type>(__v >> (sizeof(__storage_type) * CHAR_BIT * 2)),
303+
static_cast<__storage_type>(__v >> (sizeof(__storage_type) * CHAR_BIT * 3))}
304+
# endif
292305
# else
293-
# error This constructor has not been ported to this platform
306+
error This constructor has not been ported to this platform
294307
# endif
295308
# endif
296309
{
@@ -344,13 +357,33 @@ __bitset<_N_words, _Size>::to_ulong(false_type) const {
344357
if (__i != __e)
345358
__throw_overflow_error("bitset to_ulong overflow error");
346359

347-
return __first_[0];
360+
return to_ulong(true_type());
348361
}
349362

350363
template <size_t _N_words, size_t _Size>
351364
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
352365
__bitset<_N_words, _Size>::to_ulong(true_type) const {
353-
return __first_[0];
366+
return to_ulong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long)>());
367+
}
368+
369+
template <size_t _N_words, size_t _Size>
370+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
371+
__bitset<_N_words, _Size>::to_ulong(true_type, false_type) const {
372+
return static_cast<unsigned long>(__first_[0]);
373+
}
374+
375+
template <size_t _N_words, size_t _Size>
376+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
377+
__bitset<_N_words, _Size>::to_ulong(true_type, true_type) const {
378+
unsigned long __r = __first_[0];
379+
_LIBCPP_DIAGNOSTIC_PUSH
380+
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
381+
const size_t __ul_words = sizeof(unsigned long) / sizeof(__storage_type);
382+
const size_t __n_words = _N_words < __ul_words ? _N_words : __ul_words;
383+
for (size_t __i = 1; __i < __n_words; ++__i)
384+
__r |= static_cast<unsigned long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT * __i);
385+
_LIBCPP_DIAGNOSTIC_POP
386+
return __r;
354387
}
355388

356389
template <size_t _N_words, size_t _Size>
@@ -373,7 +406,7 @@ __bitset<_N_words, _Size>::to_ullong(true_type) const {
373406
template <size_t _N_words, size_t _Size>
374407
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
375408
__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const {
376-
return __first_[0];
409+
return static_cast<unsigned long long>(__first_[0]);
377410
}
378411

379412
template <size_t _N_words, size_t _Size>
@@ -383,7 +416,7 @@ __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
383416
_LIBCPP_DIAGNOSTIC_PUSH
384417
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
385418
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;
419+
const size_t __n_words = _N_words < __ull_words ? _N_words : __ull_words;
387420
for (size_t __i = 1; __i < __n_words; ++__i)
388421
__r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT * __i);
389422
_LIBCPP_DIAGNOSTIC_POP
@@ -494,8 +527,7 @@ inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset() _NOEXCEPT : __first_(0)
494527

495528
template <size_t _Size>
496529
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)) {}
530+
: __first_(static_cast<__storage_type>(__v)) {}
499531

500532
template <size_t _Size>
501533
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
@@ -524,12 +556,12 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<1, _Siz
524556

525557
template <size_t _Size>
526558
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _Size>::to_ulong() const {
527-
return __first_;
559+
return static_cast<unsigned long>(__first_);
528560
}
529561

530562
template <size_t _Size>
531563
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __bitset<1, _Size>::to_ullong() const {
532-
return __first_;
564+
return static_cast<unsigned long long>(__first_);
533565
}
534566

535567
template <size_t _Size>
@@ -595,8 +627,8 @@ protected:
595627

596628
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT {}
597629

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

601633
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { return true; }
602634
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT { return false; }
@@ -626,7 +658,8 @@ public:
626658

627659
// 23.3.5.1 constructors:
628660
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
629-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT : __base(__v) {}
661+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT
662+
: __base(sizeof(unsigned long long) * CHAR_BIT <= _Size ? __v : __v & ((1ULL << _Size) - 1)) {}
630663
template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>
631664
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
632665
const _CharT* __str,

0 commit comments

Comments
 (0)