VCTR
Loading...
Searching...
No Matches
Clamp.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::detail
24{
25/* The x86 min and max instructions return their second operand if both compare equal or one of them is NaN. Clamp
26 passes x as the second operand, so that NaN and the sign of zero pass through like with std::min and std::max.
27 The Neon float min and max instructions however propagate NaN and order -0 below +0, so a lane that equals the bound
28 would take the sign of the bound. A compare and blend keeps x in these cases, like the scalar implementation.
29 */
30
32template <class T>
33NeonRegister<T> neonClampLow (NeonRegister<T> x, NeonRegister<T> lowerBound)
34{
35 if constexpr (is::realFloatNumber<T>)
36 return NeonRegister<T>::bitwiseBlend (x, lowerBound, NeonRegister<T>::template compare<CompareOp::less> (x, lowerBound));
37 else
38 return NeonRegister<T>::max (lowerBound, x);
39}
40
42template <class T>
43NeonRegister<T> neonClampHigh (NeonRegister<T> x, NeonRegister<T> upperBound)
44{
45 if constexpr (is::realFloatNumber<T>)
46 return NeonRegister<T>::bitwiseBlend (x, upperBound, NeonRegister<T>::template compare<CompareOp::less> (upperBound, x));
47 else
48 return NeonRegister<T>::min (upperBound, x);
49}
50} // namespace vctr::detail
51
52namespace vctr::expressions
53{
54
55template <size_t extent, class SrcType, is::constantWithType<bool> ClampLow, is::constantWithType<bool> ClampHigh>
57{
58public:
59 using value_type = ValueType<SrcType>;
60
61 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (Clamp, src)
62
63
64 static constexpr bool supportsSIMDMinMax = Expression::CommonElement::isRealFloat || Expression::CommonElement::isInt32 || Expression::CommonElement::isUint32;
65
66 static constexpr bool clampLow = ClampLow::value;
67 static constexpr bool clampHigh = ClampHigh::value;
68
69 constexpr void applyRuntimeArgs (value_type newLowerBound, value_type newUpperBound)
70 {
71 lowerBound = newLowerBound;
72 upperBound = newUpperBound;
73 }
74
75 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
76 {
77 assertBoundsAreSet();
78
79 if constexpr (clampLow && clampHigh)
80 return std::clamp (src[i], lowerBound, upperBound);
81
82 if constexpr (clampLow && ! clampHigh)
83 return std::max (src[i], lowerBound);
84
85 if constexpr (! clampLow && clampHigh)
86 return std::min (src[i], upperBound);
87 }
88
89 //==============================================================================
90 // Platform Vector Operation Implementation
91 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
93 {
94 assertBoundsAreSet();
95
96 if constexpr (clampLow && ! clampHigh)
97 Expression::Accelerate::clampLow (src.evalNextVectorOpInExpressionChain (dst), lowerBound, dst, size());
98
99 if constexpr (clampHigh)
100 {
101 auto l = clampLow ? lowerBound : std::numeric_limits<value_type>::lowest();
102 Expression::Accelerate::clamp (src.evalNextVectorOpInExpressionChain (dst), l, upperBound, dst, size());
103 }
104
105 return dst;
106 }
107
108 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
109 requires is::suitableForIppRealFloatVectorOp<SrcType, value_type, detail::preferIfIppAndAccelerateAreAvailable>
110 {
111 assertBoundsAreSet();
112 const auto* s = src.evalNextVectorOpInExpressionChain (dst);
113
114 if constexpr (clampLow)
115 {
116 Expression::IPP::clampLow (s, lowerBound, dst, sizeToInt (size()));
117
118 if constexpr (clampHigh)
119 s = dst;
120 }
121
122 if constexpr (clampHigh)
123 Expression::IPP::clampHigh (s, upperBound, dst, sizeToInt (size()));
124
125 return dst;
126 }
127
128 //==============================================================================
129 // AVX Implementation
130 VCTR_FORCEDINLINE VCTR_TARGET ("avx") void prepareAVXEvaluation() const
131 requires (has::prepareAVXEvaluation<SrcType> && supportsSIMDMinMax && Expression::CommonElement::isRealFloat)
132 {
133 src.prepareAVXEvaluation();
134 assertBoundsAreSet();
135
136 if constexpr (clampLow)
137 lowerBoundSIMD.avx = Expression::AVX::broadcast (lowerBound);
138
139 if constexpr (clampHigh)
140 upperBoundSIMD.avx = Expression::AVX::broadcast (upperBound);
141 }
142
143 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") void prepareAVXEvaluation() const
144 requires (has::prepareAVXEvaluation<SrcType> && supportsSIMDMinMax && Expression::CommonElement::isInt)
145 {
146 src.prepareAVXEvaluation();
147 assertBoundsAreSet();
148
149 if constexpr (clampLow)
150 lowerBoundSIMD.avx = Expression::AVX::broadcast (lowerBound);
151
152 if constexpr (clampHigh)
153 upperBoundSIMD.avx = Expression::AVX::broadcast (upperBound);
154 }
155
156 VCTR_FORCEDINLINE VCTR_TARGET ("fma") AVXRegister<value_type> getAVX (size_t i) const
157 requires (archX64 && has::getAVX<SrcType> && Expression::allElementTypesSame && Expression::CommonElement::isRealFloat)
158 {
159 auto x = src.getAVX (i);
160
161 if constexpr (clampLow)
162 x = Expression::AVX::max (lowerBoundSIMD.avx, x);
163
164 if constexpr (clampHigh)
165 x = Expression::AVX::min (upperBoundSIMD.avx, x);
166
167 return x;
168 }
169
170 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") AVXRegister<value_type> getAVX (size_t i) const
171 requires (archX64 && has::getAVX<SrcType> && Expression::allElementTypesSame && supportsSIMDMinMax && Expression::CommonElement::isInt)
172 {
173 auto x = src.getAVX (i);
174
175 if constexpr (clampLow)
176 x = Expression::AVX::max (lowerBoundSIMD.avx, x);
177
178 if constexpr (clampHigh)
179 x = Expression::AVX::min (upperBoundSIMD.avx, x);
180
181 return x;
182 }
183
184 //==============================================================================
185 // SSE Implementation
186 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void prepareSSEEvaluation() const
187 requires (has::prepareSSEEvaluation<SrcType> && supportsSIMDMinMax)
188 {
189 src.prepareSSEEvaluation();
190 assertBoundsAreSet();
191
192 if constexpr (clampLow)
193 lowerBoundSIMD.sse = Expression::SSE::broadcast (lowerBound);
194
195 if constexpr (clampHigh)
196 upperBoundSIMD.sse = Expression::SSE::broadcast (upperBound);
197 }
198
199 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") SSERegister<value_type> getSSE (size_t i) const
200 requires (archX64 && has::getSSE<SrcType> && Expression::allElementTypesSame && supportsSIMDMinMax)
201 {
202 auto x = src.getSSE (i);
203
204 if constexpr (clampLow)
205 x = Expression::SSE::max (lowerBoundSIMD.sse, x);
206
207 if constexpr (clampHigh)
208 x = Expression::SSE::min (upperBoundSIMD.sse, x);
209
210 return x;
211 }
212
213 //==============================================================================
214 // Neon Implementation
215 void prepareNeonEvaluation() const
216 requires (archARM && has::prepareNeonEvaluation<SrcType> && supportsSIMDMinMax)
217 {
218 src.prepareNeonEvaluation();
219 assertBoundsAreSet();
220
221 if constexpr (clampLow)
222 lowerBoundSIMD.neon = Expression::Neon::broadcast (lowerBound);
223
224 if constexpr (clampHigh)
225 upperBoundSIMD.neon = Expression::Neon::broadcast (upperBound);
226 }
227
228 NeonRegister<value_type> getNeon (size_t i) const
229 requires (archARM && has::getNeon<SrcType> && Expression::allElementTypesSame && supportsSIMDMinMax)
230 {
231 auto x = src.getNeon (i);
232
233 if constexpr (clampLow)
234 x = detail::neonClampLow (x, lowerBoundSIMD.neon);
235
236 if constexpr (clampHigh)
237 x = detail::neonClampHigh (x, upperBoundSIMD.neon);
238
239 return x;
240 }
241
242private:
243 void assertBoundsAreSet() const
244 {
245 // If you hit one of the assertions here, applyRuntimeArgs has not been called as expected.
246 // This should not happen under normal circumstances
247 if constexpr (clampLow)
248 VCTR_ASSERT (lowerBound != std::numeric_limits<value_type>::max());
249
250 if constexpr (clampHigh)
251 VCTR_ASSERT (upperBound != std::numeric_limits<value_type>::max());
252 }
253
254 value_type lowerBound = std::numeric_limits<value_type>::max();
255 value_type upperBound = std::numeric_limits<value_type>::max();
256 mutable SIMDRegisterUnion<Expression> upperBoundSIMD {};
257 mutable SIMDRegisterUnion<Expression> lowerBoundSIMD {};
258};
259
260template <size_t extent, class SrcType>
261using ClampLow = Clamp<extent, SrcType, std::true_type, std::false_type>;
262
263template <size_t extent, class SrcType>
264using ClampHigh = Clamp<extent, SrcType, std::false_type, std::true_type>;
265
266template <size_t extent, class SrcType>
267using ClampLowHigh = Clamp<extent, SrcType, std::true_type, std::true_type>;
268
269template <size_t extent, class SrcType, is::constant LowerBound, is::constant UpperBound>
271{
272public:
273 using value_type = ValueType<SrcType>;
274
275 static constexpr value_type lowerBound = LowerBound::value;
276 static constexpr value_type upperBound = UpperBound::value;
277
278 static constexpr bool clampLow = ! is::disabledConstant<LowerBound>;
279 static constexpr bool clampHigh = ! is::disabledConstant<UpperBound>;
280
281 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (ClampByConstant, src)
282
283
284 static constexpr bool supportsSIMDMinMax = Expression::CommonElement::isRealFloat || Expression::CommonElement::isInt32 || Expression::CommonElement::isUint32;
285
286 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
287 {
288 if constexpr (clampLow && clampHigh)
289 return std::clamp (src[i], lowerBound, upperBound);
290
291 if constexpr (clampLow && ! clampHigh)
292 return std::max (src[i], lowerBound);
293
294 if constexpr (! clampLow && clampHigh)
295 return std::min (src[i], upperBound);
296
297 return std::max (lowerBound, src[i]);
298 }
299
300 //==============================================================================
301 // AVX Implementation
302 VCTR_FORCEDINLINE VCTR_TARGET ("avx") void prepareAVXEvaluation() const
303 requires (has::prepareAVXEvaluation<SrcType> && supportsSIMDMinMax && Expression::CommonElement::isRealFloat)
304 {
305 src.prepareAVXEvaluation();
306
307 if constexpr (clampLow)
308 lowerBoundSIMD.avx = Expression::AVX::broadcast (lowerBound);
309
310 if constexpr (clampHigh)
311 upperBoundSIMD.avx = Expression::AVX::broadcast (upperBound);
312 }
313
314 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") void prepareAVXEvaluation() const
315 requires (has::prepareAVXEvaluation<SrcType> && supportsSIMDMinMax && Expression::CommonElement::isInt)
316 {
317 src.prepareAVXEvaluation();
318
319 if constexpr (clampLow)
320 lowerBoundSIMD.avx = Expression::AVX::broadcast (lowerBound);
321
322 if constexpr (clampHigh)
323 upperBoundSIMD.avx = Expression::AVX::broadcast (upperBound);
324 }
325
326 VCTR_FORCEDINLINE VCTR_TARGET ("fma") AVXRegister<value_type> getAVX (size_t i) const
327 requires (archX64 && has::getAVX<SrcType> && Expression::allElementTypesSame && Expression::CommonElement::isRealFloat)
328 {
329 auto x = src.getAVX (i);
330
331 if constexpr (clampLow)
332 x = Expression::AVX::max (lowerBoundSIMD.avx, x);
333
334 if constexpr (clampHigh)
335 x = Expression::AVX::min (upperBoundSIMD.avx, x);
336
337 return x;
338 }
339
340 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") AVXRegister<value_type> getAVX (size_t i) const
341 requires (archX64 && has::getAVX<SrcType> && Expression::allElementTypesSame && supportsSIMDMinMax && Expression::CommonElement::isInt)
342 {
343 auto x = src.getAVX (i);
344
345 if constexpr (clampLow)
346 x = Expression::AVX::max (lowerBoundSIMD.avx, x);
347
348 if constexpr (clampHigh)
349 x = Expression::AVX::min (upperBoundSIMD.avx, x);
350
351 return x;
352 }
353
354 //==============================================================================
355 // SSE Implementation
356 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void prepareSSEEvaluation() const
357 requires (has::prepareSSEEvaluation<SrcType> && supportsSIMDMinMax)
358 {
359 src.prepareSSEEvaluation();
360
361 if constexpr (clampLow)
362 lowerBoundSIMD.sse = Expression::SSE::broadcast (lowerBound);
363
364 if constexpr (clampHigh)
365 upperBoundSIMD.sse = Expression::SSE::broadcast (upperBound);
366 }
367
368 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") SSERegister<value_type> getSSE (size_t i) const
369 requires (archX64 && has::getSSE<SrcType> && Expression::allElementTypesSame && supportsSIMDMinMax)
370 {
371 auto x = src.getSSE (i);
372
373 if constexpr (clampLow)
374 x = Expression::SSE::max (lowerBoundSIMD.sse, x);
375
376 if constexpr (clampHigh)
377 x = Expression::SSE::min (upperBoundSIMD.sse, x);
378
379 return x;
380 }
381
382 //==============================================================================
383 // Neon Implementation
384 void prepareNeonEvaluation() const
385 requires (archARM && has::prepareNeonEvaluation<SrcType> && supportsSIMDMinMax)
386 {
387 src.prepareNeonEvaluation();
388
389 if constexpr (clampLow)
390 lowerBoundSIMD.neon = Expression::Neon::broadcast (lowerBound);
391
392 if constexpr (clampHigh)
393 upperBoundSIMD.neon = Expression::Neon::broadcast (upperBound);
394 }
395
396 NeonRegister<value_type> getNeon (size_t i) const
397 requires (archARM && has::getNeon<SrcType> && Expression::allElementTypesSame && supportsSIMDMinMax)
398 {
399 auto x = src.getNeon (i);
400
401 if constexpr (clampLow)
402 x = detail::neonClampLow (x, lowerBoundSIMD.neon);
403
404 if constexpr (clampHigh)
405 x = detail::neonClampHigh (x, upperBoundSIMD.neon);
406
407 return x;
408 }
409
410 //==============================================================================
411 // Platform Vector Operation Implementation
412 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
413 requires is::suitableForAccelerateRealFloatVectorOp<SrcType, value_type, detail::dontPreferIfIppAndAccelerateAreAvailable>
414 {
415 if constexpr (clampLow && ! clampHigh)
416 Expression::Accelerate::clampLow (src.evalNextVectorOpInExpressionChain (dst), lowerBound, dst, size());
417
418 if constexpr (clampHigh)
419 {
420 auto l = clampLow ? lowerBound : std::numeric_limits<value_type>::lowest();
421 Expression::Accelerate::clamp (src.evalNextVectorOpInExpressionChain (dst), l, upperBound, dst, size());
422 }
423
424 return dst;
425 }
426
427 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
428 requires is::suitableForIppRealFloatVectorOp<SrcType, value_type, detail::preferIfIppAndAccelerateAreAvailable>
429 {
430 const auto* s = src.evalNextVectorOpInExpressionChain (dst);
431
432 if constexpr (clampLow)
433 {
434 Expression::IPP::clampLow (s, lowerBound, dst, sizeToInt (size()));
435
436 if constexpr (clampHigh)
437 s = dst;
438 }
439
440 if constexpr (clampHigh)
441 Expression::IPP::clampHigh (s, upperBound, dst, sizeToInt (size()));
442
443 return dst;
444 }
445
446private:
447 mutable SIMDRegisterUnion<Expression> upperBoundSIMD {};
448 mutable SIMDRegisterUnion<Expression> lowerBoundSIMD {};
449};
450
451} // namespace vctr::expressions
452
453namespace vctr
454{
455
463template <class T>
464constexpr auto clampLow (T lowerBound)
465{
466 return makeExpressionChainBuilderWithRuntimeArgs<expressions::ClampLow> (lowerBound, std::numeric_limits<T>::max());
467}
468
476template <class T>
477constexpr auto clampHigh (T upperBound)
478{
479 return makeExpressionChainBuilderWithRuntimeArgs<expressions::ClampHigh> (std::numeric_limits<T>::max(), upperBound);
480}
481
489template <class T>
490constexpr auto clamp (T lowerBound, T upperBound)
491{
492 return makeExpressionChainBuilderWithRuntimeArgs<expressions::ClampLowHigh> (lowerBound, upperBound);
493}
494
502template <is::range Range>
503constexpr auto clamp (const Range& range)
504{
505 return makeExpressionChainBuilderWithRuntimeArgs<expressions::ClampLowHigh> (range.getStart(), range.getEnd());
506}
507
508
517template <auto lowerBound>
519
528template <auto upperBound>
530
539template <auto lowerBound, auto upperBound>
541
546template <const auto& range>
547requires is::range<decltype (range)>
549
550} // namespace vctr
A simple range class.
Definition: Range.h:32
constexpr ValueType getStart() const noexcept
Returns the start of the range.
Definition: Range.h:74
constexpr ValueType getEnd() const noexcept
Returns the end of the range.
Definition: Range.h:80
Definition: Clamp.h:271
static constexpr bool supportsSIMDMinMax
All SIMD register types offer min and max for these types.
Definition: Clamp.h:284
Definition: Clamp.h:57
static constexpr bool supportsSIMDMinMax
All SIMD register types offer min and max for these types.
Definition: Clamp.h:64
Constrains a type to be of the type DisabledConstant.
Definition: NumericTypeConcepts.h:111
Constrains the type to be a range, this is a class with a getStart, getLength and getEnd member funct...
Definition: GenericConcepts.h:127
A combined concept to check if Apple Accelerate is a suitable option for a real valued floating point...
Definition: ContainerAndExpressionConcepts.h:309
constexpr ExpressionChainBuilder< expressions::ClampByConstant, DisabledConstant, Constant< upperBound > > clampHighByConstant
Ensures that the elements are not higher than upperBound.
Definition: Clamp.h:529
constexpr auto clampLow(T lowerBound)
Ensures that the elements are not lower than lowerBound.
Definition: Clamp.h:464
constexpr ExpressionChainBuilder< expressions::ClampByConstant, Constant< lowerBound >, Constant< upperBound > > clampByConstant
Ensures that the elements are not lower than lowerBound and not higher than upperBound.
Definition: Clamp.h:540
constexpr auto clampHigh(T upperBound)
Ensures that the elements are not greater than upperBound.
Definition: Clamp.h:477
constexpr ExpressionChainBuilder< expressions::ClampByConstant, Constant< lowerBound >, DisabledConstant > clampLowByConstant
Ensures that the elements are not lower than lowerBound.
Definition: Clamp.h:518
constexpr ExpressionChainBuilder< expressions::ClampByConstant, ConstantRangeStart< range >, ConstantRangeEnd< range > > clampToRange
Ensures that the elements are within the range.
Definition: Clamp.h:548
constexpr auto clamp(T lowerBound, T upperBound)
Ensures that the elements are not lower than lowerBound and not higher than upperBound.
Definition: Clamp.h:490
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
int sizeToInt(size_t size)
Casts the size_t argument to an int.
Definition: PlatformVectorOpsHelpers.h:27
Evaluates the return value of getEnd called on the constant reference template argument and wraps tha...
Definition: ExpressionChainBuilder.h:137
Evaluates the return value of getStart called on the constant reference template argument and wraps t...
Definition: ExpressionChainBuilder.h:127
A helper struct to pass a constant as argument wrapped in a struct with a single public static conste...
Definition: ExpressionChainBuilder.h:106
A helper struct to indicate that a constant template should be considered disabled.
Definition: ExpressionChainBuilder.h:144
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