1*4824e7fdSDimitry Andric // -*- C++ -*- 2*4824e7fdSDimitry Andric //===----------------------------------------------------------------------===// 3*4824e7fdSDimitry Andric // 4*4824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5*4824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6*4824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7*4824e7fdSDimitry Andric // 8*4824e7fdSDimitry Andric //===----------------------------------------------------------------------===// 9*4824e7fdSDimitry Andric 10*4824e7fdSDimitry Andric #ifndef _LIBCPP___NUMERIC_MIDPOINT_H 11*4824e7fdSDimitry Andric #define _LIBCPP___NUMERIC_MIDPOINT_H 12*4824e7fdSDimitry Andric 13*4824e7fdSDimitry Andric #include <__config> 14*4824e7fdSDimitry Andric #include <limits> 15*4824e7fdSDimitry Andric #include <type_traits> 16*4824e7fdSDimitry Andric 17*4824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 18*4824e7fdSDimitry Andric # pragma GCC system_header 19*4824e7fdSDimitry Andric #endif 20*4824e7fdSDimitry Andric 21*4824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS 22*4824e7fdSDimitry Andric #include <__undef_macros> 23*4824e7fdSDimitry Andric 24*4824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 25*4824e7fdSDimitry Andric 26*4824e7fdSDimitry Andric #if _LIBCPP_STD_VER > 17 27*4824e7fdSDimitry Andric template <class _Tp> 28*4824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY constexpr 29*4824e7fdSDimitry Andric enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>, _Tp> 30*4824e7fdSDimitry Andric midpoint(_Tp __a, _Tp __b) noexcept 31*4824e7fdSDimitry Andric _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK 32*4824e7fdSDimitry Andric { 33*4824e7fdSDimitry Andric using _Up = make_unsigned_t<_Tp>; 34*4824e7fdSDimitry Andric constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1; 35*4824e7fdSDimitry Andric 36*4824e7fdSDimitry Andric _Up __diff = _Up(__b) - _Up(__a); 37*4824e7fdSDimitry Andric _Up __sign_bit = __b < __a; 38*4824e7fdSDimitry Andric 39*4824e7fdSDimitry Andric _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff); 40*4824e7fdSDimitry Andric 41*4824e7fdSDimitry Andric return __a + __half_diff; 42*4824e7fdSDimitry Andric } 43*4824e7fdSDimitry Andric 44*4824e7fdSDimitry Andric 45*4824e7fdSDimitry Andric template <class _TPtr> 46*4824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY constexpr 47*4824e7fdSDimitry Andric enable_if_t<is_pointer_v<_TPtr> 48*4824e7fdSDimitry Andric && is_object_v<remove_pointer_t<_TPtr>> 49*4824e7fdSDimitry Andric && ! is_void_v<remove_pointer_t<_TPtr>> 50*4824e7fdSDimitry Andric && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr> 51*4824e7fdSDimitry Andric midpoint(_TPtr __a, _TPtr __b) noexcept 52*4824e7fdSDimitry Andric { 53*4824e7fdSDimitry Andric return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a); 54*4824e7fdSDimitry Andric } 55*4824e7fdSDimitry Andric 56*4824e7fdSDimitry Andric 57*4824e7fdSDimitry Andric template <typename _Tp> 58*4824e7fdSDimitry Andric constexpr int __sign(_Tp __val) { 59*4824e7fdSDimitry Andric return (_Tp(0) < __val) - (__val < _Tp(0)); 60*4824e7fdSDimitry Andric } 61*4824e7fdSDimitry Andric 62*4824e7fdSDimitry Andric template <typename _Fp> 63*4824e7fdSDimitry Andric constexpr _Fp __fp_abs(_Fp __f) { return __f >= 0 ? __f : -__f; } 64*4824e7fdSDimitry Andric 65*4824e7fdSDimitry Andric template <class _Fp> 66*4824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY constexpr 67*4824e7fdSDimitry Andric enable_if_t<is_floating_point_v<_Fp>, _Fp> 68*4824e7fdSDimitry Andric midpoint(_Fp __a, _Fp __b) noexcept 69*4824e7fdSDimitry Andric { 70*4824e7fdSDimitry Andric constexpr _Fp __lo = numeric_limits<_Fp>::min()*2; 71*4824e7fdSDimitry Andric constexpr _Fp __hi = numeric_limits<_Fp>::max()/2; 72*4824e7fdSDimitry Andric return __fp_abs(__a) <= __hi && __fp_abs(__b) <= __hi ? // typical case: overflow is impossible 73*4824e7fdSDimitry Andric (__a + __b)/2 : // always correctly rounded 74*4824e7fdSDimitry Andric __fp_abs(__a) < __lo ? __a + __b/2 : // not safe to halve a 75*4824e7fdSDimitry Andric __fp_abs(__b) < __lo ? __a/2 + __b : // not safe to halve b 76*4824e7fdSDimitry Andric __a/2 + __b/2; // otherwise correctly rounded 77*4824e7fdSDimitry Andric } 78*4824e7fdSDimitry Andric 79*4824e7fdSDimitry Andric #endif // _LIBCPP_STD_VER > 17 80*4824e7fdSDimitry Andric 81*4824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD 82*4824e7fdSDimitry Andric 83*4824e7fdSDimitry Andric _LIBCPP_POP_MACROS 84*4824e7fdSDimitry Andric 85*4824e7fdSDimitry Andric #endif // _LIBCPP___NUMERIC_MIDPOINT_H 86