Skip to content

Commit bcd553d

Browse files
committed
Remove unnecessary static_casts
1 parent 4ec181e commit bcd553d

File tree

5 files changed

+96
-87
lines changed

5 files changed

+96
-87
lines changed

libcxx/include/__bit/countr.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ template <class _Tp>
4646
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero_impl(_Tp __t) _NOEXCEPT {
4747
_LIBCPP_ASSERT_INTERNAL(__t != 0, "__countr_zero_impl called with zero value");
4848
static_assert(is_unsigned<_Tp>::value, "__countr_zero_impl only works with unsigned types");
49-
// Use constexpr if as a C++17 extension for clang
5049
if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned int)) {
5150
return std::__libcpp_ctz(static_cast<unsigned int>(__t));
5251
} else if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned long)) {
@@ -55,7 +54,6 @@ template <class _Tp>
5554
return std::__libcpp_ctz(static_cast<unsigned long long>(__t));
5655
} else {
5756
#if _LIBCPP_STD_VER == 11
58-
// A constexpr implementation for C++11 using variable declaration as a C++14 extension for clang
5957
unsigned long long __ull = static_cast<unsigned long long>(__t);
6058
const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
6159
return __ull == 0ull ? __ulldigits + std::__countr_zero_impl<_Tp>(__t >> __ulldigits) : std::__libcpp_ctz(__ull);
@@ -74,7 +72,7 @@ template <class _Tp>
7472
template <class _Tp>
7573
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero(_Tp __t) _NOEXCEPT {
7674
static_assert(is_unsigned<_Tp>::value, "__countr_zero only works with unsigned types");
77-
#if __has_builtin(__builtin_ctzg)
75+
#if __has_builtin(__builtin_ctzg) // TODO (LLVM 21): This can be dropped once we only support Clang >= 19.
7876
return __builtin_ctzg(__t, numeric_limits<_Tp>::digits);
7977
#else
8078
return __t != 0 ? std::__countr_zero_impl(__t) : numeric_limits<_Tp>::digits;

libcxx/include/__bit_reference

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,29 @@ struct __size_difference_type_traits<_Cp, __void_t<typename _Cp::difference_type
6868
};
6969

7070
// The `__x_mask` functions are designed to work exclusively with any unsigned `_StorageType`s, including small
71-
// integral types such as unsigned char/short, `uint8_t`, and `uint16_t`. To prevent undefined behaviors or
72-
// ambiguities due to integral promotions for the small integral types, all bitwise operations are explicitly
73-
// cast back to the unsigned `_StorageType`.
71+
// integral types such as unsigned char/short, `uint8_t`, and `uint16_t`. To prevent undefined behavior or
72+
// ambiguities due to integral promotions for the small integral types, all intermediate bitwise operations are
73+
// explicitly cast back to the unsigned `_StorageType`.
7474

7575
// Creates a mask of type `_StorageType` with a specified number of leading zeros (__clz) and sets all remaining
76-
// bits to one
76+
// bits to one.
7777
template <class _StorageType>
7878
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __trailing_mask(unsigned __clz) {
7979
static_assert(is_unsigned<_StorageType>::value, "__trailing_mask only works with unsigned types");
80-
return static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz);
80+
return static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz;
8181
}
8282

8383
// Creates a mask of type `_StorageType` with a specified number of leading zeros (__clz), a specified number of
84-
// trailing zeros (__ctz), and sets all bits in between to one
84+
// trailing zeros (__ctz), and sets all bits in between to one.
8585
template <class _StorageType>
8686
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __middle_mask(unsigned __clz, unsigned __ctz) {
8787
static_assert(is_unsigned<_StorageType>::value, "__middle_mask only works with unsigned types");
88-
return static_cast<_StorageType>(
89-
static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz) &
90-
std::__trailing_mask<_StorageType>(__clz));
88+
return (static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz) &
89+
std::__trailing_mask<_StorageType>(__clz);
9190
}
9291

9392
// This function is designed to operate correctly even for smaller integral types like `uint8_t`, `uint16_t`,
94-
// or `unsigned short`. Casting back to _StorageType is crucial to prevent undefined behavior that can arise
95-
// from integral promotions.
93+
// or `unsigned short`.
9694
// See https://github.com/llvm/llvm-project/pull/122410.
9795
template <class _StoragePointer>
9896
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
@@ -102,12 +100,11 @@ __fill_masked_range(_StoragePointer __word, unsigned __clz, unsigned __ctz, bool
102100
using _StorageType = typename pointer_traits<_StoragePointer>::element_type;
103101
_LIBCPP_ASSERT_VALID_INPUT_RANGE(
104102
__ctz + __clz < sizeof(_StorageType) * CHAR_BIT, "__fill_masked_range called with invalid range");
105-
_StorageType __m = static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz) &
106-
static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz);
103+
_StorageType __m = std::__middle_mask<_StorageType>(__clz, __ctz);
107104
if (__fill_val)
108105
*__word |= __m;
109106
else
110-
*__word &= static_cast<_StorageType>(~__m);
107+
*__word &= ~__m;
111108
}
112109

113110
template <class _Cp, bool = __has_storage_type<_Cp>::value>

libcxx/test/std/algorithms/alg.nonmodifying/alg.find/find.pass.cpp

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
220220
Test<TriviallyComparable<wchar_t>, TriviallyComparable<wchar_t>>().operator()<TriviallyComparable<wchar_t>*>();
221221
#endif
222222

223-
// TODO: Remove the `_LIBCPP_ENABLE_EXPERIMENTAL` check once we have the FTM guarded or views::join isn't
224-
// experimental anymore
225-
#if TEST_STD_VER >= 20 && (!defined(_LIBCPP_VERSION) || defined(_LIBCPP_ENABLE_EXPERIMENTAL))
223+
#if TEST_STD_VER >= 20
226224
{
227225
std::vector<std::vector<int>> vec = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
228226
auto view = vec | std::views::join;
@@ -232,34 +230,8 @@ TEST_CONSTEXPR_CXX20 bool test() {
232230

233231
types::for_each(types::integral_types(), TestIntegerPromotions());
234232

235-
// Verify that the std::vector<bool>::iterator optimization works properly for allocators with custom size types
236-
// See https://github.com/llvm/llvm-project/issues/122528
237-
{
238-
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
239-
std::vector<bool, Alloc> in(100, false, Alloc(1));
240-
in[in.size() - 2] = true;
241-
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
242-
}
243-
{
244-
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
245-
std::vector<bool, Alloc> in(199, false, Alloc(1));
246-
in[in.size() - 2] = true;
247-
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
248-
}
249-
{
250-
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
251-
std::vector<bool, Alloc> in(200, false, Alloc(1));
252-
in[in.size() - 2] = true;
253-
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
254-
}
255233
{
256-
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
257-
std::vector<bool, Alloc> in(257, false, Alloc(1));
258-
in[in.size() - 2] = true;
259-
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
260-
}
261-
262-
{ // Test vector<bool>::iterator optimization
234+
// Test vector<bool>::iterator optimization
263235
std::vector<bool> vec(256 + 8);
264236
for (ptrdiff_t i = 8; i <= 256; i *= 2) {
265237
for (size_t offset = 0; offset < 8; offset += 2) {
@@ -269,6 +241,39 @@ TEST_CONSTEXPR_CXX20 bool test() {
269241
assert(std::find(vec.begin() + offset, vec.end(), false) == vec.begin() + offset + i);
270242
}
271243
}
244+
245+
// Verify that the std::vector<bool>::iterator optimization works properly for allocators with custom size types
246+
// Fix https://github.com/llvm/llvm-project/issues/122528
247+
{
248+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
249+
std::vector<bool, Alloc> in(100, false, Alloc(1));
250+
in[in.size() - 2] = true;
251+
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
252+
}
253+
{
254+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
255+
std::vector<bool, Alloc> in(199, false, Alloc(1));
256+
in[in.size() - 2] = true;
257+
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
258+
}
259+
{
260+
using Alloc = sized_allocator<bool, unsigned short, short>;
261+
std::vector<bool, Alloc> in(200, false, Alloc(1));
262+
in[in.size() - 2] = true;
263+
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
264+
}
265+
{
266+
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
267+
std::vector<bool, Alloc> in(205, false, Alloc(1));
268+
in[in.size() - 2] = true;
269+
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
270+
}
271+
{
272+
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
273+
std::vector<bool, Alloc> in(257, false, Alloc(1));
274+
in[in.size() - 2] = true;
275+
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
276+
}
272277
}
273278

274279
return true;

libcxx/test/std/algorithms/alg.nonmodifying/alg.find/ranges.find.pass.cpp

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ constexpr bool test() {
203203
}
204204
}
205205

206-
{ // Test vector<bool>::iterator optimization
206+
{
207+
// Test vector<bool>::iterator optimization
207208
std::vector<bool> vec(256 + 8);
208209
for (ptrdiff_t i = 8; i <= 256; i *= 2) {
209210
for (size_t offset = 0; offset < 8; offset += 2) {
@@ -216,33 +217,39 @@ constexpr bool test() {
216217
std::ranges::begin(vec) + offset + i);
217218
}
218219
}
219-
}
220220

221-
// Verify that the std::vector<bool>::iterator optimization works properly for allocators with custom size types
222-
// See https://github.com/llvm/llvm-project/issues/122528
223-
{
224-
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
225-
std::vector<bool, Alloc> in(100, false, Alloc(1));
226-
in[in.size() - 2] = true;
227-
assert(std::ranges::find(in, true) == in.end() - 2);
228-
}
229-
{
230-
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
231-
std::vector<bool, Alloc> in(199, false, Alloc(1));
232-
in[in.size() - 2] = true;
233-
assert(std::ranges::find(in, true) == in.end() - 2);
234-
}
235-
{
236-
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
237-
std::vector<bool, Alloc> in(200, false, Alloc(1));
238-
in[in.size() - 2] = true;
239-
assert(std::ranges::find(in, true) == in.end() - 2);
240-
}
241-
{
242-
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
243-
std::vector<bool, Alloc> in(257, false, Alloc(1));
244-
in[in.size() - 2] = true;
245-
assert(std::ranges::find(in, true) == in.end() - 2);
221+
// Verify that the std::vector<bool>::iterator optimization works properly for allocators with custom size types
222+
// See https://github.com/llvm/llvm-project/issues/122528
223+
{
224+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
225+
std::vector<bool, Alloc> in(100, false, Alloc(1));
226+
in[in.size() - 2] = true;
227+
assert(std::ranges::find(in, true) == in.end() - 2);
228+
}
229+
{
230+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
231+
std::vector<bool, Alloc> in(199, false, Alloc(1));
232+
in[in.size() - 2] = true;
233+
assert(std::ranges::find(in, true) == in.end() - 2);
234+
}
235+
{
236+
using Alloc = sized_allocator<bool, unsigned short, short>;
237+
std::vector<bool, Alloc> in(200, false, Alloc(1));
238+
in[in.size() - 2] = true;
239+
assert(std::ranges::find(in, true) == in.end() - 2);
240+
}
241+
{
242+
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
243+
std::vector<bool, Alloc> in(205, false, Alloc(1));
244+
in[in.size() - 2] = true;
245+
assert(std::ranges::find(in, true) == in.end() - 2);
246+
}
247+
{
248+
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
249+
std::vector<bool, Alloc> in(257, false, Alloc(1));
250+
in[in.size() - 2] = true;
251+
assert(std::ranges::find(in, true) == in.end() - 2);
252+
}
246253
}
247254

248255
return true;

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

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

9+
// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=15000000
10+
911
// bitset<N>& operator<<=(size_t pos); // constexpr since C++23
1012

1113
#include <bitset>
@@ -18,20 +20,20 @@
1820

1921
template <std::size_t N>
2022
TEST_CONSTEXPR_CXX23 bool test_left_shift() {
21-
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
22-
for (std::size_t c = 0; c != cases.size(); ++c) {
23-
for (std::size_t s = 0; s <= N+1; ++s) {
24-
std::bitset<N> v1 = cases[c];
25-
std::bitset<N> v2 = v1;
26-
v1 <<= s;
27-
for (std::size_t i = 0; i < v1.size(); ++i)
28-
if (i < s)
29-
assert(v1[i] == 0);
30-
else
31-
assert(v1[i] == v2[i-s]);
32-
}
23+
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
24+
for (std::size_t c = 0; c != cases.size(); ++c) {
25+
for (std::size_t s = 0; s <= N + 1; ++s) {
26+
std::bitset<N> v1 = cases[c];
27+
std::bitset<N> v2 = v1;
28+
v1 <<= s;
29+
for (std::size_t i = 0; i < v1.size(); ++i)
30+
if (i < s)
31+
assert(v1[i] == 0);
32+
else
33+
assert(v1[i] == v2[i - s]);
3334
}
34-
return true;
35+
}
36+
return true;
3537
}
3638

3739
int main(int, char**) {

0 commit comments

Comments
 (0)