VCTR
Loading...
Searching...
No Matches
NormalizeSum.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2023 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>
27requires is::realFloatNumber<ValueType<SrcType>>
29{
30public:
31 using value_type = ValueType<SrcType>;
32
33 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (NormalizeSum, src)
34
35 constexpr void applyRuntimeArgs()
36 {
37 srcSum = sum << src;
38 }
39
40 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
41 {
42 VCTR_ASSERT (srcSum != value_type (0));
43 VCTR_START_IGNORE_WARNING_MSVC (4723) // potential divide by 0
44 return src[i] / srcSum;
45 VCTR_END_IGNORE_WARNING_MSVC
46 }
47
48 //==============================================================================
49 // Platform Vector Operation Implementation
50 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
52 {
53 VCTR_ASSERT (srcSum != value_type (0));
54 Expression::Accelerate::div (src.evalNextVectorOpInExpressionChain (dst), srcSum, dst, size());
55 return dst;
56 }
57
58 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
60 {
61 VCTR_ASSERT (srcSum != value_type (0));
62 Expression::IPP::div (src.evalNextVectorOpInExpressionChain (dst), srcSum, dst, sizeToInt (size()));
63 return dst;
64 }
65
66 //==============================================================================
67 // SIMD Implementation
68 VCTR_FORCEDINLINE VCTR_TARGET ("avx") void prepareAVXEvaluation() const
70 {
71 src.prepareAVXEvaluation();
72 srcSumSIMD.avx = Expression::AVX::broadcast (srcSum);
73 }
74
75 VCTR_FORCEDINLINE VCTR_TARGET ("fma") AVXRegister<value_type> getAVX (size_t i) const
76 requires archX64 && has::getAVX<SrcType>
77 {
78 using Reg = typename Expression::AVX;
79
80 return Reg::div (src.getAVX (i), srcSumSIMD.avx);
81 }
82
83 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void prepareSSEEvaluation() const
85 {
86 src.prepareSSEEvaluation();
87 srcSumSIMD.sse = Expression::SSE::broadcast (srcSum);
88 }
89
90 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") SSERegister<value_type> getSSE (size_t i) const
91 requires archX64 && has::getSSE<SrcType>
92 {
93 using Reg = typename Expression::SSE;
94
95 return Reg::div (src.getSSE (i), srcSumSIMD.sse);
96 }
97
98 VCTR_FORCEDINLINE void prepareNeonEvaluation() const
100 {
101 src.prepareNeonEvaluation();
102 srcSumSIMD.neon = Expression::Neon::broadcast (srcSum);
103 }
104
105 VCTR_FORCEDINLINE NeonRegister<value_type> getNeon (size_t i) const
106 requires archARM && has::getNeon<SrcType>
107 {
108 using Reg = typename Expression::Neon;
109
110 return Reg::div (src.getNeon (i), srcSumSIMD.neon);
111 }
112
113private:
114 //==============================================================================
115 value_type srcSum = value_type (0);
116 mutable SIMDRegisterUnion<Expression> srcSumSIMD {};
117};
118
119}
120
121namespace vctr
122{
123
138}
Definition: NormalizeSum.h:29
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 have a member function prepareAVXEvaluation() const.
Definition: ContainerAndExpressionConcepts.h:94
Constrains a type to have a member function prepareNeonEvaluation() const.
Definition: ContainerAndExpressionConcepts.h:86
Constrains a type to have a member function prepareSSEEvaluation() const.
Definition: ContainerAndExpressionConcepts.h:102
A combined concept to check if Apple Accelerate is a suitable option for a real valued floating point...
Definition: ContainerAndExpressionConcepts.h:309
A combined concept to check if Intel IPP is a suitable option for a real valued floating point vector...
Definition: ContainerAndExpressionConcepts.h:337
constexpr ExpressionChainBuilder< expressions::NormalizeSum > normalizeSum
Computes the sum of the source elements and divides all source elements by that sum.
Definition: NormalizeSum.h:137
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:170
The base class to every expression template.
Definition: ExpressionTemplate.h:37
Definition: NeonRegister.h:28
Definition: SSERegister.h:28
Helper template to define a union of all supported SIMD types.
Definition: ExpressionTemplate.h:123