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#define __glibcxx_want_constexpr_numeric
83#define __glibcxx_want_gcd
84#define __glibcxx_want_gcd_lcm
85#define __glibcxx_want_interpolate
86#define __glibcxx_want_lcm
87#define __glibcxx_want_parallel_algorithm
88#define __glibcxx_want_ranges_iota
89#define __glibcxx_want_saturation_arithmetic
92#if __glibcxx_ranges_iota >= 202202L
96#ifdef __glibcxx_saturation_arithmetic
108namespace std _GLIBCXX_VISIBILITY(default)
110_GLIBCXX_BEGIN_NAMESPACE_VERSION
112#if __cplusplus >= 201402L
117 template<
typename _Res,
typename _Tp>
121 static_assert(
sizeof(_Res) >=
sizeof(_Tp),
122 "result type must be at least as wide as the input type");
126#ifdef _GLIBCXX_ASSERTIONS
127 if (!__is_constant_evaluated())
128 __glibcxx_assert(__val != __gnu_cxx::__int_traits<_Res>::__min);
130 return -
static_cast<_Res
>(__val);
133 template<
typename>
void __abs_r(
bool) =
delete;
136 template<
typename _Tp>
138 __gcd(_Tp __m, _Tp __n)
140 static_assert(is_unsigned<_Tp>::value,
"type must be unsigned");
147 const int __i = std::__countr_zero(__m);
149 const int __j = std::__countr_zero(__n);
151 const int __k = __i < __j ? __i : __j;
167 __n >>= std::__countr_zero(__n);
173#ifdef __cpp_lib_gcd_lcm
175 template<typename _Mn, typename _Nn>
177 gcd(_Mn __m, _Nn __n)
noexcept
179 static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
180 "std::gcd arguments must be integers");
181 static_assert(_Mn(2) == 2 && _Nn(2) == 2,
182 "std::gcd arguments must not be bool");
184 const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
185 const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
186 return __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
190 template<
typename _Mn,
typename _Nn>
192 lcm(_Mn __m, _Nn __n)
noexcept
194 static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
195 "std::lcm arguments must be integers");
196 static_assert(_Mn(2) == 2 && _Nn(2) == 2,
197 "std::lcm arguments must not be bool");
199 const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
200 const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
201 if (__m2 == 0 || __n2 == 0)
203 _Ct __r = __m2 / __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
205 if constexpr (is_signed_v<_Ct>)
206 if (__is_constant_evaluated())
209 bool __overflow = __builtin_mul_overflow(__r, __n2, &__r);
210 __glibcxx_assert(!__overflow);
217#ifdef __cpp_lib_interpolate
218 template<
typename _Tp>
221 __not_<is_same<_Tp, bool>>>,
223 midpoint(_Tp __a, _Tp __b)
noexcept
225 if constexpr (is_integral_v<_Tp>)
238 return __a + __k * _Tp(_Up(__M - __m) / 2);
244 const _Tp __abs_a = __a < 0 ? -__a : __a;
245 const _Tp __abs_b = __b < 0 ? -__b : __b;
246 if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
247 return (__a + __b) / 2;
252 return __a/2 + __b/2;
256 template<
typename _Tp>
258 midpoint(_Tp* __a, _Tp* __b)
noexcept
260 static_assert(
sizeof(_Tp) != 0,
"type must be complete" );
261 return __a + (__b - __a) / 2;
265#if __cplusplus >= 201703L
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
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.
Implementation details not part of the namespace std interface.
constexpr common_type_t< _Mn, _Nn > lcm(_Mn __m, _Nn __n)
Least common multiple.
constexpr common_type_t< _Mn, _Nn > gcd(_Mn __m, _Nn __n) noexcept
Greatest common divisor.
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...