VCTR
Loading...
Searching...
No Matches
Sum.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{
25template <class ValueType>
26struct SumInit
27{
28 static constexpr ValueType value {};
29};
30
31template <class StringType>
32requires std::convertible_to<const char*, StringType>
33struct SumInit<StringType>
34{
35 static constexpr const char* value = "";
36};
37}
38
39namespace vctr::expressions
40{
41
42template <size_t extent, class SrcType>
43requires has::operatorPlusEquals<ValueType<SrcType>>
45{
46public:
47 using value_type = ValueType<SrcType>;
48
49 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (Sum, src)
50
51 static constexpr auto reductionResultInitValue = detail::SumInit<value_type>::value;
52
53 VCTR_FORCEDINLINE constexpr void reduceElementWise (value_type& result, size_t i) const
54 {
55 result += src[i];
56 }
57
58 //==============================================================================
59 VCTR_FORCEDINLINE value_type reduceVectorOp() const
61 {
62 return Expression::Accelerate::sum (src.data(), size());
63 };
64
65 VCTR_FORCEDINLINE value_type reduceVectorOp() const
67 {
68 return Expression::IPP::sum (src.data(), sizeToInt (size()));
69 };
70
71 //==============================================================================
72 VCTR_FORWARD_PREPARE_SIMD_EVALUATION_UNARY_EXPRESSION_MEMBER_FUNCTIONS
73
74 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
76 {
77 result = Expression::Neon::add (result, src.getNeon (i));
78 }
79
80 VCTR_FORCEDINLINE VCTR_TARGET ("fma") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
81 requires Config::archX64 && has::getAVX<SrcType> && is::realFloatNumber<value_type>
82 {
83 result = Expression::AVX::add (result, src.getAVX (i));
84 }
85
86 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
87 requires Config::archX64 && has::getAVX<SrcType> && is::int32Number<value_type>
88 {
89 result = Expression::AVX::add (result, src.getAVX (i));
90 }
91
92 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
94 {
95 result = Expression::SSE::add (result, src.getSSE (i));
96 }
97
98 //==============================================================================
99 template <size_t n>
100 VCTR_FORCEDINLINE static constexpr value_type finalizeReduction (const std::array<value_type, n>& sums)
101 {
102 if constexpr (n == 1)
103 return sums[0];
104
105 return std::reduce (sums.begin(), sums.end());
106 }
107};
108
109} // namespace vctr::expressions
110
111namespace vctr
112{
113
119
120} // namespace vctr
Definition: Sum.h:45
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 floating point number.
Definition: NumericTypeConcepts.h:83
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 real or complex floating point ve...
Definition: ContainerAndExpressionConcepts.h:308
constexpr ExpressionChainBuilder< expressions::Sum > sum
Computes the sum of the source values.
Definition: Sum.h:118
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
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