// -*- C++ -*- //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// //============================================================================== // NOTICE: This file has been modified by Green Hills Software, Inc. Such // modifications are subject to copyright by Green Hills Software, Inc. See the // "Green Hills Software Modifications" section in the LICENSE.txt included with // this distribution for more information. //============================================================================== #ifndef _LIBCPP___NUMERIC_MIDPOINT_H #define _LIBCPP___NUMERIC_MIDPOINT_H #include <__config> #include #include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header #endif _LIBCPP_PUSH_MACROS #include <__undef_macros> #if defined(__ghs__) #pragma ghs start_cxx_lib_header #pragma ghs startdata #if defined(__ghs_max_pack_value) #pragma pack (push, __ghs_max_pack_value) #endif #endif _LIBCPP_BEGIN_NAMESPACE_STD #if _LIBCPP_STD_VER > 17 template _LIBCPP_INLINE_VISIBILITY constexpr enable_if_t && !is_same_v && !is_null_pointer_v<_Tp>, _Tp> midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK { using _Up = make_unsigned_t<_Tp>; constexpr _Up __bitshift = 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; } #if defined(__ghs__) // In order to make the specification for midpoint correct, make the template // type the type pointed to by the input pointer type instead of the pointer // type itself. Libcxx intends this change to avoid users getting the wrong // specialization when specializing midpoint explicitly, but their version // is not standard conformant. template _LIBCPP_INLINE_VISIBILITY constexpr enable_if_t && ! is_void_v<_T> && (sizeof(_T) > 0), _T*> midpoint(_T *__a, _T *__b) noexcept #else template _LIBCPP_INLINE_VISIBILITY constexpr enable_if_t && is_object_v> && ! is_void_v> && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr> midpoint(_TPtr __a, _TPtr __b) noexcept #endif /* defined(__ghs__) */ { return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a); } template _LIBCPP_HIDE_FROM_ABI constexpr int __sign(_Tp __val) { return (_Tp(0) < __val) - (__val < _Tp(0)); } template _LIBCPP_HIDE_FROM_ABI constexpr _Fp __fp_abs(_Fp __f) { return __f >= 0 ? __f : -__f; } template _LIBCPP_INLINE_VISIBILITY constexpr enable_if_t, _Fp> midpoint(_Fp __a, _Fp __b) noexcept { constexpr _Fp __lo = numeric_limits<_Fp>::min()*2; constexpr _Fp __hi = numeric_limits<_Fp>::max()/2; return __fp_abs(__a) <= __hi && __fp_abs(__b) <= __hi ? // typical case: overflow is impossible (__a + __b)/2 : // always correctly rounded __fp_abs(__a) < __lo ? __a + __b/2 : // not safe to halve a __fp_abs(__b) < __lo ? __a/2 + __b : // not safe to halve b __a/2 + __b/2; // otherwise correctly rounded } #endif // _LIBCPP_STD_VER > 17 _LIBCPP_END_NAMESPACE_STD #if defined(__ghs__) #if defined(__ghs_max_pack_value) #pragma pack(pop) #endif #pragma ghs enddata #pragma ghs end_cxx_lib_header #endif _LIBCPP_POP_MACROS #endif // _LIBCPP___NUMERIC_MIDPOINT_H