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 // Unlike most expressions, Accelerate is preferred over IPP here on purpose: Accelerate has a native mean square,
111 // while the IPP version below is emulated via the L2 norm, which squares a square root and loses precision.
112 VCTR_FORCEDINLINE value_type reduceVectorOp() const
114 {
115 return Expression::Accelerate::meanSquare (src.data(), size());
116 };
117
118 VCTR_FORCEDINLINE value_type reduceVectorOp() const
120 {
121 auto l2Norm = Expression::IPP::l2Norm (src.data(), sizeToInt (size()));
122 return (l2Norm * l2Norm) / value_type (size());
123 };
124
125 //==============================================================================
126 VCTR_FORWARD_PREPARE_SIMD_EVALUATION_UNARY_EXPRESSION_MEMBER_FUNCTIONS
127
128 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
130 {
131 auto s = src.getNeon (i);
132 s = Expression::Neon::mul (s, s);
133 result = Expression::Neon::add (result, s);
134 }
135
136 VCTR_FORCEDINLINE VCTR_TARGET ("fma") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
137 requires Config::archX64 && has::getAVX<SrcType> && is::realFloatNumber<value_type>
138 {
139 auto s = src.getAVX (i);
140 s = Expression::AVX::mul (s, s);
141 result = Expression::AVX::add (result, s);
142 }
143
144 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
145 requires Config::archX64 && has::getSSE<SrcType> && is::realFloatNumber<value_type>
146 {
147 auto s = src.getSSE (i);
148 s = Expression::SSE::mul (s, s);
149 result = Expression::SSE::add (result, s);
150 }
151
152 //==============================================================================
153 template <size_t n>
154 VCTR_FORCEDINLINE constexpr value_type finalizeReduction (const std::array<value_type, n>& sums) const
155 {
156 auto sum = n == 1 ? sums[0] : std::reduce (sums.begin(), sums.end());
157
158 return value_type (sum / FloatType<RealType<value_type>> (size()));
159 }
160};
161
162template <size_t extent, class SrcType>
165{
166public:
167 using value_type = ValueType<SrcType>;
168
169 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (RootMeanSquare, src)
170
171 static constexpr value_type reductionResultInitValue = 0;
172
173 VCTR_FORCEDINLINE constexpr void reduceElementWise (value_type& result, size_t i) const
174 {
175 auto s = src[i];
176 result += s * s;
177 }
178
179 //==============================================================================
180 // Unlike most expressions, Accelerate is preferred over IPP here on purpose: Accelerate has a native RMS, while the
181 // IPP version below is emulated via the L2 norm.
182 VCTR_FORCEDINLINE value_type reduceVectorOp() const
184 {
185 return Expression::Accelerate::rms (src.data(), size());
186 };
187
188 VCTR_FORCEDINLINE value_type reduceVectorOp() const
190 {
191 auto l2Norm = Expression::IPP::l2Norm (src.data(), sizeToInt (size()));
192 return std::sqrt ((l2Norm * l2Norm) / value_type (size()));
193 };
194
195 //==============================================================================
196 VCTR_FORWARD_PREPARE_SIMD_EVALUATION_UNARY_EXPRESSION_MEMBER_FUNCTIONS
197
198 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
200 {
201 auto s = src.getNeon (i);
202 s = Expression::Neon::mul (s, s);
203 result = Expression::Neon::add (result, s);
204 }
205
206 VCTR_FORCEDINLINE VCTR_TARGET ("fma") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
207 requires Config::archX64 && has::getAVX<SrcType> && is::realFloatNumber<value_type>
208 {
209 auto s = src.getAVX (i);
210 s = Expression::AVX::mul (s, s);
211 result = Expression::AVX::add (result, s);
212 }
213
214 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
215 requires Config::archX64 && has::getSSE<SrcType> && is::realFloatNumber<value_type>
216 {
217 auto s = src.getSSE (i);
218 s = Expression::SSE::mul (s, s);
219 result = Expression::SSE::add (result, s);
220 }
221
222 //==============================================================================
223 template <size_t n>
224 VCTR_FORCEDINLINE constexpr value_type finalizeReduction (const std::array<value_type, n>& squaredSums) const
225 {
226 auto squaredSum = n == 1 ? squaredSums[0] : std::reduce (squaredSums.begin(), squaredSums.end());
227 auto meanSquaredSum = squaredSum / RealType<value_type> (src.size());
228
229 #if VCTR_USE_GCEM
231 {
232 if (std::is_constant_evaluated())
233 {
234 return value_type (gcem::sqrt (meanSquaredSum));
235 }
236 }
237 #endif
238
239 return value_type (std::sqrt (meanSquaredSum));
240 }
241};
242
243} // namespace vctr::expressions
244
245namespace vctr
246{
247
253
259
265
266} // namespace vctr
Definition: Mean.h:95
Definition: Mean.h:29
Definition: Mean.h:165
Constrains a type to have a member function getAVX (size_t) const.
Definition: ContainerAndExpressionConcepts.h:98
Constrains a type to have a member function getNeon (size_t) const.
Definition: ContainerAndExpressionConcepts.h:90
Constrains a type to have a member function getSSE (size_t) const.
Definition: ContainerAndExpressionConcepts.h:106
Constrains a type to represent a real valued 32 bit integer number.
Definition: NumericTypeConcepts.h:61
Constrains a type to represent a real valued or std::complex number type.
Definition: NumericTypeConcepts.h:53
Constrains a type to represent a real valued floating point number.
Definition: NumericTypeConcepts.h:87
Constrains a type to represent a real or complex valued floating point number.
Definition: NumericTypeConcepts.h:95
A combined concept to check if Apple Accelerate is a suitable option for a floating point vector redu...
Definition: ContainerAndExpressionConcepts.h:333
A combined concept to check if Intel IPP is a suitable option for a floating point vector reduction o...
Definition: ContainerAndExpressionConcepts.h:357
A combined concept to check if Intel IPP is a suitable option for a real or complex floating point ve...
Definition: ContainerAndExpressionConcepts.h:361
constexpr ExpressionChainBuilder< expressions::RootMeanSquare > rms
Computes the square root of the mean value of the squared source values.
Definition: Mean.h:264
constexpr ExpressionChainBuilder< expressions::Mean > mean
Computes the mean value of the source values.
Definition: Mean.h:252
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:258
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:170
The base class to every expression template.
Definition: ExpressionTemplate.h:37
Definition: NeonRegister.h:28
Definition: SSERegister.h:28