Skip to content

Commit 913aa1c

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

File tree

1 file changed

+60
-24
lines changed

1 file changed

+60
-24
lines changed

libcxx/include/bitset

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,14 @@ private:
231231
void __init(unsigned long long __v, false_type) _NOEXCEPT;
232232
_LIBCPP_HIDE_FROM_ABI void __init(unsigned long long __v, true_type) _NOEXCEPT;
233233
# endif // _LIBCPP_CXX03_LANG
234+
# if _LIBCPP_STD_VER == 11
235+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR void
236+
__initialize(unsigned long long __v, size_t __i, size_t __n_words) _NOEXCEPT;
237+
# endif // _LIBCPP_STD_VER == 11
234238
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(false_type) const;
235239
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(true_type) const;
240+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(true_type, false_type) const;
241+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(true_type, true_type) const;
236242
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(false_type) const;
237243
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type) const;
238244
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type, false_type) const;
@@ -278,23 +284,33 @@ inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned lon
278284

279285
# endif // _LIBCPP_CXX03_LANG
280286

287+
# if _LIBCPP_STD_VER == 11
288+
template <size_t _N_words, size_t _Size>
289+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR void
290+
__bitset<_N_words, _Size>::__initialize(unsigned long long __v, size_t __i, size_t __n_words) _NOEXCEPT {
291+
if (__i < __n_words) {
292+
__first_[__i] = static_cast<__storage_type>(__v >> __bits_per_word * __i);
293+
__initialize(__v, __i + 1, __n_words);
294+
}
295+
}
296+
# endif
297+
281298
template <size_t _N_words, size_t _Size>
282299
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
283300
# 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
301+
: __first_{static_cast<__storage_type>(__v)}
295302
# endif
296303
{
297-
# ifdef _LIBCPP_CXX03_LANG
304+
# ifndef _LIBCPP_CXX03_LANG
305+
const size_t __ull_words = sizeof(unsigned long long) / sizeof(__storage_type);
306+
const size_t __n_words = _N_words < __ull_words ? _N_words : __ull_words;
307+
# if _LIBCPP_STD_VER >= 14
308+
for (size_t __i = 1; __i < __n_words; ++__i)
309+
__first_[__i] = static_cast<__storage_type>(__v >> __bits_per_word * __i);
310+
# else
311+
__initialize(__v, 1, __n_words);
312+
# endif
313+
# else
298314
__init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
299315
# endif
300316
}
@@ -344,13 +360,33 @@ __bitset<_N_words, _Size>::to_ulong(false_type) const {
344360
if (__i != __e)
345361
__throw_overflow_error("bitset to_ulong overflow error");
346362

347-
return __first_[0];
363+
return to_ulong(true_type());
348364
}
349365

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

356392
template <size_t _N_words, size_t _Size>
@@ -373,7 +409,7 @@ __bitset<_N_words, _Size>::to_ullong(true_type) const {
373409
template <size_t _N_words, size_t _Size>
374410
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
375411
__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const {
376-
return __first_[0];
412+
return static_cast<unsigned long long>(__first_[0]);
377413
}
378414

379415
template <size_t _N_words, size_t _Size>
@@ -383,9 +419,9 @@ __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
383419
_LIBCPP_DIAGNOSTIC_PUSH
384420
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
385421
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;
422+
const size_t __n_words = _N_words < __ull_words ? _N_words : __ull_words;
387423
for (size_t __i = 1; __i < __n_words; ++__i)
388-
__r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT * __i);
424+
__r |= static_cast<unsigned long long>(__first_[__i]) << (__bits_per_word * __i);
389425
_LIBCPP_DIAGNOSTIC_POP
390426
return __r;
391427
}
@@ -494,8 +530,7 @@ inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset() _NOEXCEPT : __first_(0)
494530

495531
template <size_t _Size>
496532
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)) {}
533+
: __first_(static_cast<__storage_type>(__v)) {}
499534

500535
template <size_t _Size>
501536
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
@@ -524,12 +559,12 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<1, _Siz
524559

525560
template <size_t _Size>
526561
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _Size>::to_ulong() const {
527-
return __first_;
562+
return static_cast<unsigned long>(__first_);
528563
}
529564

530565
template <size_t _Size>
531566
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __bitset<1, _Size>::to_ullong() const {
532-
return __first_;
567+
return static_cast<unsigned long long>(__first_);
533568
}
534569

535570
template <size_t _Size>
@@ -595,8 +630,8 @@ protected:
595630

596631
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT {}
597632

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

601636
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { return true; }
602637
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT { return false; }
@@ -626,7 +661,8 @@ public:
626661

627662
// 23.3.5.1 constructors:
628663
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
629-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT : __base(__v) {}
664+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT
665+
: __base(sizeof(unsigned long long) * CHAR_BIT <= _Size ? __v : __v & (1ULL << _Size) - 1) {}
630666
template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>
631667
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
632668
const _CharT* __str,

0 commit comments

Comments
 (0)