1 // -*- C++ -*-
2 //===-- execution_impl.h --------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef __PSTL_execution_impl_H
11 #define __PSTL_execution_impl_H
12 
13 #include <iterator>
14 #include <type_traits>
15 
16 #include "execution_defs.h"
17 
18 namespace __pstl
19 {
20 namespace __internal
21 {
22 
23 using namespace __pstl::execution;
24 
25 /* predicate */
26 
27 template <typename _Tp>
__lazy_and(_Tp,std::false_type)28 std::false_type __lazy_and(_Tp, std::false_type)
29 {
30     return std::false_type{};
31 };
32 
33 template <typename _Tp>
34 inline _Tp
__lazy_and(_Tp __a,std::true_type)35 __lazy_and(_Tp __a, std::true_type)
36 {
37     return __a;
38 }
39 
40 template <typename _Tp>
__lazy_or(_Tp,std::true_type)41 std::true_type __lazy_or(_Tp, std::true_type)
42 {
43     return std::true_type{};
44 };
45 
46 template <typename _Tp>
47 inline _Tp
__lazy_or(_Tp __a,std::false_type)48 __lazy_or(_Tp __a, std::false_type)
49 {
50     return __a;
51 }
52 
53 /* iterator */
54 template <typename _IteratorType, typename... _OtherIteratorTypes>
55 struct __is_random_access_iterator
56 {
57     static constexpr bool value =
58       __internal::__is_random_access_iterator<_IteratorType>::value && __internal::__is_random_access_iterator<_OtherIteratorTypes...>::value;
59     typedef std::integral_constant<bool, value> type;
60 };
61 
62 template <typename _IteratorType>
63 struct __is_random_access_iterator<_IteratorType>
64     : std::is_same<typename std::iterator_traits<_IteratorType>::iterator_category, std::random_access_iterator_tag>
65 {
66 };
67 
68 /* policy */
69 template <typename _Policy>
70 struct __policy_traits
71 {
72 };
73 
74 template <>
75 struct __policy_traits<sequenced_policy>
76 {
77     typedef std::false_type allow_parallel;
78     typedef std::false_type allow_unsequenced;
79     typedef std::false_type allow_vector;
80 };
81 
82 template <>
83 struct __policy_traits<unsequenced_policy>
84 {
85     typedef std::false_type allow_parallel;
86     typedef std::true_type allow_unsequenced;
87     typedef std::true_type allow_vector;
88 };
89 
90 #if __PSTL_USE_PAR_POLICIES
91 template <>
92 struct __policy_traits<parallel_policy>
93 {
94     typedef std::true_type allow_parallel;
95     typedef std::false_type allow_unsequenced;
96     typedef std::false_type allow_vector;
97 };
98 
99 template <>
100 struct __policy_traits<parallel_unsequenced_policy>
101 {
102     typedef std::true_type allow_parallel;
103     typedef std::true_type allow_unsequenced;
104     typedef std::true_type allow_vector;
105 };
106 #endif
107 
108 template <typename _ExecutionPolicy>
109 using __collector_t = typename __internal::__policy_traits<typename std::decay<_ExecutionPolicy>::type>::__collector_type;
110 
111 template <typename _ExecutionPolicy>
112 using __allow_vector = typename __internal::__policy_traits<typename std::decay<_ExecutionPolicy>::type>::__allow_vector;
113 
114 template <typename _ExecutionPolicy>
115 using __allow_unsequenced = typename __internal::__policy_traits<typename std::decay<_ExecutionPolicy>::type>::__allow_unsequenced;
116 
117 template <typename _ExecutionPolicy>
118 using __allow_parallel = typename __internal::__policy_traits<typename std::decay<_ExecutionPolicy>::type>::__allow_parallel;
119 
120 template <typename _ExecutionPolicy, typename... _IteratorTypes>
121 auto
122 __is_vectorization_preferred(_ExecutionPolicy&& __exec)
123     -> decltype(__internal::__lazy_and(__exec.__allow_vector(), typename __internal::__is_random_access_iterator<_IteratorTypes...>::type()))
124 {
125     return __internal::__lazy_and(__exec.__allow_vector(), typename __internal::__is_random_access_iterator<_IteratorTypes...>::type());
126 }
127 
128 template <typename _ExecutionPolicy, typename... _IteratorTypes>
129 auto
130 __is_parallelization_preferred(_ExecutionPolicy&& __exec)
131     -> decltype(__internal::__lazy_and(__exec.__allow_parallel(), typename __internal::__is_random_access_iterator<_IteratorTypes...>::type()))
132 {
133     return __internal::__lazy_and(__exec.__allow_parallel(), typename __internal::__is_random_access_iterator<_IteratorTypes...>::type());
134 }
135 
136 template <typename policy, typename... _IteratorTypes>
137 struct __prefer_unsequenced_tag
138 {
139     static constexpr bool value =
140         __internal::__allow_unsequenced<policy>::value && __internal::__is_random_access_iterator<_IteratorTypes...>::value;
141     typedef std::integral_constant<bool, value> type;
142 };
143 
144 template <typename policy, typename... _IteratorTypes>
145 struct __prefer_parallel_tag
146 {
147     static constexpr bool value =
148         __internal::__allow_parallel<policy>::value && __internal::__is_random_access_iterator<_IteratorTypes...>::value;
149     typedef std::integral_constant<bool, value> type;
150 };
151 
152 } // namespace __internal
153 } // namespace __pstl
154 
155 #endif /* __PSTL_execution_impl_H */
156