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