@@ -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.
7272template <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.
8080template <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.
9290template <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 = __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
108105template <class _Cp , bool = __has_storage_type<_Cp>::value>
0 commit comments