55#ifndef _GLIBCXX_NUMERIC
56#define _GLIBCXX_NUMERIC 1
59#pragma GCC system_header
66#ifdef _GLIBCXX_PARALLEL
70#if __cplusplus >= 201402L
76#if __cplusplus >= 201703L
80#if __cplusplus > 201703L
84#define __glibcxx_want_algorithm_iterator_requirements
85#define __glibcxx_want_constexpr_numeric
86#define __glibcxx_want_gcd
87#define __glibcxx_want_gcd_lcm
88#define __glibcxx_want_interpolate
89#define __glibcxx_want_lcm
90#define __glibcxx_want_parallel_algorithm
91#define __glibcxx_want_ranges_iota
92#define __glibcxx_want_saturation_arithmetic
95#if __glibcxx_ranges_iota >= 202202L
99#ifdef __glibcxx_saturation_arithmetic
111namespace std _GLIBCXX_VISIBILITY(default)
113_GLIBCXX_BEGIN_NAMESPACE_VERSION
115#if __cplusplus >= 201402L
120 template<
typename _Res,
typename _Tp>
124 static_assert(
sizeof(_Res) >=
sizeof(_Tp),
125 "result type must be at least as wide as the input type");
129#ifdef _GLIBCXX_ASSERTIONS
130 if (!__is_constant_evaluated())
131 __glibcxx_assert(__val != __gnu_cxx::__int_traits<_Res>::__min);
133 return -
static_cast<_Res
>(__val);
136 template<
typename>
void __abs_r(
bool) =
delete;
139 template<
typename _Tp>
141 __gcd(_Tp __m, _Tp __n)
143 static_assert(is_unsigned<_Tp>::value,
"type must be unsigned");
150 const int __i = std::__countr_zero(__m);
152 const int __j = std::__countr_zero(__n);
154 const int __k = __i < __j ? __i : __j;
170 __n >>= std::__countr_zero(__n);
176#ifdef __cpp_lib_gcd_lcm
178 template<typename _Mn, typename _Nn>
180 gcd(_Mn __m, _Nn __n)
noexcept
182 static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
183 "std::gcd arguments must be integers");
184 static_assert(_Mn(2) == 2 && _Nn(2) == 2,
185 "std::gcd arguments must not be bool");
187 const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
188 const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
189 return __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
193 template<
typename _Mn,
typename _Nn>
195 lcm(_Mn __m, _Nn __n)
noexcept
197 static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
198 "std::lcm arguments must be integers");
199 static_assert(_Mn(2) == 2 && _Nn(2) == 2,
200 "std::lcm arguments must not be bool");
202 const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
203 const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
204 if (__m2 == 0 || __n2 == 0)
206 _Ct __r = __m2 / __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
208 if constexpr (is_signed_v<_Ct>)
209 if (__is_constant_evaluated())
212 bool __overflow = __builtin_mul_overflow(__r, __n2, &__r);
213 __glibcxx_assert(!__overflow);
220#ifdef __cpp_lib_interpolate
221 template<
typename _Tp>
224 __not_<is_same<_Tp, bool>>>,
226 midpoint(_Tp __a, _Tp __b)
noexcept
228 if constexpr (is_integral_v<_Tp>)
241 return __a + __k * _Tp(_Up(__M - __m) / 2);
247 const _Tp __abs_a = __a < 0 ? -__a : __a;
248 const _Tp __abs_b = __b < 0 ? -__b : __b;
249 if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
250 return (__a + __b) / 2;
255 return __a/2 + __b/2;
259 template<
typename _Tp>
261 midpoint(_Tp* __a, _Tp* __b)
noexcept
263 static_assert(
sizeof(_Tp) != 0,
"type must be complete" );
264 return __a + (__b - __a) / 2;
268#if __cplusplus >= 201703L
291 template<
typename _InputIterator,
typename _Tp,
typename _BinaryOperation>
294 reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
295 _BinaryOperation __binary_op)
298 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, __ref>);
299 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, _Tp&>);
300 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, _Tp&>);
301 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, __ref>);
302 if constexpr (__is_any_random_access_iter<_InputIterator>::value)
304 while ((__last - __first) >= 4)
306 _Tp __v1 = __binary_op(__first[0], __first[1]);
307 _Tp __v2 = __binary_op(__first[2], __first[3]);
308 _Tp __v3 = __binary_op(__v1, __v2);
309 __init = __binary_op(__init, __v3);
313 for (; __first != __last; ++__first)
314 __init = __binary_op(__init, *__first);
329 template<
typename _InputIterator,
typename _Tp>
332 reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
346 template<
typename _InputIterator>
348 inline typename iterator_traits<_InputIterator>::value_type
349 reduce(_InputIterator __first, _InputIterator __last)
373 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp,
374 typename _BinaryOperation1,
typename _BinaryOperation2>
378 _InputIterator2 __first2, _Tp __init,
379 _BinaryOperation1 __binary_op1,
380 _BinaryOperation2 __binary_op2)
382 if constexpr (__and_v<__is_any_random_access_iter<_InputIterator1>,
383 __is_any_random_access_iter<_InputIterator2>>)
385 while ((__last1 - __first1) >= 4)
387 _Tp __v1 = __binary_op1(__binary_op2(__first1[0], __first2[0]),
388 __binary_op2(__first1[1], __first2[1]));
389 _Tp __v2 = __binary_op1(__binary_op2(__first1[2], __first2[2]),
390 __binary_op2(__first1[3], __first2[3]));
391 _Tp __v3 = __binary_op1(__v1, __v2);
392 __init = __binary_op1(__init, __v3);
397 for (; __first1 != __last1; ++__first1, (void) ++__first2)
398 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
417 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp>
421 _InputIterator2 __first2, _Tp __init)
442 template<
typename _InputIterator,
typename _Tp,
443 typename _BinaryOperation,
typename _UnaryOperation>
447 _BinaryOperation __binary_op, _UnaryOperation __unary_op)
449 if constexpr (__is_any_random_access_iter<_InputIterator>::value)
451 while ((__last - __first) >= 4)
453 _Tp __v1 = __binary_op(__unary_op(__first[0]),
454 __unary_op(__first[1]));
455 _Tp __v2 = __binary_op(__unary_op(__first[2]),
456 __unary_op(__first[3]));
457 _Tp __v3 = __binary_op(__v1, __v2);
458 __init = __binary_op(__init, __v3);
462 for (; __first != __last; ++__first)
463 __init = __binary_op(__init, __unary_op(*__first));
485 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
486 typename _BinaryOperation>
490 _OutputIterator __result, _Tp __init,
491 _BinaryOperation __binary_op)
493 while (__first != __last)
496 __init = __binary_op(__v, *__first);
520 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp>
522 inline _OutputIterator
524 _OutputIterator __result, _Tp __init)
548 template<
typename _InputIterator,
typename _OutputIterator,
549 typename _BinaryOperation,
typename _Tp>
553 _OutputIterator __result, _BinaryOperation __binary_op,
556 for (; __first != __last; ++__first)
557 *__result++ = __init = __binary_op(__init, *__first);
577 template<
typename _InputIterator,
typename _OutputIterator,
578 typename _BinaryOperation>
582 _OutputIterator __result, _BinaryOperation __binary_op)
584 if (__first != __last)
586 auto __init = _GLIBCXX_ITER_MOVE(__first);
587 *__result++ = __init;
589 if (__first != __last)
611 template<
typename _InputIterator,
typename _OutputIterator>
613 inline _OutputIterator
615 _OutputIterator __result)
638 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
639 typename _BinaryOperation,
typename _UnaryOperation>
643 _OutputIterator __result, _Tp __init,
644 _BinaryOperation __binary_op,
645 _UnaryOperation __unary_op)
647 while (__first != __last)
650 __init = __binary_op(__v, __unary_op(*__first));
677 template<
typename _InputIterator,
typename _OutputIterator,
678 typename _BinaryOperation,
typename _UnaryOperation,
typename _Tp>
682 _OutputIterator __result,
683 _BinaryOperation __binary_op,
684 _UnaryOperation __unary_op,
687 for (; __first != __last; ++__first)
688 *__result++ = __init = __binary_op(__init, __unary_op(*__first));
711 template<
typename _InputIterator,
typename _OutputIterator,
712 typename _BinaryOperation,
typename _UnaryOperation>
716 _OutputIterator __result,
717 _BinaryOperation __binary_op,
718 _UnaryOperation __unary_op)
720 if (__first != __last)
722 auto __init = __unary_op(*__first);
723 *__result++ = __init;
725 if (__first != __last)
727 __binary_op, __unary_op,
736#if __glibcxx_ranges_iota >= 202202L
739 template<
typename _Out,
typename _Tp>
740 using iota_result = out_value_result<_Out, _Tp>;
744 template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent, weakly_incrementable _Tp>
745 requires indirectly_writable<_Out, const _Tp&>
746 constexpr iota_result<_Out, _Tp>
747 operator()(_Out __first, _Sent __last, _Tp __value)
const
749 while (__first != __last)
751 *__first =
static_cast<const _Tp&
>(__value);
758 template<weakly_incrementable _Tp, output_range<const _Tp&> _Range>
759 constexpr iota_result<borrowed_iterator_t<_Range>, _Tp>
760 operator()(_Range&& __r, _Tp __value)
const
761 {
return (*
this)(ranges::begin(__r), ranges::end(__r),
std::move(__value)); }
764 inline constexpr __iota_fn
iota{};
768_GLIBCXX_END_NAMESPACE_VERSION
771#if __cplusplus >= 201703L && _GLIBCXX_HOSTED
773# if _PSTL_EXECUTION_POLICIES_DEFINED
775# include <pstl/glue_numeric_impl.h>
778# include <pstl/glue_numeric_defs.h>
779# 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 void iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value)
Create a range of sequentially increasing values.
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...