Index: libcxx/include/numeric =================================================================== --- libcxx/include/numeric +++ libcxx/include/numeric @@ -531,15 +531,9 @@ midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK { - using _Up = std::make_unsigned_t<_Tp>; - constexpr _Up __bitshift = std::numeric_limits<_Up>::digits - 1; - - _Up __diff = _Up(__b) - _Up(__a); - _Up __sign_bit = __b < __a; - - _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff); - - return __a + __half_diff; + _Tp floored_avg = (__a & __b) + ((__a ^ __b) >> 1); + _Tp shift = (__a ^ __b)&(__a >__b); + return floored_avg + shift; }