Skip to content

Commit 9944627

Browse files
committed
Remove unnecessary cast
1 parent d4b9d9e commit 9944627

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

libcxx/include/__bit_reference

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,19 @@ struct __size_difference_type_traits<_Cp, __void_t<typename _Cp::difference_type
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 _StorageType(~_StorageType(0)) >> __clz;
7676
}
7777

7878
// Creates a mask of type `_StorageType` with a specified number of leading zeros (__clz), a specified number of
7979
// 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 (_StorageType(~_StorageType(0)) << __ctz) & std::__trailing_mask<_StorageType>(__clz);
8684
}
8785

8886
// 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.
87+
// or `unsigned short`.
9188
// See https://github.com/llvm/llvm-project/pull/122410.
9289
template <class _StoragePointer>
9390
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
@@ -97,12 +94,11 @@ __fill_masked_range(_StoragePointer __word, unsigned __clz, unsigned __ctz, bool
9794
using _StorageType = typename pointer_traits<_StoragePointer>::element_type;
9895
_LIBCPP_ASSERT_VALID_INPUT_RANGE(
9996
__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);
97+
_StorageType __m = std::__middle_mask<_StorageType>(__clz, __ctz);
10298
if (__fill_val)
10399
*__word |= __m;
104100
else
105-
*__word &= static_cast<_StorageType>(~__m);
101+
*__word &= ~__m;
106102
}
107103

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

0 commit comments

Comments
 (0)