VCTR
Loading...
Searching...
No Matches
Add.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
26//==============================================================================
28template <size_t extent, class SrcAType, class SrcBType>
30{
31public:
32 using value_type = std::common_type_t<ValueType<SrcAType>, ValueType<SrcBType>>;
33
34 VCTR_COMMON_BINARY_VEC_VEC_EXPRESSION_MEMBERS (AddVectors, srcA, srcB)
35
36 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
37 {
38 return srcA[i] + srcB[i];
39 }
40
41 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
43 {
44 Expression::Accelerate::add (srcA.evalNextVectorOpInExpressionChain (dst), srcB.evalNextVectorOpInExpressionChain (dst), dst, size());
45 return dst;
46 }
47
48 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
50 {
51 Expression::IPP::add (srcA.evalNextVectorOpInExpressionChain (dst), srcB.evalNextVectorOpInExpressionChain (dst), dst, size());
52 return dst;
53 }
54
55 //==============================================================================
56 // AVX Implementation
57 VCTR_FORCEDINLINE VCTR_TARGET ("avx") AVXRegister<value_type> getAVX (size_t i) const
58 requires (archX64 && has::getAVX<SrcAType> && has::getAVX<SrcBType> && Expression::CommonElement::isRealFloat)
59 {
60 return Expression::AVX::add (srcA.getAVX (i), srcB.getAVX (i));
61 }
62
63 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") AVXRegister<value_type> getAVX (size_t i) const
64 requires (archX64 && has::getAVX<SrcAType> && has::getAVX<SrcBType> && Expression::CommonElement::isUnsigned)
65 {
66 return Expression::AVX::add (srcA.getAVX (i), srcB.getAVX (i));
67 }
68
69 //==============================================================================
70 // SSE Implementation
71 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") SSERegister<value_type> getSSE (size_t i) const
72 requires (archX64 && has::getSSE<SrcAType> && has::getSSE<SrcBType>)
73 {
74 return Expression::SSE::add (srcA.getSSE (i), srcB.getSSE (i));
75 }
76};
77
78//==============================================================================
80template <size_t extent, class SrcType>
82{
83public:
84 using value_type = ValueType<SrcType>;
85
86 VCTR_COMMON_BINARY_SINGLE_VEC_EXPRESSION_MEMBERS (AddSingleToVec, src, single)
87
88 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
89 {
90 return single + src[i];
91 }
92
93 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
95 {
96 Expression::Accelerate::add (src.evalNextVectorOpInExpressionChain (dst), single, dst, size());
97 return dst;
98 }
99
100 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
102 {
103 Expression::IPP::add (src.evalNextVectorOpInExpressionChain (dst), single, dst, sizeToInt (size()));
104 return dst;
105 }
106
107 //==============================================================================
108 // AVX Implementation
109 VCTR_FORCEDINLINE VCTR_TARGET ("avx") AVXRegister<value_type> getAVX (size_t i) const
110 requires (archX64 && has::getAVX<SrcType> && Expression::allElementTypesSame && Expression::CommonElement::isRealFloat)
111 {
112 return Expression::AVX::add (Expression::AVX::fromSSE (singleAsSSE, singleAsSSE), src.getAVX (i));
113 }
114
115 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") AVXRegister<value_type> getAVX (size_t i) const
116 requires (archX64 && has::getAVX<SrcType> && Expression::allElementTypesSame && Expression::CommonElement::isInt)
117 {
118 return Expression::AVX::add (Expression::AVX::fromSSE (singleAsSSE, singleAsSSE), src.getAVX (i));
119 }
120 //==============================================================================
121 // SSE Implementation
122 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") SSERegister<value_type> getSSE (size_t i) const
123 requires (archX64 && has::getSSE<SrcType> && Expression::allElementTypesSame)
124 {
125 return Expression::SSE::add (singleAsSSE, src.getSSE (i));
126 }
127};
128
129} // namespace vctr::expressions
130
131namespace vctr
132{
133
138template <is::anyVctrOrExpression SrcAType, is::anyVctrOrExpression SrcBType>
139constexpr auto operator+ (SrcAType&& a, SrcBType&& b)
140{
141 assertCommonSize (a, b);
142 constexpr auto extent = getCommonExtent<SrcAType, SrcBType>();
143
144 return expressions::AddVectors<extent, SrcAType, SrcBType> (std::forward<SrcAType> (a), std::forward<SrcBType> (b));
145}
146
151template <is::anyVctrOrExpression Src>
152constexpr auto operator+ (typename std::remove_cvref_t<Src>::value_type single, Src&& vec)
153{
154 return expressions::AddSingleToVec<extentOf<Src>, Src> (single, std::forward<Src> (vec));
155}
156
161template <is::anyVctrOrExpression Src>
162constexpr auto operator+ (Src&& vec, typename std::remove_cvref_t<Src>::value_type single)
163{
164 return expressions::AddSingleToVec<extentOf<Src>, Src> (single, std::forward<Src> (vec));
165}
166
167} // namespace vctr
Adds a single value to a vector like type.
Definition: Add.h:82
Adds two vector like types.
Definition: Add.h:30
Constrains a type to have a member function getAVX (size_t) const.
Definition: ContainerAndExpressionConcepts.h:78
Constrains a type to have a member function getSSE (size_t) const.
Definition: ContainerAndExpressionConcepts.h:82
A combined concept to check if Apple Accelerate is a suitable option for a real or complex valued flo...
Definition: ContainerAndExpressionConcepts.h:311
A combined concept to check if Apple Accelerate is a suitable option for a real or complex valued flo...
Definition: ContainerAndExpressionConcepts.h:250
A combined concept to check if Intel IPP is a suitable option for a real or complex valued floating p...
Definition: ContainerAndExpressionConcepts.h:324
A combined concept to check if Intel IPP is a suitable option for a real or complex valued floating p...
Definition: ContainerAndExpressionConcepts.h:278
constexpr auto operator+(SrcAType &&a, SrcBType &&b)
Returns an expression that adds two vector or expression sources.
Definition: Add.h:139
The main namespace of the VCTR project.
Definition: Array.h:24
constexpr void assertCommonSize(const A &a, const B &b)
Ensures that both sources have the same size.
Definition: Traits.h:253
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
The base class to every expression template.
Definition: ExpressionTemplate.h:37
Definition: SSERegister.h:28