VCTR
Loading...
Searching...
No Matches
Min.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 SrcType>
27requires std::totally_ordered<ValueType<SrcType>>
29{
30public:
31 using value_type = ValueType<SrcType>;
32
33 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (Min, src)
34
35 static constexpr value_type reductionResultInitValue = std::numeric_limits<value_type>::max();
36
37 VCTR_FORCEDINLINE void reduceElementWise (value_type& result, size_t i) const
38 {
39 result = std::min (result, src[i]);
40 }
41
42 //==============================================================================
43 VCTR_FORCEDINLINE value_type reduceVectorOp() const
45 {
46 return Expression::Accelerate::min (src.data(), size());
47 };
48
49 VCTR_FORCEDINLINE value_type reduceVectorOp() const
51 {
52 return Expression::IPP::min (src.data(), size());
53 };
54
55 //==============================================================================
56 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
58 {
59 result = Expression::Neon::min (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::min (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::min (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::min (result, src.getSSE (i));
78 }
79
80 //==============================================================================
81 template <size_t n>
82 VCTR_FORCEDINLINE static value_type finalizeReduction (const std::array<value_type, n>& minima)
83 {
84 if constexpr (n == 1)
85 return minima[0];
86
87 return *std::min_element (minima.begin(), minima.end());
88 }
89};
90
91template <size_t extent, class SrcType>
94{
95public:
96 using value_type = RealType<ValueType<SrcType>>;
97
98 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (MinAbs, src)
99
100 static constexpr value_type reductionResultInitValue = std::numeric_limits<value_type>::max();
101
102 VCTR_FORCEDINLINE void reduceElementWise (value_type& result, size_t i) const
104 {
105 result = std::min (result, std::abs (src[i]));
106 }
107
108 VCTR_FORCEDINLINE void reduceElementWise (value_type& result, size_t i) const
110 {
111 result = std::min (result, src[i]);
112 }
113
114 //==============================================================================
115 VCTR_FORCEDINLINE value_type reduceVectorOp() const
117 {
118 return Expression::IPP::minAbs (src.data(), size());
119 };
120
121 //==============================================================================
122 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
123 requires Config::archARM && has::getNeon<SrcType> && (is::realFloatNumber<value_type> || std::same_as<int32_t, value_type>)
124 {
125 result = Expression::Neon::min (result, Expression::Neon::abs (src.getNeon (i)));
126 }
127
128 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
129 requires Config::archARM && has::getNeon<SrcType> && std::same_as<uint32_t, value_type>
130 {
131 result = Expression::Neon::min (result, src.getNeon (i));
132 }
133
134 VCTR_FORCEDINLINE VCTR_TARGET ("avx") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
135 requires Config::archX64 && has::getAVX<SrcType> && is::realFloatNumber<value_type>
136 {
137 static const auto avxSignBit = Expression::AVX::broadcast (typename Expression::CommonElement::Type (-0.0));
138
139 result = Expression::AVX::min (result, Expression::AVX::andNot (avxSignBit, src.getAVX (i)));
140 }
141
142 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
143 requires Config::archX64 && has::getAVX<SrcType> && std::same_as<int32_t, value_type>
144 {
145 result = Expression::AVX::min (result, Expression::AVX::abs (src.getAVX (i)));
146 }
147
148 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
149 requires Config::archX64 && has::getAVX<SrcType> && std::same_as<uint32_t, value_type>
150 {
151 result = Expression::AVX::min (result, src.getAVX (i));
152 }
153
154 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
155 requires Config::archX64 && has::getSSE<SrcType> && is::realFloatNumber<value_type>
156 {
157 static const auto sseSignBit = Expression::SSE::broadcast (typename Expression::CommonElement::Type (-0.0));
158
159 result = Expression::SSE::min (result, Expression::SSE::andNot (sseSignBit, src.getSSE (i)));
160 }
161
162 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
163 requires Config::archX64 && has::getSSE<SrcType> && std::same_as<int32_t, value_type>
164 {
165 result = Expression::SSE::min (result, Expression::SSE::abs (src.getSSE (i)));
166 }
167
168 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
169 requires Config::archX64 && has::getSSE<SrcType> && std::same_as<uint32_t, value_type>
170 {
171 result = Expression::SSE::min (result, src.getSSE (i));
172 }
173
174 //==============================================================================
175 template <size_t n>
176 VCTR_FORCEDINLINE static value_type finalizeReduction (const std::array<value_type, n>& minima)
177 {
178 if constexpr (n == 1)
179 return minima[0];
180
181 return *std::min_element (minima.begin(), minima.end());
182 }
183};
184
185} // namespace vctr::expressions
186
187namespace vctr
188{
189
195
201}
Definition: Min.h:94
Definition: Min.h:29
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 valued signed number (e.g.
Definition: NumericTypeConcepts.h:61
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
constexpr ExpressionChainBuilder< expressions::MinAbs > minAbs
Computes the minimum value of the absolute value of the source values.
Definition: Min.h:200
constexpr ExpressionChainBuilder< expressions::Min > min
Computes the minimum value of the source values.
Definition: Min.h:194
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::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
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