@@ -64,30 +64,28 @@ struct __size_difference_type_traits<_Cp, __void_t<typename _Cp::difference_type
64
64
65
65
// The `__x_mask` functions are designed to work exclusively with any unsigned `_StorageType`s, including small
66
66
// 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`.
69
69
70
70
// 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.
72
72
template <class _StorageType >
73
73
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __trailing_mask (unsigned __clz) {
74
74
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;
76
76
}
77
77
78
78
// 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.
80
80
template <class _StorageType >
81
81
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __middle_mask (unsigned __clz, unsigned __ctz) {
82
82
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);
86
85
}
87
86
88
87
// 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`.
91
89
// See https://github.com/llvm/llvm-project/pull/122410.
92
90
template <class _StoragePointer >
93
91
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
@@ -97,12 +95,11 @@ __fill_masked_range(_StoragePointer __word, unsigned __clz, unsigned __ctz, bool
97
95
using _StorageType = typename pointer_traits<_StoragePointer>::element_type;
98
96
_LIBCPP_ASSERT_VALID_INPUT_RANGE (
99
97
__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);
102
99
if (__fill_val)
103
100
*__word |= __m;
104
101
else
105
- *__word &= static_cast <_StorageType>( ~__m) ;
102
+ *__word &= ~__m;
106
103
}
107
104
108
105
template <class _Cp , bool = __has_storage_type<_Cp>::value>
0 commit comments