35 #ifndef _BLAZE_MATH_EXPRESSIONS_DVECREDUCEEXPR_H_ 36 #define _BLAZE_MATH_EXPRESSIONS_DVECREDUCEEXPR_H_ 79 struct DVecReduceExprHelper
83 using CT = RemoveReference_t< CompositeType_t<VT> >;
86 using ET = ElementType_t<CT>;
90 static constexpr
bool value =
92 If_t< HasSIMDEnabled_v<OP>, GetSIMDEnabled<OP,ET,ET>, HasLoad<OP> >::value );
120 template<
typename VT
123 inline auto dvecreduce(
const DenseVector<VT,TF>& dv, OP op )
124 -> DisableIf_t< DVecReduceExprHelper<VT,OP>::value, ElementType_t<VT> >
126 using CT = CompositeType_t<VT>;
127 using ET = ElementType_t<VT>;
129 const size_t N( (~dv).
size() );
131 if( N == 0UL )
return ET{};
132 if( N == 1UL )
return (~dv)[0UL];
138 ET redux1( tmp[0UL] );
139 ET redux2( tmp[1UL] );
142 for( ; (i+4UL) <= N; i+=4UL ) {
143 redux1 = op( op( redux1, tmp[i ] ), tmp[i+1UL] );
144 redux2 = op( op( redux2, tmp[i+2UL] ), tmp[i+3UL] );
146 for( ; (i+2UL) <= N; i+=2UL ) {
147 redux1 = op( redux1, tmp[i ] );
148 redux2 = op( redux2, tmp[i+1UL] );
151 redux1 = op( redux1, tmp[i] );
154 return op( redux1, redux2 );
173 template<
typename VT
176 inline auto dvecreduce(
const DenseVector<VT,TF>& dv, OP op )
177 -> EnableIf_t< DVecReduceExprHelper<VT,OP>::value, ElementType_t<VT> >
179 using CT = CompositeType_t<VT>;
180 using ET = ElementType_t<VT>;
182 const size_t N( (~dv).
size() );
184 if( N == 0UL )
return ET{};
196 const size_t ipos( N &
size_t(-SIMDSIZE) );
199 SIMDTrait_t<ET> xmm1( tmp.load(0UL) );
201 if( N >= SIMDSIZE*2UL )
203 SIMDTrait_t<ET> xmm2( tmp.load(SIMDSIZE) );
204 size_t i( SIMDSIZE*2UL );
206 for( ; (i+SIMDSIZE) < ipos; i+=SIMDSIZE*2UL ) {
207 xmm1 = op( xmm1, tmp.load(i ) );
208 xmm2 = op( xmm2, tmp.load(i+SIMDSIZE) );
210 for( ; i<ipos; i+=SIMDSIZE ) {
211 xmm1 = op( xmm1, tmp.load(i) );
214 xmm1 = op( xmm1, xmm2 );
217 redux =
reduce( xmm1, op );
219 for(
size_t i=ipos; i<N; ++i ) {
220 redux = op( redux, tmp[i] );
225 for(
size_t i=1UL; i<N; ++i ) {
226 redux = op( redux, tmp[i] );
248 template<
typename VT
250 inline auto dvecreduce(
const DenseVector<VT,TF>& dv, Add )
251 -> EnableIf_t< DVecReduceExprHelper<VT,Add>::value, ElementType_t<VT> >
253 using CT = CompositeType_t<VT>;
254 using ET = ElementType_t<VT>;
256 const size_t N( (~dv).
size() );
258 if( N == 0UL )
return ET{};
264 constexpr
bool remainder( !usePadding || !
IsPadded_v< RemoveReference_t<CT> > );
269 if( !BLAZE_CLANG_COMPILER && !remainder )
271 SIMDTrait_t<ET> xmm1, xmm2;
274 for( ; (i+SIMDSIZE) < N; i+=SIMDSIZE*2UL ) {
275 xmm1 += tmp.load(i );
276 xmm2 += tmp.load(i+SIMDSIZE);
282 redux =
sum( xmm1 + xmm2 );
284 else if( !remainder || N >= SIMDSIZE )
286 const size_t ipos( ( remainder )?( N &
size_t(-SIMDSIZE) ):( N ) );
289 SIMDTrait_t<ET> xmm1( tmp.load(0UL) );
291 if( remainder ? N >= SIMDSIZE*2UL : N > SIMDSIZE )
293 SIMDTrait_t<ET> xmm2( tmp.load(SIMDSIZE) );
294 size_t i( SIMDSIZE*2UL );
296 for( ; (i+SIMDSIZE) < ipos; i+=SIMDSIZE*2UL ) {
297 xmm1 += tmp.load(i );
298 xmm2 += tmp.load(i+SIMDSIZE);
300 for( ; i<ipos; i+=SIMDSIZE ) {
309 for(
size_t i=ipos; remainder && i<N; ++i ) {
315 for(
size_t i=1UL; i<N; ++i ) {
337 template<
typename VT
339 inline auto dvecreduce(
const DenseVector<VT,TF>& dv, Min )
340 -> EnableIf_t< IsUniform_v<VT>, ElementType_t<VT> >
359 template<
typename VT
361 inline auto dvecreduce(
const DenseVector<VT,TF>& dv, Max )
362 -> EnableIf_t< IsUniform_v<VT>, ElementType_t<VT> >
399 template<
typename VT
406 return dvecreduce( ~dv, op );
428 template<
typename VT
455 template<
typename VT
483 template<
typename VT
511 template<
typename VT
538 template<
typename VT
542 if( (~dv).
size() < 2UL )
547 const size_t size( a.size() );
551 for(
size_t i=1UL; i<
size; ++i ) {
555 min = std::move( cur );
580 template<
typename VT
584 if( (~dv).
size() < 2UL )
589 const size_t size( a.size() );
593 for(
size_t i=1UL; i<
size; ++i ) {
597 max = std::move( cur );
Header file for auxiliary alias declarations.
Header file for the HasLoad type trait.
Header file for basic type definitions.
Header file for the Add functor.
decltype(auto) prod(const DenseMatrix< MT, SO > &dm)
Reduces the given dense matrix by means of multiplication.
Definition: DMatReduceExpr.h:2220
Header file for the DenseVector base class.
Generic wrapper for the addition operator.
Definition: Add.h:83
size_t argmax(const DenseVector< VT, TF > &dv)
Returns the index of the first largest element of the dense vector.
Definition: DVecReduceExpr.h:582
decltype(auto) reduce(const DenseMatrix< MT, SO > &dm, OP op)
Performs a custom reduction operation on the given dense matrix.
Definition: DMatReduceExpr.h:2017
Header file for the DisableIf class template.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1162
decltype(auto) sum(const DenseMatrix< MT, SO > &dm)
Reduces the given dense matrix by means of addition.
Definition: DMatReduceExpr.h:2147
Header file for the Mult functor.
Header file for all SIMD functionality.
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:76
Compiler-specific system settings.
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1198
Header file for the EnableIf class template.
Header file for the IsPadded type trait.
size_t argmin(const DenseVector< VT, TF > &dv)
Returns the index of the first smallest element of the dense vector.
Definition: DVecReduceExpr.h:540
Header file for the IsSIMDEnabled type trait.
constexpr bool IsPadded_v
Auxiliary variable template for the IsPadded type trait.The IsPadded_v variable template provides a c...
Definition: IsPadded.h:134
Header file for run time assertion macros.
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.The CompositeType_t alias declaration pro...
Definition: Aliases.h:90
Generic wrapper for the max() function.
Definition: Max.h:80
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:94
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
Header file for the HasMember type traits.
Header file for the RemoveReference type trait.
Generic wrapper for the min() function.
Definition: Min.h:80
Header file for the Min functor.
Generic wrapper for the multiplication operator.
Definition: Mult.h:80
Header file for the Max functor.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression,...
Definition: Assert.h:101
Header file for the function trace functionality.