@@ -25,20 +25,18 @@ class __bit_iterator;
2525template <class , class = void >
2626struct __size_difference_type_traits ;
2727
28+ // This function is designed to operate correctly even for smaller integral types like `uint8_t`, `uint16_t`,
29+ // or `unsigned short`. Casting back to _StorageType is crucial to prevent undefined behavior that can arise
30+ // from integral promotions.
31+ // See https://github.com/llvm/llvm-project/pull/122410
2832template <class _StorageType , __enable_if_t <is_unsigned<_StorageType>::value, int > = 0 >
29- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __leading_mask (unsigned __shift) {
30- return static_cast <_StorageType>(static_cast <_StorageType>(~static_cast <_StorageType>(0 )) << __shift);
31- }
32-
33- template <class _StorageType , __enable_if_t <is_unsigned<_StorageType>::value, int > = 0 >
34- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __trailing_mask (unsigned __shift) {
35- return static_cast <_StorageType>(static_cast <_StorageType>(~static_cast <_StorageType>(0 )) >> __shift);
36- }
37-
38- template <class _StorageType , __enable_if_t <is_unsigned<_StorageType>::value, int > = 0 >
39- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __middle_mask (unsigned __lshift, unsigned __rshift) {
40- return static_cast <_StorageType>(
41- std::__leading_mask<_StorageType>(__lshift) & std::__trailing_mask<_StorageType>(__rshift));
33+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _StorageType
34+ __fill_range_in_word (_StorageType __word, unsigned __ctz, unsigned __clz, bool __fill_val) {
35+ _LIBCPP_ASSERT_VALID_INPUT_RANGE (
36+ __ctz + __clz < sizeof (_StorageType) * CHAR_BIT, " __fill_range called with invalid range" );
37+ _StorageType __m = static_cast <_StorageType>(static_cast <_StorageType>(~static_cast <_StorageType>(0 )) << __ctz) &
38+ static_cast <_StorageType>(static_cast <_StorageType>(~static_cast <_StorageType>(0 )) >> __clz);
39+ return __fill_val ? __word | __m : __word & ~__m;
4240}
4341
4442_LIBCPP_END_NAMESPACE_STD
0 commit comments