Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
ConjHelper.h
1// SPDX-License-Identifier: MPL-2.0
2
3// This file is part of Eigen, a lightweight C++ template library
4// for linear algebra.
5//
6// Copyright (C) 2017 Gael Guennebaud <gael.guennebaud@inria.fr>
7//
8// This Source Code Form is subject to the terms of the Mozilla
9// Public License v. 2.0. If a copy of the MPL was not distributed
10// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11
12#ifndef EIGEN_ARCH_CONJ_HELPER_H
13#define EIGEN_ARCH_CONJ_HELPER_H
14
15#define EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(PACKET_CPLX, PACKET_REAL) \
16 template <bool ConjLhs, bool ConjRhs> \
17 struct conj_helper<PACKET_REAL, PACKET_CPLX, ConjLhs, ConjRhs> { \
18 EIGEN_STRONG_INLINE PACKET_CPLX pmadd(const PACKET_REAL& x, const PACKET_CPLX& y, const PACKET_CPLX& c) const { \
19 return PACKET_CPLX(Eigen::internal::pmadd<PACKET_REAL>(x, conj_if<ConjRhs>().pconj(y).v, c.v)); \
20 } \
21 EIGEN_STRONG_INLINE PACKET_CPLX pmsub(const PACKET_REAL& x, const PACKET_CPLX& y, const PACKET_CPLX& c) const { \
22 return PACKET_CPLX(Eigen::internal::pmsub<PACKET_REAL>(x, conj_if<ConjRhs>().pconj(y).v, c.v)); \
23 } \
24 EIGEN_STRONG_INLINE PACKET_CPLX pmul(const PACKET_REAL& x, const PACKET_CPLX& y) const { \
25 return PACKET_CPLX(Eigen::internal::pmul<PACKET_REAL>(x, conj_if<ConjRhs>().pconj(y).v)); \
26 } \
27 }; \
28 \
29 template <> \
30 struct conj_helper<PACKET_REAL, PACKET_CPLX, true, true> : conj_helper<PACKET_REAL, PACKET_CPLX, false, true> {}; \
31 \
32 template <bool ConjLhs, bool ConjRhs> \
33 struct conj_helper<PACKET_CPLX, PACKET_REAL, ConjLhs, ConjRhs> { \
34 EIGEN_STRONG_INLINE PACKET_CPLX pmadd(const PACKET_CPLX& x, const PACKET_REAL& y, const PACKET_CPLX& c) const { \
35 return PACKET_CPLX(Eigen::internal::pmadd<PACKET_REAL>(conj_if<ConjLhs>().pconj(x).v, y, c.v)); \
36 } \
37 EIGEN_STRONG_INLINE PACKET_CPLX pmsub(const PACKET_CPLX& x, const PACKET_REAL& y, const PACKET_CPLX& c) const { \
38 return PACKET_CPLX(Eigen::internal::pmsub<PACKET_REAL>(conj_if<ConjLhs>().pconj(x).v, y, c.v)); \
39 } \
40 EIGEN_STRONG_INLINE PACKET_CPLX pmul(const PACKET_CPLX& x, const PACKET_REAL& y) const { \
41 return PACKET_CPLX(Eigen::internal::pmul<PACKET_REAL>(conj_if<ConjLhs>().pconj(x).v, y)); \
42 } \
43 }; \
44 template <> \
45 struct conj_helper<PACKET_CPLX, PACKET_REAL, true, true> : conj_helper<PACKET_CPLX, PACKET_REAL, true, false> {};
46
47// IWYU pragma: private
48#include "../../InternalHeaderCheck.h"
49
50namespace Eigen {
51namespace internal {
52
53template <bool Conjugate>
54struct conj_if;
55
56template <>
57struct conj_if<true> {
58 template <typename T>
59 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T operator()(const T& x) const {
60 return numext::conj(x);
61 }
62 template <typename T>
63 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T pconj(const T& x) const {
64 return internal::pconj(x);
65 }
66};
67
68template <>
69struct conj_if<false> {
70 template <typename T>
71 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T& operator()(const T& x) const {
72 return x;
73 }
74 template <typename T>
75 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T& pconj(const T& x) const {
76 return x;
77 }
78};
79
80// Generic Implementation, assume scalars since the packet-version is
81// specialized below.
82template <typename LhsType, typename RhsType, bool ConjLhs, bool ConjRhs>
83struct conj_helper {
84 using ResultType = typename ScalarBinaryOpTraits<LhsType, RhsType>::ReturnType;
85
86 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ResultType pmadd(const LhsType& x, const RhsType& y,
87 const ResultType& c) const {
88 return this->pmul(x, y) + c;
89 }
90
91 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ResultType pmsub(const LhsType& x, const RhsType& y,
92 const ResultType& c) const {
93 return this->pmul(x, y) - c;
94 }
95
96 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ResultType pmul(const LhsType& x, const RhsType& y) const {
97 return internal::mul(conj_if<ConjLhs>()(x), conj_if<ConjRhs>()(y));
98 }
99};
100
101template <typename LhsScalar, typename RhsScalar>
102struct conj_helper<LhsScalar, RhsScalar, true, true> {
103 using ResultType = typename ScalarBinaryOpTraits<LhsScalar, RhsScalar>::ReturnType;
104
105 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ResultType pmadd(const LhsScalar& x, const RhsScalar& y,
106 const ResultType& c) const {
107 return this->pmul(x, y) + c;
108 }
109
110 // Conjugation can reverse multiplication for noncommutative scalars.
111 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ResultType pmul(const LhsScalar& x, const RhsScalar& y) const {
112 EIGEN_IF_CONSTEXPR ((functor_is_commutative<scalar_product_op<LhsScalar, RhsScalar>>::value)) {
113 return numext::conj(internal::mul(x, y));
114 } else {
115 return internal::mul(numext::conj(x), numext::conj(y));
116 }
117 }
118};
119
120// Implementation with equal type, use packet operations.
121template <typename Packet, bool ConjLhs, bool ConjRhs>
122struct conj_helper<Packet, Packet, ConjLhs, ConjRhs> {
123 using ResultType = Packet;
124 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet pmadd(const Packet& x, const Packet& y, const Packet& c) const {
125 return Eigen::internal::pmadd(conj_if<ConjLhs>().pconj(x), conj_if<ConjRhs>().pconj(y), c);
126 }
127
128 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet pmsub(const Packet& x, const Packet& y, const Packet& c) const {
129 return Eigen::internal::pmsub(conj_if<ConjLhs>().pconj(x), conj_if<ConjRhs>().pconj(y), c);
130 }
131
132 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet pmul(const Packet& x, const Packet& y) const {
133 return Eigen::internal::pmul(conj_if<ConjLhs>().pconj(x), conj_if<ConjRhs>().pconj(y));
134 }
135};
136
137template <typename Packet>
138struct conj_helper<Packet, Packet, true, true> {
139 using ResultType = Packet;
140
141 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet pmadd(const Packet& x, const Packet& y, const Packet& c) const {
142 return Eigen::internal::pmadd(pconj(x), pconj(y), c);
143 }
144 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet pmsub(const Packet& x, const Packet& y, const Packet& c) const {
145 return Eigen::internal::pmsub(pconj(x), pconj(y), c);
146 }
147 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet pmul(const Packet& x, const Packet& y) const {
148 EIGEN_IF_CONSTEXPR ((functor_is_commutative<scalar_product_op<typename unpacket_traits<Packet>::type>>::value)) {
149 return pconj(Eigen::internal::pmul(x, y));
150 } else {
151 return Eigen::internal::pmul(pconj(x), pconj(y));
152 }
153 }
154};
155
156} // namespace internal
157} // namespace Eigen
158
159#endif // EIGEN_ARCH_CONJ_HELPER_H