[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / libcxx / include / __numeric / pstl_reduce.h
blobf9f666c2bb38b8e9e5ca45e4f47b60e7cbf24d3d
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___NUMERIC_PSTL_REDUCE_H
10 #define _LIBCPP___NUMERIC_PSTL_REDUCE_H
12 #include <__algorithm/pstl_frontend_dispatch.h>
13 #include <__config>
14 #include <__functional/identity.h>
15 #include <__iterator/iterator_traits.h>
16 #include <__numeric/pstl_transform_reduce.h>
17 #include <__type_traits/is_execution_policy.h>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
21 #endif
23 _LIBCPP_PUSH_MACROS
24 #include <__undef_macros>
26 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
28 _LIBCPP_BEGIN_NAMESPACE_STD
30 template <class>
31 void __pstl_reduce();
33 template <class _ExecutionPolicy,
34 class _ForwardIterator,
35 class _Tp,
36 class _BinaryOperation = plus<>,
37 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
38 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
39 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_Tp>
40 __reduce(_ExecutionPolicy&& __policy,
41 _ForwardIterator&& __first,
42 _ForwardIterator&& __last,
43 _Tp&& __init,
44 _BinaryOperation&& __op = {}) noexcept {
45 return std::__pstl_frontend_dispatch(
46 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce, _RawPolicy),
47 [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _Tp __g_init, _BinaryOperation __g_op) {
48 return std::__transform_reduce(
49 __policy, std::move(__g_first), std::move(__g_last), std::move(__g_init), std::move(__g_op), __identity{});
51 std::move(__first),
52 std::move(__last),
53 std::move(__init),
54 std::move(__op));
57 template <class _ExecutionPolicy,
58 class _ForwardIterator,
59 class _Tp,
60 class _BinaryOperation = plus<>,
61 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
62 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
63 _LIBCPP_HIDE_FROM_ABI _Tp
64 reduce(_ExecutionPolicy&& __policy,
65 _ForwardIterator __first,
66 _ForwardIterator __last,
67 _Tp __init,
68 _BinaryOperation __op = {}) {
69 auto __res = std::__reduce(__policy, std::move(__first), std::move(__last), std::move(__init), std::move(__op));
70 if (!__res)
71 std::__throw_bad_alloc();
72 return *std::move(__res);
75 template <class _ExecutionPolicy,
76 class _ForwardIterator,
77 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
78 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
79 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__iter_value_type<_ForwardIterator>>
80 __reduce(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last) noexcept {
81 return std::__pstl_frontend_dispatch(
82 _LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_reduce, _RawPolicy),
83 [&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last) {
84 return std::__reduce(
85 __policy, std::move(__g_first), std::move(__g_last), __iter_value_type<_ForwardIterator>());
87 std::move(__first),
88 std::move(__last));
91 template <class _ExecutionPolicy,
92 class _ForwardIterator,
93 class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
94 enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
95 _LIBCPP_HIDE_FROM_ABI __iter_value_type<_ForwardIterator>
96 reduce(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) {
97 auto __res = std::__reduce(__policy, std::move(__first), std::move(__last));
98 if (!__res)
99 std::__throw_bad_alloc();
100 return *std::move(__res);
103 _LIBCPP_END_NAMESPACE_STD
105 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
107 _LIBCPP_POP_MACROS
109 #endif // _LIBCPP___NUMERIC_PSTL_REDUCE_H