xref: /llvm-project/libcxx/include/__numeric/midpoint.h (revision e99c4906e44ae3f921fa05356909d006cda8d954)
1a0efb175SChristopher Di Bella // -*- C++ -*-
2a0efb175SChristopher Di Bella //===----------------------------------------------------------------------===//
3a0efb175SChristopher Di Bella //
4a0efb175SChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5a0efb175SChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information.
6a0efb175SChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7a0efb175SChristopher Di Bella //
8a0efb175SChristopher Di Bella //===----------------------------------------------------------------------===//
9a0efb175SChristopher Di Bella 
10a0efb175SChristopher Di Bella #ifndef _LIBCPP___NUMERIC_MIDPOINT_H
11a0efb175SChristopher Di Bella #define _LIBCPP___NUMERIC_MIDPOINT_H
12a0efb175SChristopher Di Bella 
13a0efb175SChristopher Di Bella #include <__config>
14*e99c4906SNikolas Klauser #include <__cstddef/ptrdiff_t.h>
15fafed06bSNikolas Klauser #include <__type_traits/enable_if.h>
16fafed06bSNikolas Klauser #include <__type_traits/is_floating_point.h>
17fafed06bSNikolas Klauser #include <__type_traits/is_integral.h>
18fafed06bSNikolas Klauser #include <__type_traits/is_null_pointer.h>
19fafed06bSNikolas Klauser #include <__type_traits/is_object.h>
20fafed06bSNikolas Klauser #include <__type_traits/is_pointer.h>
21fafed06bSNikolas Klauser #include <__type_traits/is_same.h>
22fafed06bSNikolas Klauser #include <__type_traits/is_void.h>
23fafed06bSNikolas Klauser #include <__type_traits/make_unsigned.h>
24fafed06bSNikolas Klauser #include <__type_traits/remove_pointer.h>
25a0efb175SChristopher Di Bella #include <limits>
26a0efb175SChristopher Di Bella 
27a0efb175SChristopher Di Bella #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28a0efb175SChristopher Di Bella #  pragma GCC system_header
29a0efb175SChristopher Di Bella #endif
30a0efb175SChristopher Di Bella 
31a0efb175SChristopher Di Bella _LIBCPP_PUSH_MACROS
32a0efb175SChristopher Di Bella #include <__undef_macros>
33a0efb175SChristopher Di Bella 
34a0efb175SChristopher Di Bella _LIBCPP_BEGIN_NAMESPACE_STD
35a0efb175SChristopher Di Bella 
364f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 20
37a0efb175SChristopher Di Bella template <class _Tp>
389783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>, _Tp>
399783f28cSLouis Dionne midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
40a0efb175SChristopher Di Bella   using _Up                = make_unsigned_t<_Tp>;
41a0efb175SChristopher Di Bella   constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
42a0efb175SChristopher Di Bella 
43a0efb175SChristopher Di Bella   _Up __diff     = _Up(__b) - _Up(__a);
44a0efb175SChristopher Di Bella   _Up __sign_bit = __b < __a;
45a0efb175SChristopher Di Bella 
46a0efb175SChristopher Di Bella   _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff);
47a0efb175SChristopher Di Bella 
48a0efb175SChristopher Di Bella   return __a + __half_diff;
49a0efb175SChristopher Di Bella }
50a0efb175SChristopher Di Bella 
51c37734d4SSanjay Marreddi template <class _Tp, enable_if_t<is_object_v<_Tp> && !is_void_v<_Tp> && (sizeof(_Tp) > 0), int> = 0>
52c37734d4SSanjay Marreddi _LIBCPP_HIDE_FROM_ABI constexpr _Tp* midpoint(_Tp* __a, _Tp* __b) noexcept {
5377a00c0dSLouis Dionne   return __a + std::midpoint(ptrdiff_t(0), __b - __a);
54a0efb175SChristopher Di Bella }
55a0efb175SChristopher Di Bella 
56a0efb175SChristopher Di Bella template <typename _Tp>
5780c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI constexpr int __sign(_Tp __val) {
58a0efb175SChristopher Di Bella   return (_Tp(0) < __val) - (__val < _Tp(0));
59a0efb175SChristopher Di Bella }
60a0efb175SChristopher Di Bella 
61a0efb175SChristopher Di Bella template <typename _Fp>
629783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI constexpr _Fp __fp_abs(_Fp __f) {
639783f28cSLouis Dionne   return __f >= 0 ? __f : -__f;
649783f28cSLouis Dionne }
65a0efb175SChristopher Di Bella 
66a0efb175SChristopher Di Bella template <class _Fp>
679783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<is_floating_point_v<_Fp>, _Fp> midpoint(_Fp __a, _Fp __b) noexcept {
68a0efb175SChristopher Di Bella   constexpr _Fp __lo = numeric_limits<_Fp>::min() * 2;
69a0efb175SChristopher Di Bella   constexpr _Fp __hi = numeric_limits<_Fp>::max() / 2;
70715567d0Srilysh 
71715567d0Srilysh   // typical case: overflow is impossible
72715567d0Srilysh   if (std::__fp_abs(__a) <= __hi && std::__fp_abs(__b) <= __hi)
73715567d0Srilysh     return (__a + __b) / 2; // always correctly rounded
74715567d0Srilysh   if (std::__fp_abs(__a) < __lo)
75715567d0Srilysh     return __a + __b / 2; // not safe to halve a
76715567d0Srilysh   if (std::__fp_abs(__b) < __lo)
77715567d0Srilysh     return __a / 2 + __b; // not safe to halve b
78715567d0Srilysh 
79715567d0Srilysh   return __a / 2 + __b / 2; // otherwise correctly rounded
80a0efb175SChristopher Di Bella }
81a0efb175SChristopher Di Bella 
824f15267dSNikolas Klauser #endif // _LIBCPP_STD_VER >= 20
83a0efb175SChristopher Di Bella 
84a0efb175SChristopher Di Bella _LIBCPP_END_NAMESPACE_STD
85a0efb175SChristopher Di Bella 
86a0efb175SChristopher Di Bella _LIBCPP_POP_MACROS
87a0efb175SChristopher Di Bella 
88a0efb175SChristopher Di Bella #endif // _LIBCPP___NUMERIC_MIDPOINT_H
89