VCTR
Loading...
Searching...
No Matches
Abs.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>
28{
29public:
30 using value_type = RealType<ValueType<SrcType>>;
31
32 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (Abs, src)
33
34 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
36 {
37 auto value = src[i];
38
39#if VCTR_USE_GCEM
40 if (std::is_constant_evaluated())
41 {
43 return gcem::abs (value);
44 else
45 return gcem::sqrt (value.real() * value.real() + value.imag() * value.imag());
46 }
47#endif
48
49 return std::abs (value);
50 }
51
52 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
54 {
55 return src[i];
56 }
57
58 // clang-format off
59 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
60 requires is::suitableForAccelerateRealFloatVectorOp <SrcType, value_type, detail::dontPreferIfIppAndAccelerateAreAvailable> ||
62 // clang-format on
63 {
64 Expression::Accelerate::abs (src.evalNextVectorOpInExpressionChain (dst), dst, size());
65 return dst;
66 }
67
68 // clang-format off
69 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
70 requires is::suitableForIppRealSingedInt32VectorOp <SrcType, value_type, detail::preferIfIppAndAccelerateAreAvailable> ||
71 is::suitableForIppRealFloatVectorOp <SrcType, value_type, detail::preferIfIppAndAccelerateAreAvailable> ||
73 // clang-format on
74 {
75 Expression::IPP::abs (src.evalNextVectorOpInExpressionChain (dst), dst, sizeToInt (size()));
76 return dst;
77 }
78
79 //==============================================================================
80 VCTR_FORWARD_PREPARE_SIMD_EVALUATION_UNARY_EXPRESSION_MEMBER_FUNCTIONS
81
82 NeonRegister<value_type> getNeon (size_t i) const
83 requires (archARM && has::getNeon<SrcType> && Expression::CommonElement::isSigned)
84 {
85 return Expression::Neon::abs (src.getNeon (i));
86 }
87
88 NeonRegister<value_type> getNeon (size_t i) const
89 requires (archARM && has::getNeon<SrcType> && ! Expression::CommonElement::isSigned)
90 {
91 return src.getNeon (i); // unsigned integers are always positive
92 }
93
94 // AVX Implementation
95 VCTR_FORCEDINLINE VCTR_TARGET ("fma") AVXRegister<value_type> getAVX (size_t i) const
96 requires (archX64 && has::getAVX<SrcType> && Expression::CommonElement::isRealFloat)
97 {
98 static const auto avxSignBit = Expression::AVX::broadcast (typename Expression::CommonElement::Type (-0.0));
99
100 return Expression::AVX::bitwiseAndNot (src.getAVX (i), avxSignBit);
101 }
102
103 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") AVXRegister<value_type> getAVX (size_t i) const
104 requires (archX64 && has::getAVX<SrcType> && Expression::CommonElement::isInt32)
105 {
106 return Expression::AVX::abs (src.getAVX (i));
107 }
108
109 VCTR_FORCEDINLINE VCTR_TARGET ("avx2") AVXRegister<value_type> getAVX (size_t i) const
110 requires (archX64 && has::getAVX<SrcType> && Expression::CommonElement::isUnsigned)
111 {
112 return src.getAVX (i); // unsigned integers are always positive
113 }
114
115 // SSE Implementation
116 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") SSERegister<value_type> getSSE (size_t i) const
117 requires (archX64 && has::getSSE<SrcType> && Expression::CommonElement::isRealFloat)
118 {
119 static const auto sseSignBit = Expression::SSE::broadcast (typename Expression::CommonElement::Type (-0.0));
120
121 return Expression::SSE::bitwiseAndNot (src.getSSE (i), sseSignBit);
122 }
123
124 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") SSERegister<value_type> getSSE (size_t i) const
125 requires (archX64 && has::getSSE<SrcType> && Expression::CommonElement::isInt32)
126 {
127 return Expression::SSE::abs (src.getSSE (i));
128 }
129
130 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") SSERegister<value_type> getSSE (size_t i) const
131 requires (archX64 && has::getSSE<SrcType> && Expression::CommonElement::isUnsigned)
132 {
133 return src.getSSE (i); // unsigned integers are always positive
134 }
135};
136
137} // namespace vctr::expressions
138
139namespace vctr
140{
148
149} // namespace vctr
Definition: Abs.h:28
Constrains a type to have a member function getAVX (size_t) const.
Definition: ContainerAndExpressionConcepts.h:98
Constrains a type to have a member function getNeon (size_t) const.
Definition: ContainerAndExpressionConcepts.h:90
Constrains a type to have a member function getSSE (size_t) const.
Definition: ContainerAndExpressionConcepts.h:106
Constrains a type to represent a real valued number.
Definition: NumericTypeConcepts.h:83
Constrains a type to represent a real valued signed number (e.g.
Definition: NumericTypeConcepts.h:69
A combined concept to check if Apple Accelerate is a suitable option for a floating point vector oper...
Definition: ContainerAndExpressionConcepts.h:321
A combined concept to check if Intel IPP is a suitable option for a floating point vector operation t...
Definition: ContainerAndExpressionConcepts.h:353
Constrains a type to represent a real valued unsigned integer number.
Definition: NumericTypeConcepts.h:78
constexpr ExpressionChainBuilder< expressions::Abs > abs
Computes the absolute value of the source values.
Definition: Abs.h:147
The main namespace of the VCTR project.
Definition: Array.h:24
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
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:170
The base class to every expression template.
Definition: ExpressionTemplate.h:37
Definition: NeonRegister.h:28
Definition: SSERegister.h:28