Skip to content

Commit c64b35e

Browse files
committed
Fix preprocessing
1 parent 6ed6853 commit c64b35e

File tree

1 file changed

+57
-21
lines changed

1 file changed

+57
-21
lines changed

libcxx/include/bitset

Lines changed: 57 additions & 21 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+
inline 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
@@ -520,6 +539,8 @@ protected:
520539
private:
521540
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong(false_type) const;
522541
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong(true_type) const;
542+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(false_type) const;
543+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(true_type) const;
523544
};
524545

525546
template <size_t _Size>
@@ -554,7 +575,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<1, _Siz
554575

555576
template <size_t _Size>
556577
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _Size>::to_ulong() const {
557-
return __to_ulong(_BoolConstant < _Size< sizeof(unsigned long) * CHAR_BIT>());
578+
return __to_ulong(_BoolConstant<_Size <= sizeof(unsigned long) * CHAR_BIT>());
558579
}
559580

560581
template <size_t _Size>
@@ -572,6 +593,21 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _S
572593

573594
template <size_t _Size>
574595
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __bitset<1, _Size>::to_ullong() const {
596+
return __to_ullong(_BoolConstant<_Size <= sizeof(unsigned long long) * CHAR_BIT>());
597+
}
598+
599+
template <size_t _Size>
600+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
601+
__bitset<1, _Size>::__to_ullong(false_type) const {
602+
if (auto __e = __make_iter(_Size); std::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true) != __e)
603+
__throw_overflow_error("__bitset<1, _Size>::__to_ullong overflow error");
604+
605+
return static_cast<unsigned long long>(__first_);
606+
}
607+
608+
template <size_t _Size>
609+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
610+
__bitset<1, _Size>::__to_ullong(true_type) const {
575611
return static_cast<unsigned long long>(__first_);
576612
}
577613

0 commit comments

Comments
 (0)