Skip to content

Commit 958a1c3

Browse files
committed
Address philnik777's comments
1 parent 4586d69 commit 958a1c3

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

libcxx/include/bitset

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public:
8080
constexpr bool operator[](size_t pos) const;
8181
reference operator[](size_t pos); // constexpr since C++23
8282
unsigned long to_ulong() const; // constexpr since C++23
83-
unsigned long long to_ullong() const; // since C++11, constexpr since C++23
83+
unsigned long long to_ullong() const; // constexpr since C++23
8484
template <class charT, class traits, class Allocator> // constexpr since C++23
8585
basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;
8686
template <class charT, class traits> // constexpr since C++23
@@ -226,30 +226,18 @@ protected:
226226

227227
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
228228

229-
// unsigned long spans only one word
230-
template <typename _StorageType = __storage_type,
231-
__enable_if_t<sizeof(_StorageType) >= sizeof(unsigned long), int> = 0>
232229
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong() const {
230+
static_assert(sizeof(__storage_type) >= sizeof(unsigned long),
231+
"bitset only supports platforms where sizeof(size_t) >= sizeof(unsigned long), such as 32-bit and "
232+
"64-bit platforms");
233233
return static_cast<unsigned long>(__first_[0]);
234234
}
235235

236-
// unsigned long may span multiple words which are concatenated to form the result
237-
template <typename _StorageType = __storage_type,
238-
__enable_if_t<sizeof(_StorageType) < sizeof(unsigned long), int> = 0>
239-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong() const {
240-
const size_t __ul_words = (sizeof(unsigned long) - 1) / sizeof(__storage_type) + 1;
241-
const size_t __n_words = _N_words < __ul_words ? _N_words : __ul_words;
242-
unsigned long __r = static_cast<unsigned long>(__first_[0]);
243-
for (size_t __i = 1; __i < __n_words; ++__i)
244-
__r |= static_cast<unsigned long>(__first_[__i]) << (__bits_per_word * __i);
245-
return __r;
246-
}
247-
248236
// _Bit_size > sizeof(unsigned long) * CHAR_BIT: overflow check needed
249237
template <size_t _Bit_size = _Size, __enable_if_t<(_Bit_size > sizeof(unsigned long) * CHAR_BIT), int> = 0>
250238
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
251239
if (auto __e = __make_iter(_Bit_size); std::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true) != __e)
252-
std::__throw_overflow_error("__bitset<_N_words, _Size>::__to_ulong overflow error");
240+
std::__throw_overflow_error("__bitset<_N_words, _Size>::to_ulong overflow error");
253241

254242
return __to_ulong();
255243
}
@@ -265,7 +253,7 @@ protected:
265253
if _LIBCPP_CONSTEXPR (_Size > sizeof(unsigned long long) * CHAR_BIT) {
266254
if (auto __e = __make_iter(_Size);
267255
std::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true) != __e)
268-
std::__throw_overflow_error("__bitset<_N_words, _Size>::__to_ullong overflow error");
256+
std::__throw_overflow_error("__bitset<_N_words, _Size>::to_ullong overflow error");
269257
}
270258

271259
// At this point, the effective bitset size (excluding leading zeros) fits in unsigned long long
@@ -527,9 +515,9 @@ inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset() _NOEXCEPT : __first_(0)
527515

528516
template <size_t _Size>
529517
inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
530-
// TODO: This is a workaround for a gdb test failure (gdb_pretty_printer_test.sh.cpp) in
531-
// stage1 CI (generic-gcc, gcc-14, g++-14), due to the __bits_per_word name lookup failure
532-
// if not referenced in the constructor initializer list.
518+
// TODO: We must refer to __bits_per_word in order to work around an issue with the GDB pretty-printers.
519+
// Without it, the pretty-printers complain about a missing __bits_per_word member. This needs to
520+
// be investigated further.
533521
// See: https://github.com/llvm/llvm-project/actions/runs/15071518915/job/42368867929?pr=121348#logs
534522
: __first_(_Size == __bits_per_word ? static_cast<__storage_type>(__v) : static_cast<__storage_type>(__v)) {}
535523

@@ -617,8 +605,8 @@ protected:
617605

618606
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT {}
619607

620-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const { return 0UL; }
621-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const { return 0ULL; }
608+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const { return 0; }
609+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const { return 0; }
622610

623611
template <bool _Sparse, class _CharT, class _Traits, class _Allocator>
624612
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>

libcxx/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: no-exceptions
10-
119
// unsigned long long to_ullong() const; // constexpr since C++23
1210

1311
#include <bitset>
@@ -59,6 +57,7 @@ TEST_CONSTEXPR_CXX23 bool test() {
5957
test_to_ullong<65>();
6058
test_to_ullong<1000>();
6159

60+
#ifndef TEST_HAS_NO_EXCEPTIONS
6261
if (!TEST_IS_CONSTANT_EVALUATED) {
6362
// bitset has true bits beyond the size of unsigned long long
6463
std::bitset<std::numeric_limits<unsigned long long>::digits + 1> q(0);
@@ -72,6 +71,7 @@ TEST_CONSTEXPR_CXX23 bool test() {
7271
assert(false);
7372
}
7473
}
74+
#endif // TEST_HAS_NO_EXCEPTIONS
7575

7676
return true;
7777
}

libcxx/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: no-exceptions
10-
119
// unsigned long to_ulong() const; // constexpr since C++23
1210

1311
#include <bitset>
@@ -59,6 +57,7 @@ TEST_CONSTEXPR_CXX23 bool test() {
5957
test_to_ulong<65>();
6058
test_to_ulong<1000>();
6159

60+
#ifndef TEST_HAS_NO_EXCEPTIONS
6261
if (!TEST_IS_CONSTANT_EVALUATED) {
6362
// bitset has true bits beyond the size of unsigned long
6463
std::bitset<std::numeric_limits<unsigned long>::digits + 1> q(0);
@@ -72,6 +71,7 @@ TEST_CONSTEXPR_CXX23 bool test() {
7271
assert(false);
7372
}
7473
}
74+
#endif // TEST_HAS_NO_EXCEPTIONS
7575

7676
return true;
7777
}

0 commit comments

Comments
 (0)