1// -*- C++ -*-
2//===---------------------------- numeric ---------------------------------===//
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
11#define _LIBCPP_NUMERIC
12
13/*
14    numeric synopsis
15
16namespace std
17{
18
19template <class InputIterator, class T>
20    constexpr T  // constexpr since C++20
21    accumulate(InputIterator first, InputIterator last, T init);
22
23template <class InputIterator, class T, class BinaryOperation>
24    constexpr T  // constexpr since C++20
25    accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
26
27template<class InputIterator>
28    constexpr typename iterator_traits<InputIterator>::value_type  // constexpr since C++20
29    reduce(InputIterator first, InputIterator last);  // C++17
30
31template<class InputIterator, class T>
32    constexpr T  // constexpr since C++20
33    reduce(InputIterator first, InputIterator last, T init);  // C++17
34
35template<class InputIterator, class T, class BinaryOperation>
36    constexpr T  // constexpr since C++20
37    reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);  // C++17
38
39template <class InputIterator1, class InputIterator2, class T>
40    constexpr T  // constexpr since C++20
41    inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
42
43template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
44    constexpr T  // constexpr since C++20
45    inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
46                  T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
47
48
49template<class InputIterator1, class InputIterator2, class T>
50    constexpr T  // constexpr since C++20
51    transform_reduce(InputIterator1 first1, InputIterator1 last1,
52                     InputIterator2 first2, T init);  // C++17
53
54template<class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
55    constexpr T  // constexpr since C++20
56    transform_reduce(InputIterator1 first1, InputIterator1 last1,
57                     InputIterator2 first2, T init,
58                     BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);  // C++17
59
60template<class InputIterator, class T, class BinaryOperation, class UnaryOperation>
61    constexpr T  // constexpr since C++20
62    transform_reduce(InputIterator first, InputIterator last, T init,
63                     BinaryOperation binary_op, UnaryOperation unary_op);  // C++17
64
65template <class InputIterator, class OutputIterator>
66    constexpr OutputIterator  // constexpr since C++20
67    partial_sum(InputIterator first, InputIterator last, OutputIterator result);
68
69template <class InputIterator, class OutputIterator, class BinaryOperation>
70    constexpr OutputIterator  // constexpr since C++20
71    partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
72
73template<class InputIterator, class OutputIterator, class T>
74    constexpr OutputIterator  // constexpr since C++20
75    exclusive_scan(InputIterator first, InputIterator last,
76                   OutputIterator result, T init); // C++17
77
78template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
79    constexpr OutputIterator  // constexpr since C++20
80    exclusive_scan(InputIterator first, InputIterator last,
81                   OutputIterator result, T init, BinaryOperation binary_op); // C++17
82
83template<class InputIterator, class OutputIterator>
84    constexpr OutputIterator  // constexpr since C++20
85    inclusive_scan(InputIterator first, InputIterator last, OutputIterator result);  // C++17
86
87template<class InputIterator, class OutputIterator, class BinaryOperation>
88    constexpr OutputIterator  // constexpr since C++20
89    inclusive_scan(InputIterator first, InputIterator last,
90                   OutputIterator result, BinaryOperation binary_op);  // C++17
91
92template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
93    constexpr OutputIterator  // constexpr since C++20
94    inclusive_scan(InputIterator first, InputIterator last,
95                   OutputIterator result, BinaryOperation binary_op, T init);  // C++17
96
97template<class InputIterator, class OutputIterator, class T,
98         class BinaryOperation, class UnaryOperation>
99    constexpr OutputIterator  // constexpr since C++20
100    transform_exclusive_scan(InputIterator first, InputIterator last,
101                             OutputIterator result, T init,
102                             BinaryOperation binary_op, UnaryOperation unary_op);  // C++17
103
104template<class InputIterator, class OutputIterator,
105         class BinaryOperation, class UnaryOperation>
106    constexpr OutputIterator  // constexpr since C++20
107    transform_inclusive_scan(InputIterator first, InputIterator last,
108                             OutputIterator result,
109                             BinaryOperation binary_op, UnaryOperation unary_op);  // C++17
110
111template<class InputIterator, class OutputIterator,
112         class BinaryOperation, class UnaryOperation, class T>
113    constexpr OutputIterator  // constexpr since C++20
114    transform_inclusive_scan(InputIterator first, InputIterator last,
115                             OutputIterator result,
116                             BinaryOperation binary_op, UnaryOperation unary_op,
117                             T init);  // C++17
118
119template <class InputIterator, class OutputIterator>
120    constexpr OutputIterator  // constexpr since C++20
121    adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
122
123template <class InputIterator, class OutputIterator, class BinaryOperation>
124    constexpr OutputIterator  // constexpr since C++20
125    adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
126
127template <class ForwardIterator, class T>
128    constexpr void  // constexpr since C++20
129    iota(ForwardIterator first, ForwardIterator last, T value);
130
131template <class M, class N>
132    constexpr common_type_t<M,N> gcd(M m, N n);    // C++17
133
134template <class M, class N>
135    constexpr common_type_t<M,N> lcm(M m, N n);    // C++17
136
137template<class T>
138    constexpr T midpoint(T a, T b) noexcept;  // C++20
139
140template<class T>
141    constexpr T* midpoint(T* a, T* b);        // C++20
142
143}  // std
144
145*/
146
147#include <__config>
148#include <iterator>
149#include <limits> // for numeric_limits
150#include <functional>
151#include <cmath> // for isnormal
152#include <version>
153
154#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
155#pragma GCC system_header
156#endif
157
158_LIBCPP_PUSH_MACROS
159#include <__undef_macros>
160
161_LIBCPP_BEGIN_NAMESPACE_STD
162
163template <class _InputIterator, class _Tp>
164_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
165_Tp
166accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
167{
168    for (; __first != __last; ++__first)
169        __init = __init + *__first;
170    return __init;
171}
172
173template <class _InputIterator, class _Tp, class _BinaryOperation>
174_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
175_Tp
176accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
177{
178    for (; __first != __last; ++__first)
179        __init = __binary_op(__init, *__first);
180    return __init;
181}
182
183#if _LIBCPP_STD_VER > 14
184template <class _InputIterator, class _Tp, class _BinaryOp>
185_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
186_Tp
187reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b)
188{
189    for (; __first != __last; ++__first)
190        __init = __b(__init, *__first);
191    return __init;
192}
193
194template <class _InputIterator, class _Tp>
195_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
196_Tp
197reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
198{
199    return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
200}
201
202template <class _InputIterator>
203_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
204typename iterator_traits<_InputIterator>::value_type
205reduce(_InputIterator __first, _InputIterator __last)
206{
207    return _VSTD::reduce(__first, __last,
208       typename iterator_traits<_InputIterator>::value_type{});
209}
210#endif
211
212template <class _InputIterator1, class _InputIterator2, class _Tp>
213_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
214_Tp
215inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
216{
217    for (; __first1 != __last1; ++__first1, (void) ++__first2)
218        __init = __init + *__first1 * *__first2;
219    return __init;
220}
221
222template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
223_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
224_Tp
225inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
226              _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
227{
228    for (; __first1 != __last1; ++__first1, (void) ++__first2)
229        __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
230    return __init;
231}
232
233#if _LIBCPP_STD_VER > 14
234template <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
235_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
236_Tp
237transform_reduce(_InputIterator __first, _InputIterator __last,
238           _Tp __init,  _BinaryOp __b, _UnaryOp __u)
239{
240    for (; __first != __last; ++__first)
241        __init = __b(__init, __u(*__first));
242    return __init;
243}
244
245template <class _InputIterator1, class _InputIterator2,
246          class _Tp, class _BinaryOp1, class _BinaryOp2>
247_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
248_Tp
249transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
250                 _InputIterator2 __first2, _Tp __init,  _BinaryOp1 __b1, _BinaryOp2 __b2)
251{
252    for (; __first1 != __last1; ++__first1, (void) ++__first2)
253        __init = __b1(__init, __b2(*__first1, *__first2));
254    return __init;
255}
256
257template <class _InputIterator1, class _InputIterator2, class _Tp>
258_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
259_Tp
260transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
261                 _InputIterator2 __first2, _Tp __init)
262{
263    return _VSTD::transform_reduce(__first1, __last1, __first2, _VSTD::move(__init),
264                                   _VSTD::plus<>(), _VSTD::multiplies<>());
265}
266#endif
267
268template <class _InputIterator, class _OutputIterator>
269_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
270_OutputIterator
271partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
272{
273    if (__first != __last)
274    {
275        typename iterator_traits<_InputIterator>::value_type __t(*__first);
276        *__result = __t;
277        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
278        {
279            __t = __t + *__first;
280            *__result = __t;
281        }
282    }
283    return __result;
284}
285
286template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
287_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
288_OutputIterator
289partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
290              _BinaryOperation __binary_op)
291{
292    if (__first != __last)
293    {
294        typename iterator_traits<_InputIterator>::value_type __t(*__first);
295        *__result = __t;
296        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
297        {
298            __t = __binary_op(__t, *__first);
299            *__result = __t;
300        }
301    }
302    return __result;
303}
304
305#if _LIBCPP_STD_VER > 14
306template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
307_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
308_OutputIterator
309exclusive_scan(_InputIterator __first, _InputIterator __last,
310               _OutputIterator __result, _Tp __init, _BinaryOp __b)
311{
312    if (__first != __last)
313    {
314        _Tp __saved = __init;
315        do
316        {
317            __init = __b(__init, *__first);
318            *__result = __saved;
319            __saved = __init;
320            ++__result;
321        } while (++__first != __last);
322    }
323    return __result;
324}
325
326template <class _InputIterator, class _OutputIterator, class _Tp>
327_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
328_OutputIterator
329exclusive_scan(_InputIterator __first, _InputIterator __last,
330               _OutputIterator __result, _Tp __init)
331{
332    return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>());
333}
334
335template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
336_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
337_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
338                               _OutputIterator __result, _BinaryOp __b,  _Tp __init)
339{
340    for (; __first != __last; ++__first, (void) ++__result) {
341        __init = __b(__init, *__first);
342        *__result = __init;
343    }
344    return __result;
345}
346
347template <class _InputIterator, class _OutputIterator, class _BinaryOp>
348_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
349_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
350                               _OutputIterator __result, _BinaryOp __b)
351{
352    if (__first != __last) {
353        typename std::iterator_traits<_InputIterator>::value_type __init = *__first;
354        *__result++ = __init;
355        if (++__first != __last)
356            return _VSTD::inclusive_scan(__first, __last, __result, __b, __init);
357    }
358
359    return __result;
360}
361
362template <class _InputIterator, class _OutputIterator>
363_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
364_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
365                               _OutputIterator __result)
366{
367    return _VSTD::inclusive_scan(__first, __last, __result, std::plus<>());
368}
369
370template <class _InputIterator, class _OutputIterator, class _Tp,
371          class _BinaryOp, class _UnaryOp>
372_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
373_OutputIterator
374transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
375                           _OutputIterator __result, _Tp __init,
376                           _BinaryOp __b, _UnaryOp __u)
377{
378    if (__first != __last)
379    {
380        _Tp __saved = __init;
381        do
382        {
383            __init = __b(__init, __u(*__first));
384            *__result = __saved;
385            __saved = __init;
386            ++__result;
387        } while (++__first != __last);
388    }
389    return __result;
390}
391
392template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
393_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
394_OutputIterator
395transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
396                           _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init)
397{
398    for (; __first != __last; ++__first, (void) ++__result) {
399        __init = __b(__init, __u(*__first));
400        *__result = __init;
401        }
402
403    return __result;
404}
405
406template <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp>
407_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
408_OutputIterator
409transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
410                               _OutputIterator __result, _BinaryOp __b, _UnaryOp __u)
411{
412    if (__first != __last) {
413        typename std::iterator_traits<_InputIterator>::value_type __init = __u(*__first);
414        *__result++ = __init;
415        if (++__first != __last)
416            return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init);
417    }
418
419    return __result;
420}
421#endif
422
423template <class _InputIterator, class _OutputIterator>
424_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
425_OutputIterator
426adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
427{
428    if (__first != __last)
429    {
430        typename iterator_traits<_InputIterator>::value_type __t1(*__first);
431        *__result = __t1;
432        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
433        {
434            typename iterator_traits<_InputIterator>::value_type __t2(*__first);
435            *__result = __t2 - __t1;
436            __t1 = _VSTD::move(__t2);
437        }
438    }
439    return __result;
440}
441
442template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
443_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
444_OutputIterator
445adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
446                      _BinaryOperation __binary_op)
447{
448    if (__first != __last)
449    {
450        typename iterator_traits<_InputIterator>::value_type __t1(*__first);
451        *__result = __t1;
452        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
453        {
454            typename iterator_traits<_InputIterator>::value_type __t2(*__first);
455            *__result = __binary_op(__t2, __t1);
456            __t1 = _VSTD::move(__t2);
457        }
458    }
459    return __result;
460}
461
462template <class _ForwardIterator, class _Tp>
463_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
464void
465iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
466{
467    for (; __first != __last; ++__first, (void) ++__value_)
468        *__first = __value_;
469}
470
471
472#if _LIBCPP_STD_VER > 14
473template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __ct_abs;
474
475template <typename _Result, typename _Source>
476struct __ct_abs<_Result, _Source, true> {
477    _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
478    _Result operator()(_Source __t) const noexcept
479    {
480        if (__t >= 0) return __t;
481        if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t);
482        return -__t;
483    }
484};
485
486template <typename _Result, typename _Source>
487struct __ct_abs<_Result, _Source, false> {
488    _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
489    _Result operator()(_Source __t) const noexcept { return __t; }
490};
491
492
493template<class _Tp>
494_LIBCPP_CONSTEXPR _LIBCPP_HIDDEN
495_Tp __gcd(_Tp __m, _Tp __n)
496{
497    static_assert((!is_signed<_Tp>::value), "");
498    return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n);
499}
500
501
502template<class _Tp, class _Up>
503_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
504common_type_t<_Tp,_Up>
505gcd(_Tp __m, _Up __n)
506{
507    static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
508    static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" );
509    static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" );
510    using _Rp = common_type_t<_Tp,_Up>;
511    using _Wp = make_unsigned_t<_Rp>;
512    return static_cast<_Rp>(_VSTD::__gcd(
513        static_cast<_Wp>(__ct_abs<_Rp, _Tp>()(__m)),
514        static_cast<_Wp>(__ct_abs<_Rp, _Up>()(__n))));
515}
516
517template<class _Tp, class _Up>
518_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
519common_type_t<_Tp,_Up>
520lcm(_Tp __m, _Up __n)
521{
522    static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
523    static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" );
524    static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" );
525    if (__m == 0 || __n == 0)
526        return 0;
527
528    using _Rp = common_type_t<_Tp,_Up>;
529    _Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n);
530    _Rp __val2 = __ct_abs<_Rp, _Up>()(__n);
531    _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
532    return __val1 * __val2;
533}
534
535#endif /* _LIBCPP_STD_VER > 14 */
536
537#if _LIBCPP_STD_VER > 17
538template <class _Tp>
539_LIBCPP_INLINE_VISIBILITY constexpr
540enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>, _Tp>
541midpoint(_Tp __a, _Tp __b) noexcept
542_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
543{
544    using _Up = std::make_unsigned_t<_Tp>;
545    constexpr _Up __bitshift = std::numeric_limits<_Up>::digits - 1;
546
547    _Up __diff = _Up(__b) - _Up(__a);
548    _Up __sign_bit = __b < __a;
549
550    _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff);
551
552    return __a + __half_diff;
553}
554
555
556template <class _TPtr>
557_LIBCPP_INLINE_VISIBILITY constexpr
558enable_if_t<is_pointer_v<_TPtr>
559             && is_object_v<remove_pointer_t<_TPtr>>
560             && ! is_void_v<remove_pointer_t<_TPtr>>
561             && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr>
562midpoint(_TPtr __a, _TPtr __b) noexcept
563{
564    return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a);
565}
566
567
568template <typename _Tp>
569constexpr int __sign(_Tp __val) {
570    return (_Tp(0) < __val) - (__val < _Tp(0));
571}
572
573template <typename _Fp>
574constexpr _Fp __fp_abs(_Fp __f) { return __f >= 0 ? __f : -__f; }
575
576template <class _Fp>
577_LIBCPP_INLINE_VISIBILITY constexpr
578enable_if_t<is_floating_point_v<_Fp>, _Fp>
579midpoint(_Fp __a, _Fp __b) noexcept
580{
581	constexpr _Fp __lo = numeric_limits<_Fp>::min()*2;
582	constexpr _Fp __hi = numeric_limits<_Fp>::max()/2;
583    return __fp_abs(__a) <= __hi && __fp_abs(__b) <= __hi ?  // typical case: overflow is impossible
584      (__a + __b)/2 :                                        // always correctly rounded
585      __fp_abs(__a) < __lo ? __a + __b/2 :                   // not safe to halve a
586      __fp_abs(__b) < __lo ? __a/2 + __b :                   // not safe to halve b
587      __a/2 + __b/2;                                         // otherwise correctly rounded
588}
589
590#endif // _LIBCPP_STD_VER > 17
591
592_LIBCPP_END_NAMESPACE_STD
593
594_LIBCPP_POP_MACROS
595
596#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
597#   include <__pstl_numeric>
598#endif
599
600#endif  // _LIBCPP_NUMERIC
601