VCTR
Loading...
Searching...
No Matches
ExpressionTemplate.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2022- by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23namespace vctr
24{
25
37{
39 template <class ReturnElementType, class... SrcTypes>
41 {
43 template <class T>
45 {
46 using Type = T;
47
48 static constexpr auto isRealFloat = is::realFloatNumber<Type>;
49
50 static constexpr auto isSigned = std::is_signed_v<Type>;
51
52 static constexpr auto isUnsigned = std::is_unsigned_v<Type>;
53
54 static constexpr auto isInt = std::is_integral_v<Type>;
55
56 static constexpr auto isSignedInt = isSigned & isInt;
57
58 static constexpr auto isComplexFloat = is::complexFloatNumber<Type>;
59
60 static constexpr auto isInt32 = std::same_as<int32_t, Type>;
61
62 static constexpr auto isUint32 = std::same_as<uint32_t, Type>;
63
64 static constexpr auto isInt64 = std::same_as<int64_t, Type>;
65
66 static constexpr auto isUint64 = std::same_as<uint64_t, Type>;
67
68 static constexpr auto isFloat = std::same_as<float, Type>;
69
70 static constexpr auto isDouble = std::same_as<double, Type>;
71 };
72
75
78
81
82 //==============================================================================
85
88
91
92 //==============================================================================
95
98
101
102 //==============================================================================
105
108
111
112 //==============================================================================
114 using Accelerate = platform_vec_ops::AppleAccelerate<typename CommonElement::Type>;
115
117 using IPP = platform_vec_ops::IntelIPP<typename CommonElement::Type>;
118 };
119
121 template <class ExpressionTypes>
123 {
124 typename ExpressionTypes::NeonSrc neon;
125 typename ExpressionTypes::SSESrc sse;
126 typename ExpressionTypes::AVXSrc avx;
127 };
128};
129
130namespace detail
131{
132
133template <size_t i, class RuntimeArgs, is::expression Expression>
134constexpr void tryApplyingRuntimeArgsToThisExpression (const RuntimeArgs& args, Expression& e)
135{
136 if constexpr (RuntimeArgs::template hasValue<i>())
137 {
138 std::apply ([&] (const auto&... args)
139 {
140 static_assert (has::applyRuntimeArgs<Expression, decltype (args)...>,
141 "The expression does not implement a applyRuntimeArgs overload that matches the argument types passed.");
142 e.applyRuntimeArgs (args...);
143 }, args.template get<i>());
144 }
145 else if constexpr (has::applyRuntimeArgs<Expression>)
146 {
147 // Some expressions need to initialize runtime values that depend on their source before every
148 // evaluation rather than being injected as external arguments (see e.g. NormalizeSum).
149 e.applyRuntimeArgs();
150 }
151}
152
153template <size_t i, class RuntimeArgs, is::anyVctrOrExpression Src>
154constexpr void tryApplyingRuntimeArgsToSrc (const RuntimeArgs& args, Src& src)
155{
156 if constexpr (has::iterateOverRuntimeArgChain<Src, i, RuntimeArgs> && i < RuntimeArgs::size())
157 src.template iterateOverRuntimeArgChain<i> (args);
158}
159
160template <class A, class B, auto op>
162{
163 using type = decltype (op (std::declval<A>(), std::declval<B>()));
164};
165
166template <class A, std::common_with<A> B, auto op>
167struct CommonVecExpressionType<A, B, op> : std::common_type<A, B> {};
168
169} // namespace detail
170
176template <class A, class B, auto op>
177using CommonVecExpressionType = typename detail::CommonVecExpressionType<A, B, op>::type;
178
179} // namespace vctr
180
194// clang-format off
195#define VCTR_COMMON_UNARY_EXPRESSION_MEMBERS(ExpressionName, srcName) \
196private: \
197 using Expression = ExpressionTypes<value_type, SrcType>; \
198 SrcType srcName; \
199public: \
200 \
201 template <class Src> \
202 constexpr ExpressionName (Src&& s) : srcName (std::forward<Src> (s)) {} \
203 \
204 constexpr const auto& getStorageInfo() const { return srcName.getStorageInfo(); } \
205 \
206 constexpr size_t size () const { return srcName.size (); } \
207 \
208 constexpr bool isNotAliased (const void* other) const { return srcName.isNotAliased (other); } \
209 \
210 template <size_t i, class RuntimeArgs> \
211 constexpr void iterateOverRuntimeArgChain (const RuntimeArgs& rtArgs) \
212 { \
213 tryApplyingRuntimeArgsToThisExpression<i> (rtArgs, *this); \
214 tryApplyingRuntimeArgsToSrc<i + 1> (rtArgs, srcName); \
215 }
216
232#define VCTR_COMMON_BINARY_VEC_VEC_EXPRESSION_MEMBERS(ExpressionName, srcAName, srcBName) \
233private: \
234 using Expression = ExpressionTypes<value_type, SrcAType, SrcBType>; \
235 using SrcAStorageInfoType = std::invoke_result_t<decltype (&std::remove_cvref_t<SrcAType>::getStorageInfo), SrcAType>; \
236 using SrcBStorageInfoType = std::invoke_result_t<decltype (&std::remove_cvref_t<SrcBType>::getStorageInfo), SrcBType>; \
237 \
238 SrcAType srcAName; \
239 SrcBType srcBName; \
240 const ::vctr::CombinedStorageInfo<std::remove_cvref_t<SrcAStorageInfoType>, std::remove_cvref_t<SrcBStorageInfoType>> storageInfo; \
241 \
242public: \
243 template <class SrcA, class SrcB> \
244 constexpr ExpressionName (SrcA&& a, SrcB&& b) \
245 : srcAName (std::forward<SrcA> (a)), \
246 srcBName (std::forward<SrcB> (b)), \
247 storageInfo (srcAName.getStorageInfo(), srcBName.getStorageInfo()) \
248 {} \
249 \
250 constexpr const auto& getStorageInfo() const { return storageInfo; } \
251 \
252 constexpr size_t size() const { return srcAName.size(); } \
253 \
254 constexpr bool isNotAliased (const void* dst) const \
255 { \
256 if constexpr (is::expression<SrcAType> && is::anyVctr<SrcBType>) \
257 { \
258 return dst != srcBName.data(); \
259 } \
260 \
261 if constexpr (is::anyVctr<SrcAType> && is::expression<SrcBType>) \
262 { \
263 return dst != srcAName.data(); \
264 } \
265 \
266 return true; \
267 }
268
283#define VCTR_COMMON_BINARY_SINGLE_VEC_EXPRESSION_MEMBERS(ExpressionName, srcVecName, srcSingleName) \
284private: \
285 using Expression = ExpressionTypes<value_type, SrcType>; \
286 \
287 SrcType srcVecName; \
288 typename Expression::CommonSrcElement::Type srcSingleName; \
289public: \
290 \
291 template <class Src> \
292 constexpr ExpressionName (typename Expression::CommonSrcElement::Type a, Src&& b) \
293 : srcVecName (std::forward<Src> (b)), \
294 srcSingleName (a) \
295 {} \
296 \
297 constexpr const auto& getStorageInfo() const { return srcVecName.getStorageInfo(); } \
298 \
299 constexpr size_t size() const { return srcVecName.size(); } \
300 constexpr bool isNotAliased (const void* other) const { return srcVecName.isNotAliased (other); }
301
303#define VCTR_FORWARD_PREPARE_SIMD_EVALUATION_UNARY_EXPRESSION_MEMBER_FUNCTIONS \
304 void prepareNeonEvaluation() const \
305 requires ::vctr::has::prepareNeonEvaluation<SrcType> \
306 { \
307 src.prepareNeonEvaluation(); \
308 } \
309 \
310 VCTR_FORCEDINLINE VCTR_TARGET ("avx") void prepareAVXEvaluation() const \
311 requires ::vctr::has::prepareAVXEvaluation<SrcType> && Expression::CommonElement::isRealFloat \
312 { \
313 src.prepareAVXEvaluation(); \
314 } \
315 \
316 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") void prepareAVXEvaluation() const \
317 requires ::vctr::has::prepareAVXEvaluation<SrcType> && Expression::CommonElement::isInt \
318 { \
319 src.prepareAVXEvaluation(); \
320 } \
321 \
322 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void prepareSSEEvaluation() const \
323 requires ::vctr::has::prepareSSEEvaluation<SrcType> \
324 { \
325 src.prepareSSEEvaluation(); \
326 }
327
329#define VCTR_FORWARD_PREPARE_SIMD_EVALUATION_BINARY_EXPRESSION_MEMBER_FUNCTIONS(srcAName, srcBName) \
330 void prepareNeonEvaluation() const \
331 requires ::vctr::has::prepareNeonEvaluation<SrcAType> && ::vctr::has::prepareNeonEvaluation<SrcBType> \
332 { \
333 srcAName.prepareNeonEvaluation(); \
334 srcBName.prepareNeonEvaluation(); \
335 } \
336 \
337 VCTR_FORCEDINLINE VCTR_TARGET ("avx") void prepareAVXEvaluation() const \
338 requires ::vctr::has::prepareAVXEvaluation<SrcAType> && ::vctr::has::prepareAVXEvaluation<SrcBType> && Expression::CommonElement::isRealFloat \
339 { \
340 srcAName.prepareAVXEvaluation(); \
341 srcBName.prepareAVXEvaluation(); \
342 } \
343 \
344 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") void prepareAVXEvaluation() const \
345 requires ::vctr::has::prepareAVXEvaluation<SrcAType> && ::vctr::has::prepareAVXEvaluation<SrcBType> && Expression::CommonElement::isInt \
346 { \
347 srcAName.prepareAVXEvaluation(); \
348 srcBName.prepareAVXEvaluation(); \
349 } \
350 \
351 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void prepareSSEEvaluation() const \
352 requires ::vctr::has::prepareSSEEvaluation<SrcAType> && ::vctr::has::prepareSSEEvaluation<SrcBType> \
353 { \
354 srcAName.prepareSSEEvaluation(); \
355 srcBName.prepareSSEEvaluation(); \
356 }
357// clang-format on
Constrains a pack of elements to be the same type.
Definition: GenericConcepts.h:142
Constrains a type to have a function applyRuntimeArgs (const Args&...)
Definition: ContainerAndExpressionConcepts.h:180
Constrains a type to represent a complex valued floating point number (e.g.
Definition: NumericTypeConcepts.h:87
Constrains a type to represent a real valued floating point number.
Definition: NumericTypeConcepts.h:83
The main namespace of the VCTR project.
Definition: Array.h:24
typename detail::CommonVecExpressionType< A, B, op >::type CommonVecExpressionType
Used to figure out the value_type for binary vector-vector operations.
Definition: ExpressionTemplate.h:177
Definition: AVXRegister.h:28
Definition: Config.h:298
Some common traits you want to check when constraining expression template functions.
Definition: ExpressionTemplate.h:45
Supplies some handy typedefs and traits needed when implementing expression templates.
Definition: ExpressionTemplate.h:41
static constexpr auto allElementTypesSame
Indicates if all source element types and the return types are same.
Definition: ExpressionTemplate.h:80
platform_vec_ops::AppleAccelerate< typename CommonElement::Type > Accelerate
The platform_vec_ops::AppleAccelerate type for the common element type.
Definition: ExpressionTemplate.h:114
platform_vec_ops::IntelIPP< typename CommonElement::Type > IPP
The platform_vec_ops::IntelIPP type for the common element type.
Definition: ExpressionTemplate.h:117
The base class to every expression template.
Definition: ExpressionTemplate.h:37
Definition: NeonRegister.h:28
Definition: SSERegister.h:28
Helper template to define a union of all supported SIMD types.
Definition: ExpressionTemplate.h:123