55#ifndef _GLIBCXX_NUMERIC
56#define _GLIBCXX_NUMERIC 1
58#pragma GCC system_header
64#ifdef _GLIBCXX_PARALLEL
68#if __cplusplus >= 201402L
74#if __cplusplus >= 201703L
78#if __cplusplus > 201703L
82#if __cplusplus > 202002L
94namespace std _GLIBCXX_VISIBILITY(default)
96_GLIBCXX_BEGIN_NAMESPACE_VERSION
98#if __cplusplus >= 201402L
103 template<
typename _Res,
typename _Tp>
107 static_assert(
sizeof(_Res) >=
sizeof(_Tp),
108 "result type must be at least as wide as the input type");
112#ifdef _GLIBCXX_ASSERTIONS
113 if (!__is_constant_evaluated())
114 __glibcxx_assert(__val != __gnu_cxx::__int_traits<_Res>::__min);
116 return -
static_cast<_Res
>(__val);
119 template<
typename>
void __abs_r(
bool) =
delete;
122 template<
typename _Tp>
124 __gcd(_Tp __m, _Tp __n)
126 static_assert(is_unsigned<_Tp>::value,
"type must be unsigned");
133 const int __i = std::__countr_zero(__m);
135 const int __j = std::__countr_zero(__n);
137 const int __k = __i < __j ? __i : __j;
153 __n >>= std::__countr_zero(__n);
158#if __cplusplus >= 201703L
160#define __cpp_lib_gcd_lcm 201606L
162#define __cpp_lib_gcd 201606L
163#define __cpp_lib_lcm 201606L
166 template<
typename _Mn,
typename _Nn>
168 gcd(_Mn __m, _Nn __n)
noexcept
170 static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
171 "std::gcd arguments must be integers");
172 static_assert(_Mn(2) == 2 && _Nn(2) == 2,
173 "std::gcd arguments must not be bool");
175 const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
176 const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
177 return __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
181 template<
typename _Mn,
typename _Nn>
183 lcm(_Mn __m, _Nn __n)
noexcept
185 static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
186 "std::lcm arguments must be integers");
187 static_assert(_Mn(2) == 2 && _Nn(2) == 2,
188 "std::lcm arguments must not be bool");
190 const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
191 const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
192 if (__m2 == 0 || __n2 == 0)
194 _Ct __r = __m2 / __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
196 if constexpr (is_signed_v<_Ct>)
197 if (__is_constant_evaluated())
200 bool __overflow = __builtin_mul_overflow(__r, __n2, &__r);
201 __glibcxx_assert(!__overflow);
208#if __cplusplus > 201703L
211# define __cpp_lib_interpolate 201902L
213 template<
typename _Tp>
216 __not_<is_same<_Tp, bool>>>,
218 midpoint(_Tp __a, _Tp __b)
noexcept
220 if constexpr (is_integral_v<_Tp>)
233 return __a + __k * _Tp(_Up(__M - __m) / 2);
239 const _Tp __abs_a = __a < 0 ? -__a : __a;
240 const _Tp __abs_b = __b < 0 ? -__b : __b;
241 if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
242 return (__a + __b) / 2;
247 return __a/2 + __b/2;
251 template<
typename _Tp>
253 midpoint(_Tp* __a, _Tp* __b)
noexcept
255 static_assert(
sizeof(_Tp) != 0,
"type must be complete" );
256 return __a + (__b - __a) / 2;
260#if __cplusplus >= 201703L
262#if __cplusplus > 201703L
263#define __cpp_lib_constexpr_numeric 201911L
288 template<
typename _InputIterator,
typename _Tp,
typename _BinaryOperation>
291 reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
292 _BinaryOperation __binary_op)
295 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, __ref>);
296 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, _Tp&>);
297 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, _Tp&>);
298 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, __ref>);
299 if constexpr (__is_random_access_iter<_InputIterator>::value)
301 while ((__last - __first) >= 4)
303 _Tp __v1 = __binary_op(__first[0], __first[1]);
304 _Tp __v2 = __binary_op(__first[2], __first[3]);
305 _Tp __v3 = __binary_op(__v1, __v2);
306 __init = __binary_op(__init, __v3);
310 for (; __first != __last; ++__first)
311 __init = __binary_op(__init, *__first);
326 template<
typename _InputIterator,
typename _Tp>
329 reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
343 template<
typename _InputIterator>
345 inline typename iterator_traits<_InputIterator>::value_type
346 reduce(_InputIterator __first, _InputIterator __last)
370 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp,
371 typename _BinaryOperation1,
typename _BinaryOperation2>
375 _InputIterator2 __first2, _Tp __init,
376 _BinaryOperation1 __binary_op1,
377 _BinaryOperation2 __binary_op2)
379 if constexpr (__and_v<__is_random_access_iter<_InputIterator1>,
380 __is_random_access_iter<_InputIterator2>>)
382 while ((__last1 - __first1) >= 4)
384 _Tp __v1 = __binary_op1(__binary_op2(__first1[0], __first2[0]),
385 __binary_op2(__first1[1], __first2[1]));
386 _Tp __v2 = __binary_op1(__binary_op2(__first1[2], __first2[2]),
387 __binary_op2(__first1[3], __first2[3]));
388 _Tp __v3 = __binary_op1(__v1, __v2);
389 __init = __binary_op1(__init, __v3);
394 for (; __first1 != __last1; ++__first1, (void) ++__first2)
395 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
414 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp>
418 _InputIterator2 __first2, _Tp __init)
439 template<
typename _InputIterator,
typename _Tp,
440 typename _BinaryOperation,
typename _UnaryOperation>
444 _BinaryOperation __binary_op, _UnaryOperation __unary_op)
446 if constexpr (__is_random_access_iter<_InputIterator>::value)
448 while ((__last - __first) >= 4)
450 _Tp __v1 = __binary_op(__unary_op(__first[0]),
451 __unary_op(__first[1]));
452 _Tp __v2 = __binary_op(__unary_op(__first[2]),
453 __unary_op(__first[3]));
454 _Tp __v3 = __binary_op(__v1, __v2);
455 __init = __binary_op(__init, __v3);
459 for (; __first != __last; ++__first)
460 __init = __binary_op(__init, __unary_op(*__first));
482 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
483 typename _BinaryOperation>
487 _OutputIterator __result, _Tp __init,
488 _BinaryOperation __binary_op)
490 while (__first != __last)
493 __init = __binary_op(__v, *__first);
517 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp>
519 inline _OutputIterator
521 _OutputIterator __result, _Tp __init)
545 template<
typename _InputIterator,
typename _OutputIterator,
546 typename _BinaryOperation,
typename _Tp>
550 _OutputIterator __result, _BinaryOperation __binary_op,
553 for (; __first != __last; ++__first)
554 *__result++ = __init = __binary_op(__init, *__first);
574 template<
typename _InputIterator,
typename _OutputIterator,
575 typename _BinaryOperation>
579 _OutputIterator __result, _BinaryOperation __binary_op)
581 if (__first != __last)
583 auto __init = *__first;
584 *__result++ = __init;
586 if (__first != __last)
608 template<
typename _InputIterator,
typename _OutputIterator>
610 inline _OutputIterator
612 _OutputIterator __result)
635 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
636 typename _BinaryOperation,
typename _UnaryOperation>
640 _OutputIterator __result, _Tp __init,
641 _BinaryOperation __binary_op,
642 _UnaryOperation __unary_op)
644 while (__first != __last)
647 __init = __binary_op(__init, __unary_op(*__first));
674 template<
typename _InputIterator,
typename _OutputIterator,
675 typename _BinaryOperation,
typename _UnaryOperation,
typename _Tp>
679 _OutputIterator __result,
680 _BinaryOperation __binary_op,
681 _UnaryOperation __unary_op,
684 for (; __first != __last; ++__first)
685 *__result++ = __init = __binary_op(__init, __unary_op(*__first));
708 template<
typename _InputIterator,
typename _OutputIterator,
709 typename _BinaryOperation,
typename _UnaryOperation>
713 _OutputIterator __result,
714 _BinaryOperation __binary_op,
715 _UnaryOperation __unary_op)
717 if (__first != __last)
719 auto __init = __unary_op(*__first);
720 *__result++ = __init;
722 if (__first != __last)
724 __binary_op, __unary_op,
733_GLIBCXX_END_NAMESPACE_VERSION
736#if __cplusplus >= 201703L && _GLIBCXX_HOSTED
738# if _PSTL_EXECUTION_POLICIES_DEFINED
740# include <pstl/glue_numeric_impl.h>
743# include <pstl/glue_numeric_defs.h>
744# define _PSTL_NUMERIC_FORWARD_DECLARED 1
748# define __cpp_lib_parallel_algorithm 201603L
typename common_type< _Tp... >::type common_type_t
Alias template for common_type.
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op, _Tp __init)
Output the cumulative sum of one range to a second range.
constexpr _OutputIterator exclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init, _BinaryOperation __binary_op)
Output the cumulative sum of one range to a second range.
constexpr _OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryOperation __binary_op, _UnaryOperation __unary_op, _Tp __init)
Output the cumulative sum of one range to a second range.
constexpr _OutputIterator transform_exclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init, _BinaryOperation __binary_op, _UnaryOperation __unary_op)
Output the cumulative sum of one range to a second range.
constexpr _Tp transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
Combine elements from two ranges and reduce.
constexpr _Tp reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
Calculate reduction of values in a range.
ISO C++ entities toplevel namespace is std.
constexpr common_type_t< _Mn, _Nn > lcm(_Mn __m, _Nn __n) noexcept
Least common multiple.
constexpr common_type_t< _Mn, _Nn > gcd(_Mn __m, _Nn __n) noexcept
Greatest common divisor.
Implementation details not part of the namespace std interface.
static constexpr _Tp max() noexcept
static constexpr _Tp min() noexcept
Traits class for iterators.
One of the math functors.
One of the math functors.
Parallel STL function calls corresponding to stl_numeric.h. The functions defined here mainly do case...