VCTR
Loading...
Searching...
No Matches
Product.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>
27requires is::number<ValueType<SrcType>>
29{
30public:
31 using value_type = ValueType<SrcType>;
32
33 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (Product, src)
34
35 static constexpr value_type reductionResultInitValue = 1;
36
37 VCTR_FORCEDINLINE constexpr void reduceElementWise (value_type& result, size_t i) const
38 {
40 {
41 if (std::is_constant_evaluated())
42 {
43 // Clang cannot constant-evaluate the built-in complex *= used by libstdc++.
44 const auto rhs = src[i];
45 const auto a = result.real();
46 const auto b = result.imag();
47 const auto c = rhs.real();
48 const auto d = rhs.imag();
49 result = value_type (a * c - b * d, a * d + b * c);
50 return;
51 }
52 }
53
54 result *= src[i];
55 }
56
57 //==============================================================================
58 VCTR_FORWARD_PREPARE_SIMD_EVALUATION_UNARY_EXPRESSION_MEMBER_FUNCTIONS
59
60 VCTR_FORCEDINLINE void reduceNeonRegisterWise (NeonRegister<value_type>& result, size_t i) const
62 {
63 result = Expression::Neon::mul (result, src.getNeon (i));
64 }
65
66 VCTR_FORCEDINLINE VCTR_TARGET ("fma") void reduceAVXRegisterWise (AVXRegister<value_type>& result, size_t i) const
67 requires Config::archX64 && has::getAVX<SrcType> && is::realFloatNumber<value_type>
68 {
69 result = Expression::AVX::mul (result, src.getAVX (i));
70 }
71
72 VCTR_FORCEDINLINE VCTR_TARGET ("sse4.1") void reduceSSERegisterWise (SSERegister<value_type>& result, size_t i) const
73 requires Config::archX64 && has::getSSE<SrcType> && is::realFloatNumber<value_type>
74 {
75 result = Expression::SSE::mul (result, src.getSSE (i));
76 }
77
78 //==============================================================================
79 template <size_t n>
80 VCTR_FORCEDINLINE static constexpr value_type finalizeReduction (const std::array<value_type, n>& products)
81 {
82 if constexpr (n == 1)
83 return products[0];
84
85 return std::reduce (products.begin(), products.end(), reductionResultInitValue, std::multiplies<>());
86 }
87};
88
89} // namespace vctr::expressions
90
91namespace vctr
92{
93
103
104} // namespace vctr
Definition: Product.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 represent a complex valued floating point number (e.g.
Definition: NumericTypeConcepts.h:91
Constrains a type to represent a real valued 32 bit integer number.
Definition: NumericTypeConcepts.h:61
Constrains a type to represent a real valued floating point number.
Definition: NumericTypeConcepts.h:87
constexpr ExpressionChainBuilder< expressions::Product > product
Computes the product of the source values.
Definition: Product.h:102
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:170
The base class to every expression template.
Definition: ExpressionTemplate.h:37
Definition: NeonRegister.h:28
Definition: SSERegister.h:28