VCTR
Loading...
Searching...
No Matches
Mean.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2023 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 SrcType>
27requires is::number<ValueType<SrcType>>
29{
30public:
31 using value_type = ValueType<SrcType>;
32
33 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (Mean, src)
34
35 static constexpr value_type reductionResultInitValue = 0;
36
37 VCTR_FORCEDINLINE constexpr void reduceElementWise (value_type& result, size_t i) const
38 {
39 result += src[i];
40 }
41
42 //==============================================================================
43 VCTR_FORCEDINLINE value_type reduceVectorOp() const
45 {
46 return Expression::Accelerate::mean (src.data(), size());
47 };
48
49 VCTR_FORCEDINLINE value_type reduceVectorOp() const
51 {
52 return Expression::IPP::mean (src.data(), sizeToInt (size()));
53 };
54
55 //==============================================================================
56 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
58 {
59 result = Expression::Neon::add (result, src.getNeon (i));
60 }
61
62 VCTR_FORCEDINLINE VCTR_TARGET ("avx") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
63 requires Config::archX64 && has::getAVX<SrcType> && is::realFloatNumber<value_type>
64 {
65 result = Expression::AVX::add (result, src.getAVX (i));
66 }
67
68 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
69 requires Config::archX64 && has::getAVX<SrcType> && is::int32Number<value_type>
70 {
71 result = Expression::AVX::add (result, src.getAVX (i));
72 }
73
74 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
76 {
77 result = Expression::SSE::add (result, src.getSSE (i));
78 }
79
80 //==============================================================================
81 template <size_t n>
82 VCTR_FORCEDINLINE constexpr value_type finalizeReduction (const std::array<value_type, n>& sums) const
83 {
84 auto sum = n == 1 ? sums[0] : std::reduce (sums.begin(), sums.end());
85
86 return value_type (sum / FloatType<RealType<value_type>> (src.size()));
87 }
88};
89
90template <size_t extent, class SrcType>
93{
94public:
95 using value_type = ValueType<SrcType>;
96
97 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (MeanSquare, src)
98
99 static constexpr value_type reductionResultInitValue = 0;
100
101 VCTR_FORCEDINLINE constexpr void reduceElementWise (value_type& result, size_t i) const
102 {
103 auto s = src[i];
104 result += s * s;
105 }
106
107 //==============================================================================
108 VCTR_FORCEDINLINE value_type reduceVectorOp() const
110 {
111 return Expression::Accelerate::meanSquare (src.data(), size());
112 };
113
114 VCTR_FORCEDINLINE value_type reduceVectorOp() const
116 {
117 auto l2Norm = Expression::IPP::l2Norm (src.data(), sizeToInt (size()));
118 return (l2Norm * l2Norm) / value_type (size());
119 };
120
121 //==============================================================================
122 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
124 {
125 auto s = src.getNeon (i);
126 s = Expression::Neon::mul (s, s);
127 result = Expression::Neon::add (result, s);
128 }
129
130 VCTR_FORCEDINLINE VCTR_TARGET ("avx") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
131 requires Config::archX64 && has::getAVX<SrcType> && is::realFloatNumber<value_type>
132 {
133 auto s = src.getAVX (i);
134 s = Expression::AVX::mul (s, s);
135 result = Expression::AVX::add (result, s);
136 }
137
138 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
139 requires Config::archX64 && has::getSSE<SrcType> && is::realFloatNumber<value_type>
140 {
141 auto s = src.getSSE (i);
142 s = Expression::SSE::mul (s, s);
143 result = Expression::SSE::add (result, s);
144 }
145
146 //==============================================================================
147 template <size_t n>
148 VCTR_FORCEDINLINE constexpr value_type finalizeReduction (const std::array<value_type, n>& sums) const
149 {
150 auto sum = n == 1 ? sums[0] : std::reduce (sums.begin(), sums.end());
151
152 return value_type (sum / FloatType<RealType<value_type>> (size()));
153 }
154};
155
156template <size_t extent, class SrcType>
159{
160public:
161 using value_type = ValueType<SrcType>;
162
163 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (RootMeanSquare, src)
164
165 static constexpr value_type reductionResultInitValue = 0;
166
167 VCTR_FORCEDINLINE constexpr void reduceElementWise (value_type& result, size_t i) const
168 {
169 auto s = src[i];
170 result += s * s;
171 }
172
173 //==============================================================================
174 VCTR_FORCEDINLINE value_type reduceVectorOp() const
176 {
177 return Expression::Accelerate::rms (src.data(), size());
178 };
179
180 VCTR_FORCEDINLINE value_type reduceVectorOp() const
182 {
183 auto l2Norm = Expression::IPP::l2Norm (src.data(), sizeToInt (size()));
184 return std::sqrt ((l2Norm * l2Norm) / value_type (size()));
185 };
186
187 //==============================================================================
188 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
190 {
191 auto s = src.getNeon (i);
192 s = Expression::Neon::mul (s, s);
193 result = Expression::Neon::add (result, s);
194 }
195
196 VCTR_FORCEDINLINE VCTR_TARGET ("avx") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
197 requires Config::archX64 && has::getAVX<SrcType> && is::realFloatNumber<value_type>
198 {
199 auto s = src.getAVX (i);
200 s = Expression::AVX::mul (s, s);
201 result = Expression::AVX::add (result, s);
202 }
203
204 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
205 requires Config::archX64 && has::getSSE<SrcType> && is::realFloatNumber<value_type>
206 {
207 auto s = src.getSSE (i);
208 s = Expression::SSE::mul (s, s);
209 result = Expression::SSE::add (result, s);
210 }
211
212 //==============================================================================
213 template <size_t n>
214 VCTR_FORCEDINLINE constexpr value_type finalizeReduction (const std::array<value_type, n>& squaredSums) const
215 {
216 auto squaredSum = n == 1 ? squaredSums[0] : std::reduce (squaredSums.begin(), squaredSums.end());
217 auto meanSquaredSum = squaredSum / RealType<value_type> (src.size());
218
219 #if VCTR_USE_GCEM
221 {
222 if (std::is_constant_evaluated())
223 {
224 return value_type (gcem::sqrt (meanSquaredSum));
225 }
226 }
227 #endif
228
229 return value_type (std::sqrt (meanSquaredSum));
230 }
231};
232
233} // namespace vctr::expressions
234
235namespace vctr
236{
237
243
249
255
256} // namespace vctr
Definition: Mean.h:93
Definition: Mean.h:29
Definition: Mean.h:159
Constrains a type to have a member function getAVX (size_t) const.
Definition: ContainerAndExpressionConcepts.h:78
Constrains a type to have a member function getNeon (size_t) const.
Definition: ContainerAndExpressionConcepts.h:74
Constrains a type to have a member function getSSE (size_t) const.
Definition: ContainerAndExpressionConcepts.h:82
Constrains a type to represent a real valued 32 bit integer number.
Definition: NumericTypeConcepts.h:53
Constrains a type to represent a real valued or std::complex number type.
Definition: NumericTypeConcepts.h:45
Constrains a type to represent a real valued floating point number.
Definition: NumericTypeConcepts.h:79
Constrains a type to represent a real or complex valued floating point number.
Definition: NumericTypeConcepts.h:87
A combined concept to check if Apple Accelerate is a suitable option for a floating point vector redu...
Definition: ContainerAndExpressionConcepts.h:262
A combined concept to check if Intel IPP is a suitable option for a floating point vector reduction o...
Definition: ContainerAndExpressionConcepts.h:286
A combined concept to check if Intel IPP is a suitable option for a real or complex floating point ve...
Definition: ContainerAndExpressionConcepts.h:290
constexpr ExpressionChainBuilder< expressions::RootMeanSquare > rms
Computes the square root of the mean value of the squared source values.
Definition: Mean.h:254
constexpr ExpressionChainBuilder< expressions::Mean > mean
Computes the mean value of the source values.
Definition: Mean.h:242
constexpr ExpressionChainBuilder< expressions::Sum > sum
Computes the sum of the source values.
Definition: Sum.h:116
constexpr ExpressionChainBuilder< expressions::MeanSquare > meanSquare
Computes the mean value of the squared source values.
Definition: Mean.h:248
The main namespace of the VCTR project.
Definition: Array.h:24
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
typename detail::FloatType< std::remove_cvref_t< T > >::Type FloatType
The best matching float type for the real number type T.
Definition: Traits.h:219
typename detail::RealType< std::remove_cvref_t< T > >::Type RealType
If T is any instance of std::complex, this will be the real value_type, otherwise this will be T.
Definition: Traits.h:211
int sizeToInt(size_t size)
Casts the size_t argument to an int.
Definition: PlatformVectorOpsHelpers.h:27
Definition: AVXRegister.h:28
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