Index: include/numeric =================================================================== --- include/numeric +++ include/numeric @@ -532,17 +532,14 @@ _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK { using _Up = std::make_unsigned_t<_Tp>; + constexpr _Up __bitshift = std::numeric_limits<_Up>::digits - 1; - int __sign = 1; - _Up __m = __a; - _Up __M = __b; - if (__a > __b) - { - __sign = -1; - __m = __b; - __M = __a; - } - return __a + __sign * _Tp(_Up(__M-__m) >> 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; }