55 #ifndef _GLIBCXX_NUMERIC
56 #define _GLIBCXX_NUMERIC 1
58 #pragma GCC system_header
64 #ifdef _GLIBCXX_PARALLEL
68 #if __cplusplus >= 201402L
73 #if __cplusplus >= 201703L
77 #if __cplusplus > 201703L
89 namespace std _GLIBCXX_VISIBILITY(default)
91 _GLIBCXX_BEGIN_NAMESPACE_VERSION
93 #if __cplusplus >= 201402L
98 template<
typename _Up,
typename _Tp>
102 static_assert(is_unsigned<_Up>::value,
"result type must be unsigned");
103 static_assert(
sizeof(_Up) >=
sizeof(_Tp),
104 "result type must be at least as wide as the input type");
105 return __val < 0 ? -(_Up)__val : (_Up)__val;
108 template<typename _Up>
void __absu(
bool) = delete;
111 template<typename _Tp>
113 __gcd(_Tp __m, _Tp __n)
115 static_assert(is_unsigned<_Tp>::value,
"type must be unsigned");
122 const int __i = std::__countr_zero(__m);
124 const int __j = std::__countr_zero(__n);
126 const int __k = __i < __j ? __i : __j;
142 __n >>= std::__countr_zero(__n);
147 template<
typename _Tp>
149 __lcm(_Tp __m, _Tp __n)
151 return (__m != 0 && __n != 0)
152 ? (__m / __detail::__gcd(__m, __n)) * __n
157 #if __cplusplus >= 201703L
159 #define __cpp_lib_gcd_lcm 201606
161 #define __cpp_lib_gcd 201606
162 #define __cpp_lib_lcm 201606
165 template<
typename _Mn,
typename _Nn>
166 constexpr common_type_t<_Mn, _Nn>
167 gcd(_Mn __m, _Nn __n) noexcept
169 static_assert(is_integral_v<_Mn>,
"std::gcd arguments must be integers");
170 static_assert(is_integral_v<_Nn>,
"std::gcd arguments must be integers");
171 static_assert(_Mn(2) != _Mn(1),
"std::gcd arguments must not be bool");
172 static_assert(_Nn(2) != _Nn(1),
"std::gcd arguments must not be bool");
174 return __detail::__gcd(__detail::__absu<_Up>(__m),
175 __detail::__absu<_Up>(__n));
179 template<
typename _Mn,
typename _Nn>
180 constexpr common_type_t<_Mn, _Nn>
181 lcm(_Mn __m, _Nn __n) noexcept
183 static_assert(is_integral_v<_Mn>,
"std::lcm arguments must be integers");
184 static_assert(is_integral_v<_Nn>,
"std::lcm arguments must be integers");
185 static_assert(_Mn(2) == 2,
"std::lcm arguments must not be bool");
186 static_assert(_Nn(2) == 2,
"std::lcm arguments must not be bool");
188 return __detail::__lcm(__detail::__absu<_Up>(__m),
189 __detail::__absu<_Up>(__n));
195 #if __cplusplus > 201703L
198 # define __cpp_lib_interpolate 201902L
200 template<
typename _Tp>
202 enable_if_t<__and_v<is_arithmetic<_Tp>, is_same<remove_cv_t<_Tp>, _Tp>,
203 __not_<is_same<_Tp, bool>>>,
205 midpoint(_Tp __a, _Tp __b) noexcept
207 if constexpr (is_integral_v<_Tp>)
209 using _Up = make_unsigned_t<_Tp>;
220 return __a + __k * _Tp(_Up(__M - __m) / 2);
226 const _Tp __abs_a = __a < 0 ? -__a : __a;
227 const _Tp __abs_b = __b < 0 ? -__b : __b;
228 if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
229 return (__a + __b) / 2;
234 return __a/2 + __b/2;
238 template<
typename _Tp>
239 constexpr enable_if_t<is_object_v<_Tp>, _Tp*>
240 midpoint(_Tp* __a, _Tp* __b) noexcept
242 static_assert(
sizeof(_Tp) != 0,
"type must be complete" );
243 return __a + (__b - __a) / 2;
247 #if __cplusplus >= 201703L
249 #if __cplusplus > 201703L
250 #define __cpp_lib_constexpr_numeric 201911L
275 template<
typename _InputIterator,
typename _Tp,
typename _BinaryOperation>
278 reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
279 _BinaryOperation __binary_op)
282 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, __ref>);
283 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, _Tp&>);
284 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, _Tp&>);
285 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, __ref>);
286 if constexpr (__is_random_access_iter<_InputIterator>::value)
288 while ((__last - __first) >= 4)
290 _Tp __v1 = __binary_op(__first[0], __first[1]);
291 _Tp __v2 = __binary_op(__first[2], __first[3]);
292 _Tp __v3 = __binary_op(__v1, __v2);
293 __init = __binary_op(__init, __v3);
297 for (; __first != __last; ++__first)
298 __init = __binary_op(__init, *__first);
313 template<
typename _InputIterator,
typename _Tp>
316 reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
330 template<
typename _InputIterator>
332 inline typename iterator_traits<_InputIterator>::value_type
333 reduce(_InputIterator __first, _InputIterator __last)
357 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp,
358 typename _BinaryOperation1,
typename _BinaryOperation2>
362 _InputIterator2 __first2, _Tp __init,
363 _BinaryOperation1 __binary_op1,
364 _BinaryOperation2 __binary_op2)
366 if constexpr (__and_v<__is_random_access_iter<_InputIterator1>,
367 __is_random_access_iter<_InputIterator2>>)
369 while ((__last1 - __first1) >= 4)
371 _Tp __v1 = __binary_op1(__binary_op2(__first1[0], __first2[0]),
372 __binary_op2(__first1[1], __first2[1]));
373 _Tp __v2 = __binary_op1(__binary_op2(__first1[2], __first2[2]),
374 __binary_op2(__first1[3], __first2[3]));
375 _Tp __v3 = __binary_op1(__v1, __v2);
376 __init = __binary_op1(__init, __v3);
381 for (; __first1 != __last1; ++__first1, (void) ++__first2)
382 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
401 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp>
405 _InputIterator2 __first2, _Tp __init)
426 template<
typename _InputIterator,
typename _Tp,
427 typename _BinaryOperation,
typename _UnaryOperation>
431 _BinaryOperation __binary_op, _UnaryOperation __unary_op)
433 if constexpr (__is_random_access_iter<_InputIterator>::value)
435 while ((__last - __first) >= 4)
437 _Tp __v1 = __binary_op(__unary_op(__first[0]),
438 __unary_op(__first[1]));
439 _Tp __v2 = __binary_op(__unary_op(__first[2]),
440 __unary_op(__first[3]));
441 _Tp __v3 = __binary_op(__v1, __v2);
442 __init = __binary_op(__init, __v3);
446 for (; __first != __last; ++__first)
447 __init = __binary_op(__init, __unary_op(*__first));
469 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
470 typename _BinaryOperation>
474 _OutputIterator __result, _Tp __init,
475 _BinaryOperation __binary_op)
477 while (__first != __last)
480 __init = __binary_op(__init, *__first);
504 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp>
506 inline _OutputIterator
508 _OutputIterator __result, _Tp __init)
532 template<
typename _InputIterator,
typename _OutputIterator,
533 typename _BinaryOperation,
typename _Tp>
537 _OutputIterator __result, _BinaryOperation __binary_op,
540 for (; __first != __last; ++__first)
541 *__result++ = __init = __binary_op(__init, *__first);
561 template<
typename _InputIterator,
typename _OutputIterator,
562 typename _BinaryOperation>
566 _OutputIterator __result, _BinaryOperation __binary_op)
568 if (__first != __last)
570 auto __init = *__first;
571 *__result++ = __init;
573 if (__first != __last)
595 template<
typename _InputIterator,
typename _OutputIterator>
597 inline _OutputIterator
599 _OutputIterator __result)
622 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
623 typename _BinaryOperation,
typename _UnaryOperation>
627 _OutputIterator __result, _Tp __init,
628 _BinaryOperation __binary_op,
629 _UnaryOperation __unary_op)
631 while (__first != __last)
634 __init = __binary_op(__init, __unary_op(*__first));
661 template<
typename _InputIterator,
typename _OutputIterator,
662 typename _BinaryOperation,
typename _UnaryOperation,
typename _Tp>
666 _OutputIterator __result,
667 _BinaryOperation __binary_op,
668 _UnaryOperation __unary_op,
671 for (; __first != __last; ++__first)
672 *__result++ = __init = __binary_op(__init, __unary_op(*__first));
695 template<
typename _InputIterator,
typename _OutputIterator,
696 typename _BinaryOperation,
typename _UnaryOperation>
700 _OutputIterator __result,
701 _BinaryOperation __binary_op,
702 _UnaryOperation __unary_op)
704 if (__first != __last)
706 auto __init = __unary_op(*__first);
707 *__result++ = __init;
709 if (__first != __last)
711 __binary_op, __unary_op,
720 _GLIBCXX_END_NAMESPACE_VERSION
723 #if __cplusplus >= 201703L
725 # if _PSTL_EXECUTION_POLICIES_DEFINED
727 # include <pstl/glue_numeric_impl.h>
730 # include <pstl/glue_numeric_defs.h>
731 # define _PSTL_NUMERIC_FORWARD_DECLARED 1
735 # define __cpp_lib_parallel_algorithm 201603L
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 _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 std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
static constexpr _Tp max() noexcept
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
constexpr common_type_t< _Mn, _Nn > gcd(_Mn __m, _Nn __n) noexcept
Greatest common divisor.
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 exclusive_scan(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Tp __init, _BinaryOperation __binary_op)
Output the cumulative sum of one range to a second range.
Traits class for iterators.
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 common_type_t< _Mn, _Nn > lcm(_Mn __m, _Nn __n) noexcept
Least common multiple.
static constexpr _Tp min() noexcept
constexpr _Tp reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
Calculate reduction of values in a range.
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...