Skip to content

Commit 33708cf

Browse files
committed
Remove unnecessary static_casts
1 parent 21cfdf7 commit 33708cf

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed

libcxx/include/__bit_reference

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,28 @@ struct __size_difference_type_traits<_Cp, __void_t<typename _Cp::difference_type
6464

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

7070
// Creates a mask of type `_StorageType` with a specified number of leading zeros (__clz) and sets all remaining
71-
// bits to one
71+
// bits to one.
7272
template <class _StorageType>
7373
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __trailing_mask(unsigned __clz) {
7474
static_assert(is_unsigned<_StorageType>::value, "__trailing_mask only works with unsigned types");
75-
return static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz);
75+
return static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz;
7676
}
7777

7878
// Creates a mask of type `_StorageType` with a specified number of leading zeros (__clz), a specified number of
79-
// trailing zeros (__ctz), and sets all bits in between to one
79+
// trailing zeros (__ctz), and sets all bits in between to one.
8080
template <class _StorageType>
8181
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __middle_mask(unsigned __clz, unsigned __ctz) {
8282
static_assert(is_unsigned<_StorageType>::value, "__middle_mask only works with unsigned types");
83-
return static_cast<_StorageType>(
84-
static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz) &
85-
std::__trailing_mask<_StorageType>(__clz));
83+
return (static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz) &
84+
std::__trailing_mask<_StorageType>(__clz);
8685
}
8786

8887
// This function is designed to operate correctly even for smaller integral types like `uint8_t`, `uint16_t`,
89-
// or `unsigned short`. Casting back to _StorageType is crucial to prevent undefined behavior that can arise
90-
// from integral promotions.
88+
// or `unsigned short`.
9189
// See https://github.com/llvm/llvm-project/pull/122410.
9290
template <class _StoragePointer>
9391
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
@@ -97,12 +95,11 @@ __fill_masked_range(_StoragePointer __word, unsigned __clz, unsigned __ctz, bool
9795
using _StorageType = typename pointer_traits<_StoragePointer>::element_type;
9896
_LIBCPP_ASSERT_VALID_INPUT_RANGE(
9997
__ctz + __clz < sizeof(_StorageType) * CHAR_BIT, "__fill_masked_range called with invalid range");
100-
_StorageType __m = static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz) &
101-
static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz);
98+
_StorageType __m = std::__middle_mask<_StorageType>(__clz, __ctz);
10299
if (__fill_val)
103100
*__word |= __m;
104101
else
105-
*__word &= static_cast<_StorageType>(~__m);
102+
*__word &= ~__m;
106103
}
107104

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

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
230230
types::for_each(types::integral_types(), TestIntegerPromotions());
231231

232232
// Verify that the std::vector<bool>::iterator optimization works properly for allocators with custom size types
233-
// See https://github.com/llvm/llvm-project/issues/122528
233+
// Fix https://github.com/llvm/llvm-project/issues/122528
234234
{
235235
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
236236
std::vector<bool, Alloc> in(100, false, Alloc(1));
@@ -244,11 +244,17 @@ TEST_CONSTEXPR_CXX20 bool test() {
244244
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
245245
}
246246
{
247-
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
247+
using Alloc = sized_allocator<bool, unsigned short, short>;
248248
std::vector<bool, Alloc> in(200, false, Alloc(1));
249249
in[in.size() - 2] = true;
250250
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
251251
}
252+
{
253+
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
254+
std::vector<bool, Alloc> in(205, false, Alloc(1));
255+
in[in.size() - 2] = true;
256+
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
257+
}
252258
{
253259
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
254260
std::vector<bool, Alloc> in(257, false, Alloc(1));

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,17 @@ constexpr bool test() {
215215
assert(std::ranges::find(in, true) == in.end() - 2);
216216
}
217217
{
218-
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
218+
using Alloc = sized_allocator<bool, unsigned short, short>;
219219
std::vector<bool, Alloc> in(200, false, Alloc(1));
220220
in[in.size() - 2] = true;
221221
assert(std::ranges::find(in, true) == in.end() - 2);
222222
}
223+
{
224+
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
225+
std::vector<bool, Alloc> in(205, false, Alloc(1));
226+
in[in.size() - 2] = true;
227+
assert(std::ranges::find(in, true) == in.end() - 2);
228+
}
223229
{
224230
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
225231
std::vector<bool, Alloc> in(257, false, Alloc(1));

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

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

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

1112
#include <bitset>
@@ -18,20 +19,20 @@
1819

1920
template <std::size_t N>
2021
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-
}
22+
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
23+
for (std::size_t c = 0; c != cases.size(); ++c) {
24+
for (std::size_t s = 0; s <= N + 1; ++s) {
25+
std::bitset<N> v1 = cases[c];
26+
std::bitset<N> v2 = v1;
27+
v1 <<= s;
28+
for (std::size_t i = 0; i < v1.size(); ++i)
29+
if (i < s)
30+
assert(v1[i] == 0);
31+
else
32+
assert(v1[i] == v2[i - s]);
3333
}
34-
return true;
34+
}
35+
return true;
3536
}
3637

3738
int main(int, char**) {

0 commit comments

Comments
 (0)