2 //===---------------------------- numeric ---------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_NUMERIC
11 #define _LIBCPP_NUMERIC
19 template <class InputIterator, class T>
21 accumulate(InputIterator first, InputIterator last, T init);
23 template <class InputIterator, class T, class BinaryOperation>
25 accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
27 template<class InputIterator>
28 typename iterator_traits<InputIterator>::value_type
29 reduce(InputIterator first, InputIterator last); // C++17
31 template<class InputIterator, class T>
33 reduce(InputIterator first, InputIterator last, T init); // C++17
35 template<class InputIterator, class T, class BinaryOperation>
37 reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op); // C++17
39 template <class InputIterator1, class InputIterator2, class T>
41 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
43 template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
45 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
46 T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
49 template<class InputIterator1, class InputIterator2, class T>
51 transform_reduce(InputIterator1 first1, InputIterator1 last1,
52 InputIterator2 first2, T init); // C++17
54 template<class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
56 transform_reduce(InputIterator1 first1, InputIterator1 last1,
57 InputIterator2 first2, T init,
58 BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); // C++17
60 template<class InputIterator, class T, class BinaryOperation, class UnaryOperation>
62 transform_reduce(InputIterator first, InputIterator last, T init,
63 BinaryOperation binary_op, UnaryOperation unary_op); // C++17
65 template <class InputIterator, class OutputIterator>
67 partial_sum(InputIterator first, InputIterator last, OutputIterator result);
69 template <class InputIterator, class OutputIterator, class BinaryOperation>
71 partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
73 template<class InputIterator, class OutputIterator, class T>
75 exclusive_scan(InputIterator first, InputIterator last,
76 OutputIterator result, T init); // C++17
78 template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
80 exclusive_scan(InputIterator first, InputIterator last,
81 OutputIterator result, T init, BinaryOperation binary_op); // C++17
83 template<class InputIterator, class OutputIterator>
85 inclusive_scan(InputIterator first, InputIterator last, OutputIterator result); // C++17
87 template<class InputIterator, class OutputIterator, class BinaryOperation>
89 inclusive_scan(InputIterator first, InputIterator last,
90 OutputIterator result, BinaryOperation binary_op); // C++17
92 template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
94 inclusive_scan(InputIterator first, InputIterator last,
95 OutputIterator result, BinaryOperation binary_op, T init); // C++17
97 template<class InputIterator, class OutputIterator, class T,
98 class BinaryOperation, class UnaryOperation>
100 transform_exclusive_scan(InputIterator first, InputIterator last,
101 OutputIterator result, T init,
102 BinaryOperation binary_op, UnaryOperation unary_op); // C++17
104 template<class InputIterator, class OutputIterator,
105 class BinaryOperation, class UnaryOperation>
107 transform_inclusive_scan(InputIterator first, InputIterator last,
108 OutputIterator result,
109 BinaryOperation binary_op, UnaryOperation unary_op); // C++17
111 template<class InputIterator, class OutputIterator,
112 class BinaryOperation, class UnaryOperation, class T>
114 transform_inclusive_scan(InputIterator first, InputIterator last,
115 OutputIterator result,
116 BinaryOperation binary_op, UnaryOperation unary_op,
119 template <class InputIterator, class OutputIterator>
121 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
123 template <class InputIterator, class OutputIterator, class BinaryOperation>
125 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
127 template <class ForwardIterator, class T>
128 void iota(ForwardIterator first, ForwardIterator last, T value);
130 template <class M, class N>
131 constexpr common_type_t<M,N> gcd(M m, N n); // C++17
133 template <class M, class N>
134 constexpr common_type_t<M,N> lcm(M m, N n); // C++17
136 integer midpoint(integer a, integer b); // C++20
137 pointer midpoint(pointer a, pointer b); // C++20
138 floating_point midpoint(floating_point a, floating_point b); // C++20
146 #include <limits> // for numeric_limits
147 #include <functional>
148 #include <cmath> // for isnormal
151 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
152 #pragma GCC system_header
156 #include <__undef_macros>
158 _LIBCPP_BEGIN_NAMESPACE_STD
160 template <class _InputIterator, class _Tp>
161 inline _LIBCPP_INLINE_VISIBILITY
163 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
165 for (; __first != __last; ++__first)
166 __init = __init + *__first;
170 template <class _InputIterator, class _Tp, class _BinaryOperation>
171 inline _LIBCPP_INLINE_VISIBILITY
173 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
175 for (; __first != __last; ++__first)
176 __init = __binary_op(__init, *__first);
180 #if _LIBCPP_STD_VER > 14
181 template <class _InputIterator, class _Tp, class _BinaryOp>
182 inline _LIBCPP_INLINE_VISIBILITY
184 reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b)
186 for (; __first != __last; ++__first)
187 __init = __b(__init, *__first);
191 template <class _InputIterator, class _Tp>
192 inline _LIBCPP_INLINE_VISIBILITY
194 reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
196 return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
199 template <class _InputIterator>
200 inline _LIBCPP_INLINE_VISIBILITY
201 typename iterator_traits<_InputIterator>::value_type
202 reduce(_InputIterator __first, _InputIterator __last)
204 return _VSTD::reduce(__first, __last,
205 typename iterator_traits<_InputIterator>::value_type{});
209 template <class _InputIterator1, class _InputIterator2, class _Tp>
210 inline _LIBCPP_INLINE_VISIBILITY
212 inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
214 for (; __first1 != __last1; ++__first1, (void) ++__first2)
215 __init = __init + *__first1 * *__first2;
219 template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
220 inline _LIBCPP_INLINE_VISIBILITY
222 inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
223 _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
225 for (; __first1 != __last1; ++__first1, (void) ++__first2)
226 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
230 #if _LIBCPP_STD_VER > 14
231 template <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
232 inline _LIBCPP_INLINE_VISIBILITY
234 transform_reduce(_InputIterator __first, _InputIterator __last,
235 _Tp __init, _BinaryOp __b, _UnaryOp __u)
237 for (; __first != __last; ++__first)
238 __init = __b(__init, __u(*__first));
242 template <class _InputIterator1, class _InputIterator2,
243 class _Tp, class _BinaryOp1, class _BinaryOp2>
244 inline _LIBCPP_INLINE_VISIBILITY
246 transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
247 _InputIterator2 __first2, _Tp __init, _BinaryOp1 __b1, _BinaryOp2 __b2)
249 for (; __first1 != __last1; ++__first1, (void) ++__first2)
250 __init = __b1(__init, __b2(*__first1, *__first2));
254 template <class _InputIterator1, class _InputIterator2, class _Tp>
255 inline _LIBCPP_INLINE_VISIBILITY
257 transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
258 _InputIterator2 __first2, _Tp __init)
260 return _VSTD::transform_reduce(__first1, __last1, __first2, _VSTD::move(__init),
261 _VSTD::plus<>(), _VSTD::multiplies<>());
265 template <class _InputIterator, class _OutputIterator>
266 inline _LIBCPP_INLINE_VISIBILITY
268 partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
270 if (__first != __last)
272 typename iterator_traits<_InputIterator>::value_type __t(*__first);
274 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
276 __t = __t + *__first;
283 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
284 inline _LIBCPP_INLINE_VISIBILITY
286 partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
287 _BinaryOperation __binary_op)
289 if (__first != __last)
291 typename iterator_traits<_InputIterator>::value_type __t(*__first);
293 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
295 __t = __binary_op(__t, *__first);
302 #if _LIBCPP_STD_VER > 14
303 template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
304 inline _LIBCPP_INLINE_VISIBILITY
306 exclusive_scan(_InputIterator __first, _InputIterator __last,
307 _OutputIterator __result, _Tp __init, _BinaryOp __b)
309 if (__first != __last)
311 _Tp __saved = __init;
314 __init = __b(__init, *__first);
318 } while (++__first != __last);
323 template <class _InputIterator, class _OutputIterator, class _Tp>
324 inline _LIBCPP_INLINE_VISIBILITY
326 exclusive_scan(_InputIterator __first, _InputIterator __last,
327 _OutputIterator __result, _Tp __init)
329 return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>());
332 template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
333 _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
334 _OutputIterator __result, _BinaryOp __b, _Tp __init)
336 for (; __first != __last; ++__first, (void) ++__result) {
337 __init = __b(__init, *__first);
343 template <class _InputIterator, class _OutputIterator, class _BinaryOp>
344 _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
345 _OutputIterator __result, _BinaryOp __b)
347 if (__first != __last) {
348 typename std::iterator_traits<_InputIterator>::value_type __init = *__first;
349 *__result++ = __init;
350 if (++__first != __last)
351 return _VSTD::inclusive_scan(__first, __last, __result, __b, __init);
357 template <class _InputIterator, class _OutputIterator>
358 _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
359 _OutputIterator __result)
361 return _VSTD::inclusive_scan(__first, __last, __result, std::plus<>());
364 template <class _InputIterator, class _OutputIterator, class _Tp,
365 class _BinaryOp, class _UnaryOp>
366 inline _LIBCPP_INLINE_VISIBILITY
368 transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
369 _OutputIterator __result, _Tp __init,
370 _BinaryOp __b, _UnaryOp __u)
372 if (__first != __last)
374 _Tp __saved = __init;
377 __init = __b(__init, __u(*__first));
381 } while (++__first != __last);
386 template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
387 _OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
388 _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init)
390 for (; __first != __last; ++__first, (void) ++__result) {
391 __init = __b(__init, __u(*__first));
398 template <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp>
399 _OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
400 _OutputIterator __result, _BinaryOp __b, _UnaryOp __u)
402 if (__first != __last) {
403 typename std::iterator_traits<_InputIterator>::value_type __init = __u(*__first);
404 *__result++ = __init;
405 if (++__first != __last)
406 return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init);
413 template <class _InputIterator, class _OutputIterator>
414 inline _LIBCPP_INLINE_VISIBILITY
416 adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
418 if (__first != __last)
420 typename iterator_traits<_InputIterator>::value_type __t1(*__first);
422 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
424 typename iterator_traits<_InputIterator>::value_type __t2(*__first);
425 *__result = __t2 - __t1;
426 __t1 = _VSTD::move(__t2);
432 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
433 inline _LIBCPP_INLINE_VISIBILITY
435 adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
436 _BinaryOperation __binary_op)
438 if (__first != __last)
440 typename iterator_traits<_InputIterator>::value_type __t1(*__first);
442 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
444 typename iterator_traits<_InputIterator>::value_type __t2(*__first);
445 *__result = __binary_op(__t2, __t1);
446 __t1 = _VSTD::move(__t2);
452 template <class _ForwardIterator, class _Tp>
453 inline _LIBCPP_INLINE_VISIBILITY
455 iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
457 for (; __first != __last; ++__first, (void) ++__value_)
462 #if _LIBCPP_STD_VER > 14
463 template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __ct_abs;
465 template <typename _Result, typename _Source>
466 struct __ct_abs<_Result, _Source, true> {
467 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
468 _Result operator()(_Source __t) const noexcept
470 if (__t >= 0) return __t;
471 if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t);
476 template <typename _Result, typename _Source>
477 struct __ct_abs<_Result, _Source, false> {
478 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
479 _Result operator()(_Source __t) const noexcept { return __t; }
484 _LIBCPP_CONSTEXPR _LIBCPP_HIDDEN
485 _Tp __gcd(_Tp __m, _Tp __n)
487 static_assert((!is_signed<_Tp>::value), "");
488 return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n);
492 template<class _Tp, class _Up>
493 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
494 common_type_t<_Tp,_Up>
495 gcd(_Tp __m, _Up __n)
497 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
498 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" );
499 static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" );
500 using _Rp = common_type_t<_Tp,_Up>;
501 using _Wp = make_unsigned_t<_Rp>;
502 return static_cast<_Rp>(_VSTD::__gcd(
503 static_cast<_Wp>(__ct_abs<_Rp, _Tp>()(__m)),
504 static_cast<_Wp>(__ct_abs<_Rp, _Up>()(__n))));
507 template<class _Tp, class _Up>
508 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
509 common_type_t<_Tp,_Up>
510 lcm(_Tp __m, _Up __n)
512 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
513 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" );
514 static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" );
515 if (__m == 0 || __n == 0)
518 using _Rp = common_type_t<_Tp,_Up>;
519 _Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n);
520 _Rp __val2 = __ct_abs<_Rp, _Up>()(__n);
521 _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
522 return __val1 * __val2;
525 #endif /* _LIBCPP_STD_VER > 14 */
527 #if _LIBCPP_STD_VER > 17
529 _LIBCPP_INLINE_VISIBILITY constexpr
530 enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>, _Tp>
531 midpoint(_Tp __a, _Tp __b) noexcept
532 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
534 using _Up = std::make_unsigned_t<_Tp>;
535 constexpr _Up __bitshift = std::numeric_limits<_Up>::digits - 1;
537 _Up __diff = _Up(__b) - _Up(__a);
538 _Up __sign_bit = __b < __a;
540 _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff);
542 return __a + __half_diff;
546 template <class _TPtr>
547 _LIBCPP_INLINE_VISIBILITY constexpr
548 enable_if_t<is_pointer_v<_TPtr>
549 && is_object_v<remove_pointer_t<_TPtr>>
550 && ! is_void_v<remove_pointer_t<_TPtr>>
551 && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr>
552 midpoint(_TPtr __a, _TPtr __b) noexcept
554 return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a);
558 template <typename _Tp>
559 constexpr int __sign(_Tp __val) {
560 return (_Tp(0) < __val) - (__val < _Tp(0));
563 template <typename _Fp>
564 constexpr _Fp __fp_abs(_Fp __f) { return __f >= 0 ? __f : -__f; }
567 _LIBCPP_INLINE_VISIBILITY constexpr
568 enable_if_t<is_floating_point_v<_Fp>, _Fp>
569 midpoint(_Fp __a, _Fp __b) noexcept
571 constexpr _Fp __lo = numeric_limits<_Fp>::min()*2;
572 constexpr _Fp __hi = numeric_limits<_Fp>::max()/2;
573 return __fp_abs(__a) <= __hi && __fp_abs(__b) <= __hi ? // typical case: overflow is impossible
574 (__a + __b)/2 : // always correctly rounded
575 __fp_abs(__a) < __lo ? __a + __b/2 : // not safe to halve a
576 __fp_abs(__b) < __lo ? __a/2 + __b : // not safe to halve b
577 __a/2 + __b/2; // otherwise correctly rounded
580 #endif // _LIBCPP_STD_VER > 17
582 _LIBCPP_END_NAMESPACE_STD
586 #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
587 # include <__pstl_numeric>
590 #endif // _LIBCPP_NUMERIC