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
93#define __glibcxx_want_clmul
96#if __glibcxx_ranges_iota >= 202202L
100#ifdef __glibcxx_saturation_arithmetic
112namespace std _GLIBCXX_VISIBILITY(default)
114_GLIBCXX_BEGIN_NAMESPACE_VERSION
116#if __cplusplus >= 201402L
121 template<
typename _Res,
typename _Tp>
125 static_assert(
sizeof(_Res) >=
sizeof(_Tp),
126 "result type must be at least as wide as the input type");
130#ifdef _GLIBCXX_ASSERTIONS
131 if (!__is_constant_evaluated())
132 __glibcxx_assert(__val != __gnu_cxx::__int_traits<_Res>::__min);
134 return -
static_cast<_Res
>(__val);
137 template<
typename>
void __abs_r(
bool) =
delete;
140 template<
typename _Tp>
142 __gcd(_Tp __m, _Tp __n)
144 static_assert(is_unsigned<_Tp>::value,
"type must be unsigned");
151 const int __i = std::__countr_zero(__m);
153 const int __j = std::__countr_zero(__n);
155 const int __k = __i < __j ? __i : __j;
171 __n >>= std::__countr_zero(__n);
177#ifdef __cpp_lib_gcd_lcm
179 template<typename _Mn, typename _Nn>
181 gcd(_Mn __m, _Nn __n)
noexcept
183 static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
184 "std::gcd arguments must be integers");
185 static_assert(_Mn(2) == 2 && _Nn(2) == 2,
186 "std::gcd arguments must not be bool");
188 const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
189 const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
190 return __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
194 template<
typename _Mn,
typename _Nn>
196 lcm(_Mn __m, _Nn __n)
noexcept
198 static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
199 "std::lcm arguments must be integers");
200 static_assert(_Mn(2) == 2 && _Nn(2) == 2,
201 "std::lcm arguments must not be bool");
203 const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
204 const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
205 if (__m2 == 0 || __n2 == 0)
207 _Ct __r = __m2 / __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
209 if constexpr (is_signed_v<_Ct>)
210 if (__is_constant_evaluated())
213 bool __overflow = __builtin_mul_overflow(__r, __n2, &__r);
214 __glibcxx_assert(!__overflow);
221#ifdef __cpp_lib_interpolate
222 template<
typename _Tp>
225 __not_<is_same<_Tp, bool>>>,
227 midpoint(_Tp __a, _Tp __b)
noexcept
229 if constexpr (is_integral_v<_Tp>)
242 return __a + __k * _Tp(_Up(__M - __m) / 2);
248 const _Tp __abs_a = __a < 0 ? -__a : __a;
249 const _Tp __abs_b = __b < 0 ? -__b : __b;
250 if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
251 return (__a + __b) / 2;
256 return __a/2 + __b/2;
260 template<
typename _Tp>
262 midpoint(_Tp* __a, _Tp* __b)
noexcept
264 static_assert(
sizeof(_Tp) != 0,
"type must be complete" );
265 return __a + (__b - __a) / 2;
269#if __cplusplus >= 201703L
292 template<
typename _InputIterator,
typename _Tp,
typename _BinaryOperation>
295 reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
296 _BinaryOperation __binary_op)
299 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, __ref>);
300 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, _Tp&>);
301 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, _Tp&>);
302 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, __ref>);
303 if constexpr (__is_any_random_access_iter<_InputIterator>::value)
305 while ((__last - __first) >= 4)
307 _Tp __v1 = __binary_op(__first[0], __first[1]);
308 _Tp __v2 = __binary_op(__first[2], __first[3]);
309 _Tp __v3 = __binary_op(__v1, __v2);
310 __init = __binary_op(__init, __v3);
314 for (; __first != __last; ++__first)
315 __init = __binary_op(__init, *__first);
330 template<
typename _InputIterator,
typename _Tp>
333 reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
347 template<
typename _InputIterator>
349 inline typename iterator_traits<_InputIterator>::value_type
350 reduce(_InputIterator __first, _InputIterator __last)
374 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp,
375 typename _BinaryOperation1,
typename _BinaryOperation2>
379 _InputIterator2 __first2, _Tp __init,
380 _BinaryOperation1 __binary_op1,
381 _BinaryOperation2 __binary_op2)
383 if constexpr (__and_v<__is_any_random_access_iter<_InputIterator1>,
384 __is_any_random_access_iter<_InputIterator2>>)
386 while ((__last1 - __first1) >= 4)
388 _Tp __v1 = __binary_op1(__binary_op2(__first1[0], __first2[0]),
389 __binary_op2(__first1[1], __first2[1]));
390 _Tp __v2 = __binary_op1(__binary_op2(__first1[2], __first2[2]),
391 __binary_op2(__first1[3], __first2[3]));
392 _Tp __v3 = __binary_op1(__v1, __v2);
393 __init = __binary_op1(__init, __v3);
398 for (; __first1 != __last1; ++__first1, (void) ++__first2)
399 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
418 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp>
422 _InputIterator2 __first2, _Tp __init)
443 template<
typename _InputIterator,
typename _Tp,
444 typename _BinaryOperation,
typename _UnaryOperation>
448 _BinaryOperation __binary_op, _UnaryOperation __unary_op)
450 if constexpr (__is_any_random_access_iter<_InputIterator>::value)
452 while ((__last - __first) >= 4)
454 _Tp __v1 = __binary_op(__unary_op(__first[0]),
455 __unary_op(__first[1]));
456 _Tp __v2 = __binary_op(__unary_op(__first[2]),
457 __unary_op(__first[3]));
458 _Tp __v3 = __binary_op(__v1, __v2);
459 __init = __binary_op(__init, __v3);
463 for (; __first != __last; ++__first)
464 __init = __binary_op(__init, __unary_op(*__first));
486 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
487 typename _BinaryOperation>
491 _OutputIterator __result, _Tp __init,
492 _BinaryOperation __binary_op)
494 while (__first != __last)
497 __init = __binary_op(__v, *__first);
521 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp>
523 inline _OutputIterator
525 _OutputIterator __result, _Tp __init)
549 template<
typename _InputIterator,
typename _OutputIterator,
550 typename _BinaryOperation,
typename _Tp>
554 _OutputIterator __result, _BinaryOperation __binary_op,
557 for (; __first != __last; ++__first)
558 *__result++ = __init = __binary_op(__init, *__first);
578 template<
typename _InputIterator,
typename _OutputIterator,
579 typename _BinaryOperation>
583 _OutputIterator __result, _BinaryOperation __binary_op)
585 if (__first != __last)
587 auto __init = _GLIBCXX_ITER_MOVE(__first);
588 *__result++ = __init;
590 if (__first != __last)
612 template<
typename _InputIterator,
typename _OutputIterator>
614 inline _OutputIterator
616 _OutputIterator __result)
639 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
640 typename _BinaryOperation,
typename _UnaryOperation>
644 _OutputIterator __result, _Tp __init,
645 _BinaryOperation __binary_op,
646 _UnaryOperation __unary_op)
648 while (__first != __last)
651 __init = __binary_op(__v, __unary_op(*__first));
678 template<
typename _InputIterator,
typename _OutputIterator,
679 typename _BinaryOperation,
typename _UnaryOperation,
typename _Tp>
683 _OutputIterator __result,
684 _BinaryOperation __binary_op,
685 _UnaryOperation __unary_op,
688 for (; __first != __last; ++__first)
689 *__result++ = __init = __binary_op(__init, __unary_op(*__first));
712 template<
typename _InputIterator,
typename _OutputIterator,
713 typename _BinaryOperation,
typename _UnaryOperation>
717 _OutputIterator __result,
718 _BinaryOperation __binary_op,
719 _UnaryOperation __unary_op)
721 if (__first != __last)
723 auto __init = __unary_op(*__first);
724 *__result++ = __init;
726 if (__first != __last)
728 __binary_op, __unary_op,
737#if __glibcxx_ranges_iota >= 202202L
740 template<
typename _Out,
typename _Tp>
741 using iota_result = out_value_result<_Out, _Tp>;
745 template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent, weakly_incrementable _Tp>
746 requires indirectly_writable<_Out, const _Tp&>
747 constexpr iota_result<_Out, _Tp>
748 operator()(_Out __first, _Sent __last, _Tp __value)
const
750 while (__first != __last)
752 *__first =
static_cast<const _Tp&
>(__value);
759 template<weakly_incrementable _Tp, output_range<const _Tp&> _Range>
760 constexpr iota_result<borrowed_iterator_t<_Range>, _Tp>
761 operator()(_Range&& __r, _Tp __value)
const
762 {
return (*
this)(ranges::begin(__r), ranges::end(__r),
std::move(__value)); }
765 inline constexpr __iota_fn iota{};
769#ifdef __glibcxx_clmul
770 template<
typename _Tp>
773 static_assert(__is_unsigned_integer<_Tp>::value);
774 constexpr _Tp high() const noexcept {
return _M_high; }
775 constexpr _Tp low() const noexcept {
return _M_low; }
777 friend constexpr bool operator==(
const wide_result&,
778 const wide_result&) =
default;
779#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
780 constexpr wide_result(_Tp __high, _Tp __low) noexcept
781 : _M_low(__low), _M_high(__high) {}
782 friend constexpr strong_ordering operator<=>(
const wide_result& __x,
783 const wide_result& __y)
786 if (
auto __r = __x._M_high <=> __y._M_high; __r != 0)
788 return __x._M_low <=> __y._M_low;
793 constexpr wide_result(_Tp __high, _Tp __low) noexcept
794 : _M_high(__high), _M_low(__low) {}
795 friend constexpr strong_ordering operator<=>(
const wide_result& __x,
796 const wide_result& __y)
803 template<__
unsigned_
integer _Tp>
804 [[nodiscard]]
constexpr wide_result<_Tp>
805 widening_clmul(_Tp __x, _Tp __y)
noexcept
808#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_clmulg)
809 __low = __builtin_clmulg(__x, __y, &__high);
810 return wide_result<_Tp>(__high, __low);
811#elif _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_elementwise_clmul) \
812 && _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_bitreverseg)
813 __low = __builtin_elementwise_clmul(__x, __y);
814 __high = __builtin_elementwise_clmul(__builtin_bitreverseg(__x),
815 __builtin_bitreverseg(__y));
816 return wide_result<_Tp>(__builtin_bitreverseg(__high) >> 1, __low);
818 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
820 __low = (__y & _Tp(1)) ? __x : 0;
821 for (
int __i = 1; __i < _Nd; ++__i)
822 if ((__y >> __i) & _Tp(1))
825 __high ^= __x >> (_Nd - __i);
827 return wide_result<_Tp>(__high, __low);
831 template<__
unsigned_
integer _Tp>
832 [[nodiscard]]
constexpr _Tp
833 clmul(_Tp __x, _Tp __y)
noexcept
835#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_clmulg)
836 return __builtin_clmulg(__x, __y);
837#elif _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_elementwise_clmul)
838 return __builtin_elementwise_clmul(__x, __y);
841 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
842 for (
int __i = 0; __i < _Nd; ++__i)
843 if ((__y >> __i) & _Tp(1))
850_GLIBCXX_END_NAMESPACE_VERSION
853#if __cpp_lib_parallel_algorithm
855# if _PSTL_EXECUTION_POLICIES_DEFINED
857# include <pstl/glue_numeric_impl.h>
860# include <pstl/glue_numeric_defs.h>
861# 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...