Skip to content

Commit 51ae219

Browse files
committed
Fix preprocessing
1 parent 6ed6853 commit 51ae219

File tree

1 file changed

+63
-31
lines changed

1 file changed

+63
-31
lines changed

libcxx/include/bitset

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,10 @@ template <size_t N> struct hash<std::bitset<N>>;
144144
# include <__cstddef/size_t.h>
145145
# include <__functional/hash.h>
146146
# include <__functional/unary_function.h>
147+
# include <__type_traits/enable_if.h>
147148
# include <__type_traits/integral_constant.h>
148149
# include <__type_traits/is_char_like_type.h>
150+
# include <__utility/integer_sequence.h>
149151
# include <climits>
150152
# include <stdexcept>
151153
# include <string_view>
@@ -220,10 +222,10 @@ protected:
220222

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

229231
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT;
@@ -243,6 +245,11 @@ private:
243245
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(true_type) const;
244246
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(true_type, false_type) const;
245247
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(true_type, true_type) const;
248+
# if _LIBCPP_STD_VER >= 14
249+
template <size_t... _Indices>
250+
_LIBCPP_HIDE_FROM_ABI constexpr __bitset(unsigned long long __v, std::index_sequence<_Indices...>) _NOEXCEPT
251+
: __first_{static_cast<__storage_type>(__v >> (_Indices * __bits_per_word))...} {}
252+
# endif
246253
};
247254

248255
template <size_t _N_words, size_t _Size>
@@ -284,35 +291,47 @@ inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned lon
284291

285292
# endif // _LIBCPP_CXX03_LANG
286293

294+
# ifdef _LIBCPP_CXX03_LANG
295+
template <size_t _N_words, size_t _Size>
296+
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT {
297+
__init(__v, _BoolConstant<sizeof(unsigned long long) == sizeof(__storage_type)>());
298+
}
299+
# elif _LIBCPP_STD_VER >= 14
287300
template <size_t _N_words, size_t _Size>
288301
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
289-
# ifndef _LIBCPP_CXX03_LANG
302+
: __bitset{__v,
303+
std::make_index_sequence<
304+
std::min<size_t>(_N_words, (sizeof(unsigned long long) - 1) / sizeof(__storage_type) + 1)>{}} {}
305+
# else
290306
# if __SIZEOF_LONG_LONG__ <= __SIZEOF_SIZE_T__
291-
: __first_{static_cast<__storage_type>(__v)}
292-
# elif __SIZEOF_LONG_LONG__ <= 2 * __SIZEOF_SIZE_T__
293-
: __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
294-
# elif __SIZEOF_LONG_LONG__ <= 4 * __SIZEOF_SIZE_T__
295-
# if _N_words == 2
296-
: __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
297-
# elif _N_words == 3
307+
template <size_t _N_words, size_t _Size>
308+
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
309+
: __first_{static_cast<__storage_type>(__v)} {}
310+
# elif __SIZEOF_LONG_LONG__ == 2 * __SIZEOF_SIZE_T__
311+
template <size_t _N_words, size_t _Size>
312+
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
313+
: __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)} {}
314+
# elif __SIZEOF_LONG_LONG__ == 4 * __SIZEOF_SIZE_T__
315+
template <size_t _N_words, size_t _Size, __enable_if_t<_N_words == 2, int> = 0>
316+
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
317+
: __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)} {}
318+
319+
template <size_t _N_words, size_t _Size, __enable_if_t<_N_words == 3, int> = 0>
320+
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
298321
: __first_{static_cast<__storage_type>(__v),
299322
static_cast<__storage_type>(__v >> __bits_per_word),
300-
static_cast<__storage_type>(__v >> (__bits_per_word * 2))}
301-
# else
323+
static_cast<__storage_type>(__v >> (__bits_per_word * 2))} {}
324+
325+
template <size_t _N_words, size_t _Size, __enable_if_t<_N_words >= 4, int> = 0>
326+
inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
302327
: __first_{static_cast<__storage_type>(__v),
303328
static_cast<__storage_type>(__v >> __bits_per_word),
304329
static_cast<__storage_type>(__v >> (__bits_per_word * 2)),
305-
static_cast<__storage_type>(__v >> (__bits_per_word * 3))}
306-
# endif
330+
static_cast<__storage_type>(__v >> (__bits_per_word * 3))} {}
307331
# else
308332
# error This constructor has not been ported to this platform
309333
# endif
310-
# endif
311-
{
312-
# ifdef _LIBCPP_CXX03_LANG
313-
__init(__v, _BoolConstant<sizeof(unsigned long long) == sizeof(__storage_type)>());
314-
# endif
315-
}
334+
# endif // _LIBCPP_CXX03_LANG
316335

317336
template <size_t _N_words, size_t _Size>
318337
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
@@ -373,12 +392,11 @@ __bitset<_N_words, _Size>::__to_ulong(true_type, false_type) const {
373392
template <size_t _N_words, size_t _Size>
374393
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
375394
__bitset<_N_words, _Size>::__to_ulong(true_type, true_type) const {
376-
unsigned long __r = static_cast<unsigned long>(__first_[0]);
377-
_LIBCPP_DIAGNOSTIC_PUSH
378-
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
379-
for (size_t __i = 1; __i < _N_words; ++__i)
395+
const size_t __ul_wrods = (sizeof(unsigned long) - 1) / sizeof(__storage_type) + 1;
396+
const size_t __n_words = _N_words < __ul_wrods ? _N_words : __ul_wrods;
397+
unsigned long __r = static_cast<unsigned long>(__first_[0]);
398+
for (size_t __i = 1; __i < __n_words; ++__i)
380399
__r |= static_cast<unsigned long>(__first_[__i]) << (__bits_per_word * __i);
381-
_LIBCPP_DIAGNOSTIC_POP
382400
return __r;
383401
}
384402

@@ -406,14 +424,11 @@ __bitset<_N_words, _Size>::__to_ullong(true_type, false_type) const {
406424
template <size_t _N_words, size_t _Size>
407425
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
408426
__bitset<_N_words, _Size>::__to_ullong(true_type, true_type) const {
409-
unsigned long long __r = static_cast<unsigned long long>(__first_[0]);
410-
_LIBCPP_DIAGNOSTIC_PUSH
411-
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
412427
const size_t __ull_wrods = (sizeof(unsigned long long) - 1) / sizeof(__storage_type) + 1;
413428
const size_t __n_words = _N_words < __ull_wrods ? _N_words : __ull_wrods;
429+
unsigned long long __r = static_cast<unsigned long long>(__first_[0]);
414430
for (size_t __i = 1; __i < __n_words; ++__i)
415431
__r |= static_cast<unsigned long long>(__first_[__i]) << (__bits_per_word * __i);
416-
_LIBCPP_DIAGNOSTIC_POP
417432
return __r;
418433
}
419434

@@ -520,14 +535,16 @@ protected:
520535
private:
521536
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong(false_type) const;
522537
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong(true_type) const;
538+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(false_type) const;
539+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(true_type) const;
523540
};
524541

525542
template <size_t _Size>
526543
inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset() _NOEXCEPT : __first_(0) {}
527544

528545
template <size_t _Size>
529546
inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
530-
: __first_(_Size == __bits_per_word ? static_cast<__storage_type>(__v) : static_cast<__storage_type>(__v)) {}
547+
: __first_(static_cast<__storage_type>(__v)) {}
531548

532549
template <size_t _Size>
533550
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
@@ -554,7 +571,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<1, _Siz
554571

555572
template <size_t _Size>
556573
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _Size>::to_ulong() const {
557-
return __to_ulong(_BoolConstant < _Size< sizeof(unsigned long) * CHAR_BIT>());
574+
return __to_ulong(_BoolConstant<_Size <= sizeof(unsigned long) * CHAR_BIT>());
558575
}
559576

560577
template <size_t _Size>
@@ -572,6 +589,21 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _S
572589

573590
template <size_t _Size>
574591
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __bitset<1, _Size>::to_ullong() const {
592+
return __to_ullong(_BoolConstant<_Size <= sizeof(unsigned long long) * CHAR_BIT>());
593+
}
594+
595+
template <size_t _Size>
596+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
597+
__bitset<1, _Size>::__to_ullong(false_type) const {
598+
if (auto __e = __make_iter(_Size); std::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true) != __e)
599+
__throw_overflow_error("__bitset<1, _Size>::__to_ullong overflow error");
600+
601+
return static_cast<unsigned long long>(__first_);
602+
}
603+
604+
template <size_t _Size>
605+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
606+
__bitset<1, _Size>::__to_ullong(true_type) const {
575607
return static_cast<unsigned long long>(__first_);
576608
}
577609

0 commit comments

Comments
 (0)