Skip to content

Commit 668d024

Browse files
committed
Fix preprocessing
1 parent 5b350a3 commit 668d024

File tree

1 file changed

+62
-30
lines changed

1 file changed

+62
-30
lines changed

libcxx/include/bitset

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ template <size_t N> struct hash<std::bitset<N>>;
147147
# include <__functional/hash.h>
148148
# include <__functional/identity.h>
149149
# include <__functional/unary_function.h>
150+
# include <__type_traits/enable_if.h>
150151
# include <__type_traits/integral_constant.h>
151152
# include <__type_traits/is_char_like_type.h>
153+
# include <__utility/integer_sequence.h>
152154
# include <climits>
153155
# include <stdexcept>
154156
# include <string_view>
@@ -223,10 +225,10 @@ protected:
223225

224226
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
225227
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
226-
return __to_ulong(_BoolConstant<_Size < sizeof(unsigned long) * CHAR_BIT>());
228+
return __to_ulong(_BoolConstant<_Size <= sizeof(unsigned long) * CHAR_BIT>());
227229
}
228230
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {
229-
return __to_ullong(_BoolConstant<_Size < sizeof(unsigned long long) * CHAR_BIT>());
231+
return __to_ullong(_BoolConstant<_Size <= sizeof(unsigned long long) * CHAR_BIT>());
230232
}
231233

232234
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { return !__scan_bits(__bit_not()); }
@@ -287,6 +289,11 @@ private:
287289
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(true_type) const;
288290
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(true_type, false_type) const;
289291
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(true_type, true_type) const;
292+
# if _LIBCPP_STD_VER >= 14
293+
template <size_t... _Indices>
294+
_LIBCPP_HIDE_FROM_ABI constexpr __bitset(unsigned long long __v, std::index_sequence<_Indices...>) _NOEXCEPT
295+
: __first_{static_cast<__storage_type>(__v >> (_Indices * __bits_per_word))...} {}
296+
# endif
290297
};
291298

292299
template <size_t _N_words, size_t _Size>
@@ -318,35 +325,47 @@ inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned lon
318325

319326
# endif // _LIBCPP_CXX03_LANG
320327

328+
# ifdef _LIBCPP_CXX03_LANG
329+
template <size_t _N_words, size_t _Size>
330+
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT {
331+
__init(__v, _BoolConstant<sizeof(unsigned long long) == sizeof(__storage_type)>());
332+
}
333+
# elif _LIBCPP_STD_VER >= 14
321334
template <size_t _N_words, size_t _Size>
322335
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
323-
# ifndef _LIBCPP_CXX03_LANG
336+
: __bitset{__v,
337+
std::make_index_sequence<
338+
std::min<size_t>(_N_words, (sizeof(unsigned long long) - 1) / sizeof(__storage_type) + 1)>{}} {}
339+
# else
324340
# if __SIZEOF_LONG_LONG__ <= __SIZEOF_SIZE_T__
325-
: __first_{static_cast<__storage_type>(__v)}
326-
# elif __SIZEOF_LONG_LONG__ <= 2 * __SIZEOF_SIZE_T__
327-
: __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
328-
# elif __SIZEOF_LONG_LONG__ <= 4 * __SIZEOF_SIZE_T__
329-
# if _N_words == 2
330-
: __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
331-
# elif _N_words == 3
341+
template <size_t _N_words, size_t _Size>
342+
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
343+
: __first_{static_cast<__storage_type>(__v)} {}
344+
# elif __SIZEOF_LONG_LONG__ == 2 * __SIZEOF_SIZE_T__
345+
template <size_t _N_words, size_t _Size>
346+
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
347+
: __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)} {}
348+
# elif __SIZEOF_LONG_LONG__ == 4 * __SIZEOF_SIZE_T__
349+
template <size_t _N_words, size_t _Size, __enable_if_t<_N_words == 2, int> = 0>
350+
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
351+
: __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)} {}
352+
353+
template <size_t _N_words, size_t _Size, __enable_if_t<_N_words == 3, int> = 0>
354+
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
332355
: __first_{static_cast<__storage_type>(__v),
333356
static_cast<__storage_type>(__v >> __bits_per_word),
334-
static_cast<__storage_type>(__v >> (__bits_per_word * 2))}
335-
# else
357+
static_cast<__storage_type>(__v >> (__bits_per_word * 2))} {}
358+
359+
template <size_t _N_words, size_t _Size, __enable_if_t<_N_words >= 4, int> = 0>
360+
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
336361
: __first_{static_cast<__storage_type>(__v),
337362
static_cast<__storage_type>(__v >> __bits_per_word),
338363
static_cast<__storage_type>(__v >> (__bits_per_word * 2)),
339-
static_cast<__storage_type>(__v >> (__bits_per_word * 3))}
340-
# endif
364+
static_cast<__storage_type>(__v >> (__bits_per_word * 3))} {}
341365
# else
342366
# error This constructor has not been ported to this platform
343367
# endif
344-
# endif
345-
{
346-
# ifdef _LIBCPP_CXX03_LANG
347-
__init(__v, _BoolConstant<sizeof(unsigned long long) <= sizeof(__storage_type)>());
348-
# endif
349-
}
368+
# endif // _LIBCPP_CXX03_LANG
350369

351370
template <size_t _N_words, size_t _Size>
352371
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
@@ -407,12 +426,11 @@ __bitset<_N_words, _Size>::__to_ulong(true_type, false_type) const {
407426
template <size_t _N_words, size_t _Size>
408427
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
409428
__bitset<_N_words, _Size>::__to_ulong(true_type, true_type) const {
410-
unsigned long __r = static_cast<unsigned long>(__first_[0]);
411-
_LIBCPP_DIAGNOSTIC_PUSH
412-
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
413-
for (size_t __i = 1; __i < _N_words; ++__i)
429+
const size_t __ul_wrods = (sizeof(unsigned long) - 1) / sizeof(__storage_type) + 1;
430+
const size_t __n_words = _N_words < __ul_wrods ? _N_words : __ul_wrods;
431+
unsigned long __r = static_cast<unsigned long>(__first_[0]);
432+
for (size_t __i = 1; __i < __n_words; ++__i)
414433
__r |= static_cast<unsigned long>(__first_[__i]) << (__bits_per_word * __i);
415-
_LIBCPP_DIAGNOSTIC_POP
416434
return __r;
417435
}
418436

@@ -440,14 +458,11 @@ __bitset<_N_words, _Size>::__to_ullong(true_type, false_type) const {
440458
template <size_t _N_words, size_t _Size>
441459
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
442460
__bitset<_N_words, _Size>::__to_ullong(true_type, true_type) const {
443-
unsigned long long __r = static_cast<unsigned long long>(__first_[0]);
444-
_LIBCPP_DIAGNOSTIC_PUSH
445-
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
446461
const size_t __ull_wrods = (sizeof(unsigned long long) - 1) / sizeof(__storage_type) + 1;
447462
const size_t __n_words = _N_words < __ull_wrods ? _N_words : __ull_wrods;
463+
unsigned long long __r = static_cast<unsigned long long>(__first_[0]);
448464
for (size_t __i = 1; __i < __n_words; ++__i)
449465
__r |= static_cast<unsigned long long>(__first_[__i]) << (__bits_per_word * __i);
450-
_LIBCPP_DIAGNOSTIC_POP
451466
return __r;
452467
}
453468

@@ -534,6 +549,8 @@ protected:
534549
private:
535550
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong(false_type) const;
536551
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong(true_type) const;
552+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(false_type) const;
553+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(true_type) const;
537554
};
538555

539556
template <size_t _Size>
@@ -568,7 +585,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<1, _Siz
568585

569586
template <size_t _Size>
570587
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _Size>::to_ulong() const {
571-
return __to_ulong(_BoolConstant < _Size< sizeof(unsigned long) * CHAR_BIT>());
588+
return __to_ulong(_BoolConstant<_Size <= sizeof(unsigned long) * CHAR_BIT>());
572589
}
573590

574591
template <size_t _Size>
@@ -586,6 +603,21 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _S
586603

587604
template <size_t _Size>
588605
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __bitset<1, _Size>::to_ullong() const {
606+
return __to_ullong(_BoolConstant<_Size <= sizeof(unsigned long long) * CHAR_BIT>());
607+
}
608+
609+
template <size_t _Size>
610+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
611+
__bitset<1, _Size>::__to_ullong(false_type) const {
612+
if (auto __e = __make_iter(_Size); std::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true) != __e)
613+
__throw_overflow_error("__bitset<1, _Size>::__to_ullong overflow error");
614+
615+
return static_cast<unsigned long long>(__first_);
616+
}
617+
618+
template <size_t _Size>
619+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
620+
__bitset<1, _Size>::__to_ullong(true_type) const {
589621
return static_cast<unsigned long long>(__first_);
590622
}
591623

0 commit comments

Comments
 (0)