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
90 namespace 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 #ifdef _GLIBCXX_ASSERTIONS
109 if (!__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 201606L
158 #define __cpp_lib_gcd 201606L
159 #define __cpp_lib_lcm 201606L
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 constexpr (is_signed_v<_Ct>)
193 if (__is_constant_evaluated())
196 bool __overflow = __builtin_mul_overflow(__r, __n2, &__r);
197 __glibcxx_assert(!__overflow);
204 #if __cplusplus > 201703L
207 # define __cpp_lib_interpolate 201902L
209 template<
typename _Tp>
211 enable_if_t<__and_v<is_arithmetic<_Tp>, is_same<remove_cv_t<_Tp>, _Tp>,
212 __not_<is_same<_Tp, bool>>>,
214 midpoint(_Tp __a, _Tp __b) noexcept
216 if constexpr (is_integral_v<_Tp>)
218 using _Up = make_unsigned_t<_Tp>;
229 return __a + __k * _Tp(_Up(__M - __m) / 2);
235 const _Tp __abs_a = __a < 0 ? -__a : __a;
236 const _Tp __abs_b = __b < 0 ? -__b : __b;
237 if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
238 return (__a + __b) / 2;
243 return __a/2 + __b/2;
247 template<
typename _Tp>
248 constexpr enable_if_t<is_object_v<_Tp>, _Tp*>
249 midpoint(_Tp* __a, _Tp* __b) noexcept
251 static_assert(
sizeof(_Tp) != 0,
"type must be complete" );
252 return __a + (__b - __a) / 2;
256 #if __cplusplus >= 201703L
258 #if __cplusplus > 201703L
259 #define __cpp_lib_constexpr_numeric 201911L
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
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
744 # define __cpp_lib_parallel_algorithm 201603L
static constexpr _Tp max() noexcept
Parallel STL function calls corresponding to stl_numeric.h. The functions defined here mainly do case...
Traits class for iterators.
One of the math functors.
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.
One of the math functors.
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 _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 std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
typename common_type< _Tp...>::type common_type_t
Alias template for common_type.
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 _Tp reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
Calculate reduction of values in a range.
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.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
static constexpr _Tp min() noexcept
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.