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(), size());
69 };
70
71 //==============================================================================
72 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
74 {
75 result = Expression::Neon::add (result, src.getNeon (i));
76 }
77
78 VCTR_FORCEDINLINE VCTR_TARGET ("avx") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
79 requires Config::archX64 && has::getAVX<SrcType> && is::realFloatNumber<value_type>
80 {
81 result = Expression::AVX::add (result, src.getAVX (i));
82 }
83
84 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
85 requires Config::archX64 && has::getAVX<SrcType> && is::int32Number<value_type>
86 {
87 result = Expression::AVX::add (result, src.getAVX (i));
88 }
89
90 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
92 {
93 result = Expression::SSE::add (result, src.getSSE (i));
94 }
95
96 //==============================================================================
97 template <size_t n>
98 VCTR_FORCEDINLINE static constexpr value_type finalizeReduction (const std::array<value_type, n>& sums)
99 {
100 if constexpr (n == 1)
101 return sums[0];
102
103 return std::reduce (sums.begin(), sums.end());
104 }
105};
106
107} // namespace vctr::expressions
108
109namespace vctr
110{
111
117
118} // namespace vctr
Definition: Sum.h:45
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 floating point number.
Definition: NumericTypeConcepts.h:79
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 real or complex floating point ve...
Definition: ContainerAndExpressionConcepts.h:290
constexpr ExpressionChainBuilder< expressions::Sum > sum
Computes the sum of the source values.
Definition: Sum.h:116
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
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