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_FORWARD_PREPARE_SIMD_EVALUATION_UNARY_EXPRESSION_MEMBER_FUNCTIONS
57
58 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
60 {
61 result = Expression::Neon::add (result, src.getNeon (i));
62 }
63
64 VCTR_FORCEDINLINE VCTR_TARGET ("fma") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
65 requires Config::archX64 && has::getAVX<SrcType> && is::realFloatNumber<value_type>
66 {
67 result = Expression::AVX::add (result, src.getAVX (i));
68 }
69
70 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
71 requires Config::archX64 && has::getAVX<SrcType> && is::int32Number<value_type>
72 {
73 result = Expression::AVX::add (result, src.getAVX (i));
74 }
75
76 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
78 {
79 result = Expression::SSE::add (result, src.getSSE (i));
80 }
81
82 //==============================================================================
83 template <size_t n>
84 VCTR_FORCEDINLINE constexpr value_type finalizeReduction (const std::array<value_type, n>& sums) const
85 {
86 auto sum = n == 1 ? sums[0] : std::reduce (sums.begin(), sums.end());
87
88 return value_type (sum / FloatType<RealType<value_type>> (src.size()));
89 }
90};
91
92template <size_t extent, class SrcType>
95{
96public:
97 using value_type = ValueType<SrcType>;
98
99 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (MeanSquare, src)
100
101 static constexpr value_type reductionResultInitValue = 0;
102
103 VCTR_FORCEDINLINE constexpr void reduceElementWise (value_type& result, size_t i) const
104 {
105 auto s = src[i];
106 result += s * s;
107 }
108
109 //==============================================================================
110 VCTR_FORCEDINLINE value_type reduceVectorOp() const
112 {
113 return Expression::Accelerate::meanSquare (src.data(), size());
114 };
115
116 VCTR_FORCEDINLINE value_type reduceVectorOp() const
118 {
119 auto l2Norm = Expression::IPP::l2Norm (src.data(), sizeToInt (size()));
120 return (l2Norm * l2Norm) / value_type (size());
121 };
122
123 //==============================================================================
124 VCTR_FORWARD_PREPARE_SIMD_EVALUATION_UNARY_EXPRESSION_MEMBER_FUNCTIONS
125
126 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
128 {
129 auto s = src.getNeon (i);
130 s = Expression::Neon::mul (s, s);
131 result = Expression::Neon::add (result, s);
132 }
133
134 VCTR_FORCEDINLINE VCTR_TARGET ("fma") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
135 requires Config::archX64 && has::getAVX<SrcType> && is::realFloatNumber<value_type>
136 {
137 auto s = src.getAVX (i);
138 s = Expression::AVX::mul (s, s);
139 result = Expression::AVX::add (result, s);
140 }
141
142 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
143 requires Config::archX64 && has::getSSE<SrcType> && is::realFloatNumber<value_type>
144 {
145 auto s = src.getSSE (i);
146 s = Expression::SSE::mul (s, s);
147 result = Expression::SSE::add (result, s);
148 }
149
150 //==============================================================================
151 template <size_t n>
152 VCTR_FORCEDINLINE constexpr value_type finalizeReduction (const std::array<value_type, n>& sums) const
153 {
154 auto sum = n == 1 ? sums[0] : std::reduce (sums.begin(), sums.end());
155
156 return value_type (sum / FloatType<RealType<value_type>> (size()));
157 }
158};
159
160template <size_t extent, class SrcType>
163{
164public:
165 using value_type = ValueType<SrcType>;
166
167 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (RootMeanSquare, src)
168
169 static constexpr value_type reductionResultInitValue = 0;
170
171 VCTR_FORCEDINLINE constexpr void reduceElementWise (value_type& result, size_t i) const
172 {
173 auto s = src[i];
174 result += s * s;
175 }
176
177 //==============================================================================
178 VCTR_FORCEDINLINE value_type reduceVectorOp() const
180 {
181 return Expression::Accelerate::rms (src.data(), size());
182 };
183
184 VCTR_FORCEDINLINE value_type reduceVectorOp() const
186 {
187 auto l2Norm = Expression::IPP::l2Norm (src.data(), sizeToInt (size()));
188 return std::sqrt ((l2Norm * l2Norm) / value_type (size()));
189 };
190
191 //==============================================================================
192 VCTR_FORWARD_PREPARE_SIMD_EVALUATION_UNARY_EXPRESSION_MEMBER_FUNCTIONS
193
194 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
196 {
197 auto s = src.getNeon (i);
198 s = Expression::Neon::mul (s, s);
199 result = Expression::Neon::add (result, s);
200 }
201
202 VCTR_FORCEDINLINE VCTR_TARGET ("fma") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
203 requires Config::archX64 && has::getAVX<SrcType> && is::realFloatNumber<value_type>
204 {
205 auto s = src.getAVX (i);
206 s = Expression::AVX::mul (s, s);
207 result = Expression::AVX::add (result, s);
208 }
209
210 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
211 requires Config::archX64 && has::getSSE<SrcType> && is::realFloatNumber<value_type>
212 {
213 auto s = src.getSSE (i);
214 s = Expression::SSE::mul (s, s);
215 result = Expression::SSE::add (result, s);
216 }
217
218 //==============================================================================
219 template <size_t n>
220 VCTR_FORCEDINLINE constexpr value_type finalizeReduction (const std::array<value_type, n>& squaredSums) const
221 {
222 auto squaredSum = n == 1 ? squaredSums[0] : std::reduce (squaredSums.begin(), squaredSums.end());
223 auto meanSquaredSum = squaredSum / RealType<value_type> (src.size());
224
225 #if VCTR_USE_GCEM
227 {
228 if (std::is_constant_evaluated())
229 {
230 return value_type (gcem::sqrt (meanSquaredSum));
231 }
232 }
233 #endif
234
235 return value_type (std::sqrt (meanSquaredSum));
236 }
237};
238
239} // namespace vctr::expressions
240
241namespace vctr
242{
243
249
255
261
262} // namespace vctr
Definition: Mean.h:95
Definition: Mean.h:29
Definition: Mean.h:163
Constrains a type to have a member function getAVX (size_t) const.
Definition: ContainerAndExpressionConcepts.h:92
Constrains a type to have a member function getNeon (size_t) const.
Definition: ContainerAndExpressionConcepts.h:84
Constrains a type to have a member function getSSE (size_t) const.
Definition: ContainerAndExpressionConcepts.h:100
Constrains a type to represent a real valued 32 bit integer number.
Definition: NumericTypeConcepts.h:57
Constrains a type to represent a real valued or std::complex number type.
Definition: NumericTypeConcepts.h:49
Constrains a type to represent a real valued floating point number.
Definition: NumericTypeConcepts.h:83
Constrains a type to represent a real or complex valued floating point number.
Definition: NumericTypeConcepts.h:91
A combined concept to check if Apple Accelerate is a suitable option for a floating point vector redu...
Definition: ContainerAndExpressionConcepts.h:280
A combined concept to check if Intel IPP is a suitable option for a floating point vector reduction o...
Definition: ContainerAndExpressionConcepts.h:304
A combined concept to check if Intel IPP is a suitable option for a real or complex floating point ve...
Definition: ContainerAndExpressionConcepts.h:308
constexpr ExpressionChainBuilder< expressions::RootMeanSquare > rms
Computes the square root of the mean value of the squared source values.
Definition: Mean.h:260
constexpr ExpressionChainBuilder< expressions::Mean > mean
Computes the mean value of the source values.
Definition: Mean.h:248
constexpr ExpressionChainBuilder< expressions::Sum > sum
Computes the sum of the source values.
Definition: Sum.h:118
constexpr ExpressionChainBuilder< expressions::MeanSquare > meanSquare
Computes the mean value of the squared source values.
Definition: Mean.h:254
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:157
The base class to every expression template.
Definition: ExpressionTemplate.h:37
Definition: NeonRegister.h:28
Definition: SSERegister.h:28