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