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#ifdef __glibcxx_saturation_arithmetic
104namespace std _GLIBCXX_VISIBILITY(default)
106_GLIBCXX_BEGIN_NAMESPACE_VERSION
108#if __cplusplus >= 201402L
113 template<
typename _Res,
typename _Tp>
117 static_assert(
sizeof(_Res) >=
sizeof(_Tp),
118 "result type must be at least as wide as the input type");
122#ifdef _GLIBCXX_ASSERTIONS
123 if (!__is_constant_evaluated())
126 return -
static_cast<_Res
>(__val);
129 template<
typename>
void __abs_r(
bool) =
delete;
132 template<
typename _Tp>
134 __gcd(_Tp __m, _Tp __n)
136 static_assert(is_unsigned<_Tp>::value,
"type must be unsigned");
143 const int __i = std::__countr_zero(__m);
145 const int __j = std::__countr_zero(__n);
147 const int __k = __i < __j ? __i : __j;
163 __n >>= std::__countr_zero(__n);
169#ifdef __cpp_lib_gcd_lcm
171 template<typename _Mn, typename _Nn>
172 constexpr common_type_t<_Mn, _Nn>
173 gcd(_Mn __m, _Nn __n)
noexcept
175 static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
176 "std::gcd arguments must be integers");
177 static_assert(_Mn(2) == 2 && _Nn(2) == 2,
178 "std::gcd arguments must not be bool");
179 using _Ct = common_type_t<_Mn, _Nn>;
180 const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
181 const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
182 return __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
186 template<
typename _Mn,
typename _Nn>
187 constexpr common_type_t<_Mn, _Nn>
188 lcm(_Mn __m, _Nn __n)
noexcept
190 static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
191 "std::lcm arguments must be integers");
192 static_assert(_Mn(2) == 2 && _Nn(2) == 2,
193 "std::lcm arguments must not be bool");
194 using _Ct = common_type_t<_Mn, _Nn>;
195 const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
196 const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
197 if (__m2 == 0 || __n2 == 0)
199 _Ct __r = __m2 / __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
201 if constexpr (is_signed_v<_Ct>)
202 if (__is_constant_evaluated())
205 bool __overflow = __builtin_mul_overflow(__r, __n2, &__r);
206 __glibcxx_assert(!__overflow);
213#ifdef __cpp_lib_interpolate
214 template<
typename _Tp>
216 enable_if_t<__and_v<is_arithmetic<_Tp>, is_same<remove_cv_t<_Tp>, _Tp>,
217 __not_<is_same<_Tp, bool>>>,
219 midpoint(_Tp __a, _Tp __b)
noexcept
221 if constexpr (is_integral_v<_Tp>)
223 using _Up = make_unsigned_t<_Tp>;
234 return __a + __k * _Tp(_Up(__M - __m) / 2);
240 const _Tp __abs_a = __a < 0 ? -__a : __a;
241 const _Tp __abs_b = __b < 0 ? -__b : __b;
242 if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
243 return (__a + __b) / 2;
248 return __a/2 + __b/2;
252 template<
typename _Tp>
253 constexpr enable_if_t<is_object_v<_Tp>, _Tp*>
254 midpoint(_Tp* __a, _Tp* __b)
noexcept
256 static_assert(
sizeof(_Tp) != 0,
"type must be complete" );
257 return __a + (__b - __a) / 2;
261#if __cplusplus >= 201703L
284 template<
typename _InputIterator,
typename _Tp,
typename _BinaryOperation>
287 reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
288 _BinaryOperation __binary_op)
291 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, __ref>);
292 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, _Tp&>);
293 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, _Tp&>);
294 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, __ref>);
295 if constexpr (__is_random_access_iter<_InputIterator>::value)
297 while ((__last - __first) >= 4)
299 _Tp __v1 = __binary_op(__first[0], __first[1]);
300 _Tp __v2 = __binary_op(__first[2], __first[3]);
301 _Tp __v3 = __binary_op(__v1, __v2);
302 __init = __binary_op(__init, __v3);
306 for (; __first != __last; ++__first)
307 __init = __binary_op(__init, *__first);
322 template<
typename _InputIterator,
typename _Tp>
325 reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
339 template<
typename _InputIterator>
341 inline typename iterator_traits<_InputIterator>::value_type
342 reduce(_InputIterator __first, _InputIterator __last)
366 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp,
367 typename _BinaryOperation1,
typename _BinaryOperation2>
371 _InputIterator2 __first2, _Tp __init,
372 _BinaryOperation1 __binary_op1,
373 _BinaryOperation2 __binary_op2)
375 if constexpr (__and_v<__is_random_access_iter<_InputIterator1>,
376 __is_random_access_iter<_InputIterator2>>)
378 while ((__last1 - __first1) >= 4)
380 _Tp __v1 = __binary_op1(__binary_op2(__first1[0], __first2[0]),
381 __binary_op2(__first1[1], __first2[1]));
382 _Tp __v2 = __binary_op1(__binary_op2(__first1[2], __first2[2]),
383 __binary_op2(__first1[3], __first2[3]));
384 _Tp __v3 = __binary_op1(__v1, __v2);
385 __init = __binary_op1(__init, __v3);
390 for (; __first1 != __last1; ++__first1, (void) ++__first2)
391 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
410 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp>
414 _InputIterator2 __first2, _Tp __init)
435 template<
typename _InputIterator,
typename _Tp,
436 typename _BinaryOperation,
typename _UnaryOperation>
440 _BinaryOperation __binary_op, _UnaryOperation __unary_op)
442 if constexpr (__is_random_access_iter<_InputIterator>::value)
444 while ((__last - __first) >= 4)
446 _Tp __v1 = __binary_op(__unary_op(__first[0]),
447 __unary_op(__first[1]));
448 _Tp __v2 = __binary_op(__unary_op(__first[2]),
449 __unary_op(__first[3]));
450 _Tp __v3 = __binary_op(__v1, __v2);
451 __init = __binary_op(__init, __v3);
455 for (; __first != __last; ++__first)
456 __init = __binary_op(__init, __unary_op(*__first));
478 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
479 typename _BinaryOperation>
483 _OutputIterator __result, _Tp __init,
484 _BinaryOperation __binary_op)
486 while (__first != __last)
489 __init = __binary_op(__init, *__first);
513 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp>
515 inline _OutputIterator
517 _OutputIterator __result, _Tp __init)
541 template<
typename _InputIterator,
typename _OutputIterator,
542 typename _BinaryOperation,
typename _Tp>
546 _OutputIterator __result, _BinaryOperation __binary_op,
549 for (; __first != __last; ++__first)
550 *__result++ = __init = __binary_op(__init, *__first);
570 template<
typename _InputIterator,
typename _OutputIterator,
571 typename _BinaryOperation>
575 _OutputIterator __result, _BinaryOperation __binary_op)
577 if (__first != __last)
579 auto __init = *__first;
580 *__result++ = __init;
582 if (__first != __last)
604 template<
typename _InputIterator,
typename _OutputIterator>
606 inline _OutputIterator
608 _OutputIterator __result)
631 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
632 typename _BinaryOperation,
typename _UnaryOperation>
636 _OutputIterator __result, _Tp __init,
637 _BinaryOperation __binary_op,
638 _UnaryOperation __unary_op)
640 while (__first != __last)
643 __init = __binary_op(__init, __unary_op(*__first));
670 template<
typename _InputIterator,
typename _OutputIterator,
671 typename _BinaryOperation,
typename _UnaryOperation,
typename _Tp>
675 _OutputIterator __result,
676 _BinaryOperation __binary_op,
677 _UnaryOperation __unary_op,
680 for (; __first != __last; ++__first)
681 *__result++ = __init = __binary_op(__init, __unary_op(*__first));
704 template<
typename _InputIterator,
typename _OutputIterator,
705 typename _BinaryOperation,
typename _UnaryOperation>
709 _OutputIterator __result,
710 _BinaryOperation __binary_op,
711 _UnaryOperation __unary_op)
713 if (__first != __last)
715 auto __init = __unary_op(*__first);
716 *__result++ = __init;
718 if (__first != __last)
720 __binary_op, __unary_op,
729_GLIBCXX_END_NAMESPACE_VERSION
732#if __cplusplus >= 201703L && _GLIBCXX_HOSTED
734# if _PSTL_EXECUTION_POLICIES_DEFINED
736# include <pstl/glue_numeric_impl.h>
739# include <pstl/glue_numeric_defs.h>
740# define _PSTL_NUMERIC_FORWARD_DECLARED 1
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.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
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...