VCTR
Loading...
Searching...
No Matches
Pow.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::expressions
24{
25
26template <size_t extent, class SrcAType, class SrcBType>
28{
29public:
30 using CommonType = std::common_type_t<ValueType<SrcAType>, ValueType<SrcBType>>;
31 using value_type = std::conditional_t<std::integral<CommonType>, double, CommonType>;
32
33 VCTR_COMMON_BINARY_VEC_VEC_EXPRESSION_MEMBERS (PowVectors, srcBase, srcExp)
34
35 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
36 {
37#if VCTR_USE_GCEM
39 {
40 if (std::is_constant_evaluated())
41 return value_type (gcem::pow (srcBase[i], srcExp[i]));
42 }
43#endif
44
45 return value_type (std::pow (srcBase[i], srcExp[i]));
46 }
47
48 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
50 {
51 Expression::Accelerate::pow (srcBase.evalNextVectorOpInExpressionChain (dst), srcExp.evalNextVectorOpInExpressionChain (dst), dst, sizeToInt (size()));
52 return dst;
53 }
54
55 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
57 {
58 Expression::IPP::pow (srcBase.evalNextVectorOpInExpressionChain (dst), srcExp.evalNextVectorOpInExpressionChain (dst), dst, sizeToInt (size()));
59 return dst;
60 }
61
62 //==============================================================================
63 // Apple specific SIMD Implementation
64 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") SSERegister<value_type> getSSE (size_t i) const
66 {
67 return { vpowf (srcBase.getSSE (i).value, srcExp.getSSE (i).value) };
68 }
69
70 NeonRegister<value_type> getNeon (size_t i) const
72 {
73 return { vpowf (srcBase.getNeon (i).value, srcExp.getNeon (i).value) };
74 }
75};
76
77template <size_t extent, class SrcType>
79{
80public:
81 using value_type = std::conditional_t<std::is_integral_v<ValueType<SrcType>>, double, ValueType<SrcType>>;
82
83 VCTR_COMMON_BINARY_SINGLE_VEC_EXPRESSION_MEMBERS (PowSingleExponent, base, exp)
84
85 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
86 {
87#if VCTR_USE_GCEM
89 {
90 if (std::is_constant_evaluated())
91 return value_type (gcem::pow (base[i], exp));
92 }
93#endif
94
95 return value_type (std::pow (base[i], exp));
96 }
97
98 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
100 {
101 Expression::IPP::pow (base.evalNextVectorOpInExpressionChain (dst), exp, dst, sizeToInt (size()));
102 return dst;
103 }
104};
105
106template <size_t extent, class SrcType>
108{
109public:
110 using value_type = std::conditional_t<std::is_integral_v<ValueType<SrcType>>, double, ValueType<SrcType>>;
111
112 VCTR_COMMON_BINARY_SINGLE_VEC_EXPRESSION_MEMBERS (PowSingleBase, exp, base)
113
114 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
115 {
116#if VCTR_USE_GCEM
118 {
119 if (std::is_constant_evaluated())
120 return value_type (gcem::pow (base, exp[i]));
121 }
122#endif
123
124 return value_type (std::pow (base, exp[i]));
125 }
126};
127
128template <size_t extent, class SrcType, is::constant ConstantType>
130{
131public:
132 using value_type = std::conditional_t<std::is_integral_v<ValueType<SrcType>>, double, ValueType<SrcType>>;
133
134 static constexpr value_type exp = ConstantType::value;
135
136 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (PowConstantExponent, src)
137
138 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
139 {
140#if VCTR_USE_GCEM
142 {
143 if (std::is_constant_evaluated())
144 return value_type (gcem::pow (src[i], exp));
145 }
146#endif
147
148 return value_type (std::pow (src[i], exp));
149 }
150
151 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
153 {
154 Expression::IPP::pow (src.evalNextVectorOpInExpressionChain (dst), exp, dst, sizeToInt (size()));
155 return dst;
156 }
157};
158
159template <size_t extent, class SrcType, is::constant ConstantType>
161{
162public:
163 using value_type = std::conditional_t<std::is_integral_v<ValueType<SrcType>>, double, ValueType<SrcType>>;
164
165 static constexpr value_type base = ConstantType::value;
166
167 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (PowConstantBase, src)
168
169 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
170 {
171#if VCTR_USE_GCEM
173 {
174 if (std::is_constant_evaluated())
175 return value_type (gcem::pow (base, src[i]));
176 }
177#endif
178
179 return value_type (std::pow (base, src[i]));
180 }
181
182 //==============================================================================
183 // Platform Vector Operation Implementation
184 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
187 {
188 // Todo: Quick workaround to use this in the context of chained accelerated operations.
189 // This is useful to speed up decibel calculation. We should optimise it – maybe based
190 // on chaining e.g. exp2 (log2 (base) * src)
191 const auto s = size();
192 const auto* x = src.evalNextVectorOpInExpressionChain (dst);
193
194 for (size_t i = 0; i < s; ++i)
195 dst[i] = std::pow (base, x[i]);
196
197 return dst;
198 }
199};
200
201} // namespace vctr::expressions
202
203namespace vctr
204{
205
210 template <is::anyVctrOrExpression SrcBaseType, is::anyVctrOrExpression SrcExpType>
211 constexpr auto pow (SrcBaseType&& bases, SrcExpType&& exponents)
212{
213 assertCommonSize (bases, exponents);
214 constexpr auto extent = getCommonExtent<SrcBaseType, SrcExpType>();
215
216 return expressions::PowVectors<extent, SrcBaseType, SrcExpType> (std::forward<SrcBaseType> (bases), std::forward<SrcExpType> (exponents));
217}
218
223template <is::anyVctrOrExpression Src>
224constexpr auto pow (typename std::remove_cvref_t<Src>::value_type base, Src&& exponents)
225{
226 return expressions::PowSingleBase<extentOf<Src>, Src> (base, exponents);
227}
228
239template <is::anyVctrOrExpression Src>
240constexpr auto pow (Src&& bases, typename std::remove_cvref_t<Src>::value_type exponent)
241{
242 return expressions::PowSingleExponent<extentOf<Src>, Src> (exponent, bases);
243}
244
249template <auto base>
251
256template <auto exponent>
258
259} // namespace vctr
Definition: Pow.h:28
Constrains a type to represent a complex valued floating point number (e.g.
Definition: NumericTypeConcepts.h:83
A combined concept to check if Apple Accelerate vpf functions are suitable to work on Neon registers.
Definition: ContainerAndExpressionConcepts.h:238
A combined concept to check if Apple Accelerate is a suitable option for a real valued floating point...
Definition: ContainerAndExpressionConcepts.h:303
A combined concept to check if Apple Accelerate is a suitable option for a real valued floating point...
Definition: ContainerAndExpressionConcepts.h:242
A combined concept to check if Apple Accelerate vpf functions are suitable to work on SSE registers.
Definition: ContainerAndExpressionConcepts.h:234
A combined concept to check if Intel IPP is a suitable option for a real valued floating point vector...
Definition: ContainerAndExpressionConcepts.h:266
A combined concept to check if Intel IPP is a suitable option for a real or complex valued floating p...
Definition: ContainerAndExpressionConcepts.h:324
A combined concept to check if Intel IPP is a suitable option for a real or complex valued floating p...
Definition: ContainerAndExpressionConcepts.h:278
constexpr ExpressionChainBuilder< expressions::Exp > exp
Computes e (Euler's number, 2.7182818...) raised to the source vector elements power.
Definition: Exp.h:84
constexpr ExpressionChainBuilder< expressions::PowConstantBase, Constant< base > > powConstantBase
Evaluates base raised to the power of the source elements.
Definition: Pow.h:250
constexpr auto pow(SrcBaseType &&bases, SrcExpType &&exponents)
Returns an expression that raises the elements in bases element-wise to the power of the elements in ...
Definition: Pow.h:211
constexpr ExpressionChainBuilder< expressions::PowConstantExponent, Constant< exponent > > powConstantExponent
Evaluates the source elements raised to the power of exponent.
Definition: Pow.h:257
The main namespace of the VCTR project.
Definition: Array.h:24
constexpr void assertCommonSize(const A &a, const B &b)
Ensures that both sources have the same size.
Definition: Traits.h:253
typename detail::ValueType< std::remove_cvref_t< T > >::Type ValueType
If T is an expression template, it equals its return type, if it's a type that defines value_type as ...
Definition: Traits.h:201
int sizeToInt(size_t size)
Casts the size_t argument to an int.
Definition: PlatformVectorOpsHelpers.h:27
An expression chain builder is an object which supplies various operator<< overloads which build chai...
Definition: ExpressionChainBuilder.h:136
The base class to every expression template.
Definition: ExpressionTemplate.h:37
Definition: NeonRegister.h:28
Definition: SSERegister.h:28