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
90namespace std _GLIBCXX_VISIBILITY(default)
92_GLIBCXX_BEGIN_NAMESPACE_VERSION
94#if __cplusplus >= 201402L
99 template<
typename _Res,
typename _Tp>
103 static_assert(
sizeof(_Res) >=
sizeof(_Tp),
104 "result type must be at least as wide as the input type");
108#if defined _GLIBCXX_ASSERTIONS && defined _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
109 if (!__builtin_is_constant_evaluated())
112 return -
static_cast<_Res
>(__val);
115 template<
typename>
void __abs_r(
bool) =
delete;
118 template<
typename _Tp>
120 __gcd(_Tp __m, _Tp __n)
122 static_assert(is_unsigned<_Tp>::value,
"type must be unsigned");
129 const int __i = std::__countr_zero(__m);
131 const int __j = std::__countr_zero(__n);
133 const int __k = __i < __j ? __i : __j;
149 __n >>= std::__countr_zero(__n);
154#if __cplusplus >= 201703L
156#define __cpp_lib_gcd_lcm 201606
158#define __cpp_lib_gcd 201606
159#define __cpp_lib_lcm 201606
162 template<
typename _Mn,
typename _Nn>
163 constexpr common_type_t<_Mn, _Nn>
164 gcd(_Mn __m, _Nn __n)
noexcept
166 static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
167 "std::gcd arguments must be integers");
168 static_assert(_Mn(2) == 2 && _Nn(2) == 2,
169 "std::gcd arguments must not be bool");
171 const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
172 const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
173 return __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
177 template<
typename _Mn,
typename _Nn>
178 constexpr common_type_t<_Mn, _Nn>
179 lcm(_Mn __m, _Nn __n)
noexcept
181 static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
182 "std::lcm arguments must be integers");
183 static_assert(_Mn(2) == 2 && _Nn(2) == 2,
184 "std::lcm arguments must not be bool");
186 const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
187 const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
188 if (__m2 == 0 || __n2 == 0)
190 _Ct __r = __m2 / __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
192#if defined _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
193 if constexpr (is_signed_v<_Ct>)
194 if (__builtin_is_constant_evaluated())
198 bool __overflow = __builtin_mul_overflow(__r, __n2, &__r);
199 __glibcxx_assert(!__overflow);
206#if __cplusplus > 201703L
209# define __cpp_lib_interpolate 201902L
211 template<
typename _Tp>
213 enable_if_t<__and_v<is_arithmetic<_Tp>, is_same<remove_cv_t<_Tp>, _Tp>,
214 __not_<is_same<_Tp, bool>>>,
216 midpoint(_Tp __a, _Tp __b)
noexcept
218 if constexpr (is_integral_v<_Tp>)
220 using _Up = make_unsigned_t<_Tp>;
231 return __a + __k * _Tp(_Up(__M - __m) / 2);
237 const _Tp __abs_a = __a < 0 ? -__a : __a;
238 const _Tp __abs_b = __b < 0 ? -__b : __b;
239 if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
240 return (__a + __b) / 2;
245 return __a/2 + __b/2;
249 template<
typename _Tp>
250 constexpr enable_if_t<is_object_v<_Tp>, _Tp*>
251 midpoint(_Tp* __a, _Tp* __b)
noexcept
253 static_assert(
sizeof(_Tp) != 0,
"type must be complete" );
254 return __a + (__b - __a) / 2;
258#if __cplusplus >= 201703L
260#if __cplusplus > 201703L
261#define __cpp_lib_constexpr_numeric 201911L
286 template<
typename _InputIterator,
typename _Tp,
typename _BinaryOperation>
289 reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
290 _BinaryOperation __binary_op)
293 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, __ref>);
294 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, _Tp&>);
295 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, _Tp&>);
296 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, __ref>);
297 if constexpr (__is_random_access_iter<_InputIterator>::value)
299 while ((__last - __first) >= 4)
301 _Tp __v1 = __binary_op(__first[0], __first[1]);
302 _Tp __v2 = __binary_op(__first[2], __first[3]);
303 _Tp __v3 = __binary_op(__v1, __v2);
304 __init = __binary_op(__init, __v3);
308 for (; __first != __last; ++__first)
309 __init = __binary_op(__init, *__first);
324 template<
typename _InputIterator,
typename _Tp>
327 reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
341 template<
typename _InputIterator>
343 inline typename iterator_traits<_InputIterator>::value_type
344 reduce(_InputIterator __first, _InputIterator __last)
368 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp,
369 typename _BinaryOperation1,
typename _BinaryOperation2>
373 _InputIterator2 __first2, _Tp __init,
374 _BinaryOperation1 __binary_op1,
375 _BinaryOperation2 __binary_op2)
377 if constexpr (__and_v<__is_random_access_iter<_InputIterator1>,
378 __is_random_access_iter<_InputIterator2>>)
380 while ((__last1 - __first1) >= 4)
382 _Tp __v1 = __binary_op1(__binary_op2(__first1[0], __first2[0]),
383 __binary_op2(__first1[1], __first2[1]));
384 _Tp __v2 = __binary_op1(__binary_op2(__first1[2], __first2[2]),
385 __binary_op2(__first1[3], __first2[3]));
386 _Tp __v3 = __binary_op1(__v1, __v2);
387 __init = __binary_op1(__init, __v3);
392 for (; __first1 != __last1; ++__first1, (void) ++__first2)
393 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
412 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp>
416 _InputIterator2 __first2, _Tp __init)
437 template<
typename _InputIterator,
typename _Tp,
438 typename _BinaryOperation,
typename _UnaryOperation>
442 _BinaryOperation __binary_op, _UnaryOperation __unary_op)
444 if constexpr (__is_random_access_iter<_InputIterator>::value)
446 while ((__last - __first) >= 4)
448 _Tp __v1 = __binary_op(__unary_op(__first[0]),
449 __unary_op(__first[1]));
450 _Tp __v2 = __binary_op(__unary_op(__first[2]),
451 __unary_op(__first[3]));
452 _Tp __v3 = __binary_op(__v1, __v2);
453 __init = __binary_op(__init, __v3);
457 for (; __first != __last; ++__first)
458 __init = __binary_op(__init, __unary_op(*__first));
480 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
481 typename _BinaryOperation>
485 _OutputIterator __result, _Tp __init,
486 _BinaryOperation __binary_op)
488 while (__first != __last)
491 __init = __binary_op(__init, *__first);
515 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp>
517 inline _OutputIterator
519 _OutputIterator __result, _Tp __init)
543 template<
typename _InputIterator,
typename _OutputIterator,
544 typename _BinaryOperation,
typename _Tp>
548 _OutputIterator __result, _BinaryOperation __binary_op,
551 for (; __first != __last; ++__first)
552 *__result++ = __init = __binary_op(__init, *__first);
572 template<
typename _InputIterator,
typename _OutputIterator,
573 typename _BinaryOperation>
577 _OutputIterator __result, _BinaryOperation __binary_op)
579 if (__first != __last)
581 auto __init = *__first;
582 *__result++ = __init;
584 if (__first != __last)
606 template<
typename _InputIterator,
typename _OutputIterator>
608 inline _OutputIterator
610 _OutputIterator __result)
633 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
634 typename _BinaryOperation,
typename _UnaryOperation>
638 _OutputIterator __result, _Tp __init,
639 _BinaryOperation __binary_op,
640 _UnaryOperation __unary_op)
642 while (__first != __last)
645 __init = __binary_op(__init, __unary_op(*__first));
672 template<
typename _InputIterator,
typename _OutputIterator,
673 typename _BinaryOperation,
typename _UnaryOperation,
typename _Tp>
677 _OutputIterator __result,
678 _BinaryOperation __binary_op,
679 _UnaryOperation __unary_op,
682 for (; __first != __last; ++__first)
683 *__result++ = __init = __binary_op(__init, __unary_op(*__first));
706 template<
typename _InputIterator,
typename _OutputIterator,
707 typename _BinaryOperation,
typename _UnaryOperation>
711 _OutputIterator __result,
712 _BinaryOperation __binary_op,
713 _UnaryOperation __unary_op)
715 if (__first != __last)
717 auto __init = __unary_op(*__first);
718 *__result++ = __init;
720 if (__first != __last)
722 __binary_op, __unary_op,
731_GLIBCXX_END_NAMESPACE_VERSION
734#if __cplusplus >= 201703L
736# if _PSTL_EXECUTION_POLICIES_DEFINED
738# include <pstl/glue_numeric_impl.h>
741# include <pstl/glue_numeric_defs.h>
742# define _PSTL_NUMERIC_FORWARD_DECLARED 1
746# define __cpp_lib_parallel_algorithm 201603L
typename common_type< _Tp... >::type common_type_t
Alias template for common_type.
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.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
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...