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 = PlatformVectorOps::AppleAccelerate<typename CommonElement::Type>;
115
117 using IPP = PlatformVectorOps::IntelIPP<typename CommonElement::Type>;
118 };
119};
120
121namespace detail
122{
123
124template <size_t i, class RuntimeArgs, is::expression Expression>
125constexpr void tryApplyingRuntimeArgsToThisExpression (const RuntimeArgs& args, Expression& e)
126{
127 if constexpr (RuntimeArgs::template hasValue<i>())
128 {
129 std::apply ([&] (const auto&... args)
130 {
131 static_assert (has::applyRuntimeArgs<Expression, decltype (args)...>,
132 "The expression does not implement a applyRuntimeArgs overload that matches the argument types passed.");
133 e.applyRuntimeArgs (args...);
134 }, args.template get<i>());
135 }
136 else if constexpr (has::applyRuntimeArgs<Expression>)
137 {
138 // Some expressions need to initialize runtime values that depend on their source before every
139 // evaluation rather than being injected as external arguments (see e.g. NormalizeSum).
140 e.applyRuntimeArgs();
141 }
142}
143
144template <size_t i, class RuntimeArgs, is::anyVctrOrExpression Src>
145constexpr void tryApplyingRuntimeArgsToSrc (const RuntimeArgs& args, Src& src)
146{
147 if constexpr (has::iterateOverRuntimeArgChain<Src, i, RuntimeArgs> && i < RuntimeArgs::size())
148 src.template iterateOverRuntimeArgChain<i> (args);
149}
150
151} // namespace detail
152
153} // namespace vctr
154
168// clang-format off
169#define VCTR_COMMON_UNARY_EXPRESSION_MEMBERS(ExpressionName, srcName) \
170private: \
171 using Expression = ExpressionTypes<value_type, SrcType>; \
172 SrcType srcName; \
173public: \
174 \
175 template <class Src> \
176 constexpr ExpressionName (Src&& s) : srcName (std::forward<Src> (s)) {} \
177 \
178 constexpr const auto& getStorageInfo() const { return srcName.getStorageInfo(); } \
179 \
180 constexpr size_t size () const { return srcName.size (); } \
181 \
182 constexpr bool isNotAliased (const void* other) const { return srcName.isNotAliased (other); } \
183 \
184 template <size_t i, class RuntimeArgs> \
185 constexpr void iterateOverRuntimeArgChain (const RuntimeArgs& rtArgs) \
186 { \
187 tryApplyingRuntimeArgsToThisExpression<i> (rtArgs, *this); \
188 tryApplyingRuntimeArgsToSrc<i + 1> (rtArgs, srcName); \
189 }
190
206#define VCTR_COMMON_BINARY_VEC_VEC_EXPRESSION_MEMBERS(ExpressionName, srcAName, srcBName) \
207private: \
208 using Expression = ExpressionTypes<value_type, SrcAType, SrcBType>; \
209 using SrcAStorageInfoType = std::invoke_result_t<decltype (&std::remove_cvref_t<SrcAType>::getStorageInfo), SrcAType>; \
210 using SrcBStorageInfoType = std::invoke_result_t<decltype (&std::remove_cvref_t<SrcBType>::getStorageInfo), SrcBType>; \
211 \
212 SrcAType srcAName; \
213 SrcBType srcBName; \
214 const CombinedStorageInfo<std::remove_cvref_t<SrcAStorageInfoType>, std::remove_cvref_t<SrcBStorageInfoType>> storageInfo; \
215 \
216public: \
217 template <class SrcA, class SrcB> \
218 constexpr ExpressionName (SrcA&& a, SrcB&& b) \
219 : srcAName (std::forward<SrcA> (a)), \
220 srcBName (std::forward<SrcB> (b)), \
221 storageInfo (srcAName.getStorageInfo(), srcBName.getStorageInfo()) \
222 {} \
223 \
224 constexpr const auto& getStorageInfo() const { return storageInfo; } \
225 \
226 constexpr size_t size() const { return srcAName.size(); } \
227 \
228 constexpr bool isNotAliased (const void* dst) const \
229 { \
230 if constexpr (is::expression<SrcAType> && is::anyVctr<SrcBType>) \
231 { \
232 return dst != srcBName.data(); \
233 } \
234 \
235 if constexpr (is::anyVctr<SrcAType> && is::expression<SrcBType>) \
236 { \
237 return dst != srcAName.data(); \
238 } \
239 \
240 return true; \
241 }
242
257#define VCTR_COMMON_BINARY_SINGLE_VEC_EXPRESSION_MEMBERS(ExpressionName, srcVecName, srcSingleName) \
258private: \
259 using Expression = ExpressionTypes<value_type, SrcType>; \
260 \
261 SrcType srcVecName; \
262 typename Expression::CommonSrcElement::Type srcSingleName; \
263 const typename Expression::SSESrc srcSingleName##AsSSE; \
264 const typename Expression::NeonSrc srcSingleName##AsNeon; \
265public: \
266 \
267 template <class Src> \
268 constexpr ExpressionName (typename Expression::CommonSrcElement::Type a, Src&& b) \
269 : srcVecName (std::forward<Src> (b)), \
270 srcSingleName (a), \
271 srcSingleName##AsSSE (Expression::SSESrc::broadcast (a)), \
272 srcSingleName##AsNeon (Expression::NeonSrc::broadcast (a)) \
273 {} \
274 \
275 constexpr const auto& getStorageInfo() const { return srcVecName.getStorageInfo(); } \
276 \
277 constexpr size_t size() const { return srcVecName.size(); } \
278 constexpr bool isNotAliased (const void* other) const { return srcVecName.isNotAliased (other); }
279// 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:162
Constrains a type to represent a complex valued floating point number (e.g.
Definition: NumericTypeConcepts.h:83
Constrains a type to represent a real valued floating point number.
Definition: NumericTypeConcepts.h:79
The main namespace of the VCTR project.
Definition: Array.h:24
Definition: AVXRegister.h:28
Definition: Config.h:290
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
PlatformVectorOps::IntelIPP< typename CommonElement::Type > IPP
The PlatformVectorOps::IntelIPP type for the common element type.
Definition: ExpressionTemplate.h:117
static constexpr auto allElementTypesSame
Indicates if all source element types and the return types are same.
Definition: ExpressionTemplate.h:80
PlatformVectorOps::AppleAccelerate< typename CommonElement::Type > Accelerate
The PlatformVectorOps::AppleAccelerate type for the common element type.
Definition: ExpressionTemplate.h:114
The base class to every expression template.
Definition: ExpressionTemplate.h:37
Definition: NeonRegister.h:28
Definition: SSERegister.h:28