11#ifndef EIGEN_REALVIEW_H
12#define EIGEN_REALVIEW_H
15#include "./InternalHeaderCheck.h"
28struct complex_array_access : std::false_type {};
30struct complex_array_access<std::complex<T>> : std::true_type {};
32template <
typename Xpr>
33struct traits<RealView<Xpr>> :
public traits<Xpr> {
35 static constexpr int double_size(T size,
bool times_two) {
36 int size_as_int = int(size);
37 if (size_as_int == Dynamic)
return Dynamic;
38 return times_two ? (2 * size_as_int) : size_as_int;
41 using Base = traits<Xpr>;
42 using ComplexScalar =
typename Base::Scalar;
43 using Scalar =
typename NumTraits<ComplexScalar>::Real;
45 static constexpr bool ArrayAccess = complex_array_access<ComplexScalar>::value;
47 static constexpr int ActualDirectAccessBit =
48 ArrayAccess && inner_stride_at_compile_time<Xpr>::value == 1 ?
DirectAccessBit : 0;
49 static constexpr int ActualLvalueBit = !std::is_const<Xpr>::value && ArrayAccess ?
LvalueBit : 0;
51 static constexpr int FlagMask =
53 static constexpr int BaseFlags = int(evaluator<Xpr>::Flags) | int(Base::Flags);
54 static constexpr int Flags = BaseFlags & FlagMask;
55 static constexpr bool IsRowMajor = Flags &
RowMajorBit;
56 static constexpr int RowsAtCompileTime = double_size(Base::RowsAtCompileTime, !IsRowMajor);
57 static constexpr int ColsAtCompileTime = double_size(Base::ColsAtCompileTime, IsRowMajor);
58 static constexpr int SizeAtCompileTime = size_at_compile_time(RowsAtCompileTime, ColsAtCompileTime);
59 static constexpr int MaxRowsAtCompileTime = double_size(Base::MaxRowsAtCompileTime, !IsRowMajor);
60 static constexpr int MaxColsAtCompileTime = double_size(Base::MaxColsAtCompileTime, IsRowMajor);
61 static constexpr int MaxSizeAtCompileTime = size_at_compile_time(MaxRowsAtCompileTime, MaxColsAtCompileTime);
62 static constexpr int OuterStrideAtCompileTime = double_size(outer_stride_at_compile_time<Xpr>::value,
true);
63 static constexpr int InnerStrideAtCompileTime = inner_stride_at_compile_time<Xpr>::value;
66template <
typename Xpr>
67struct evaluator<RealView<Xpr>> :
private evaluator<Xpr> {
68 using BaseEvaluator = evaluator<Xpr>;
69 using XprType = RealView<Xpr>;
70 using ExpressionTraits = traits<XprType>;
71 using ComplexScalar =
typename ExpressionTraits::ComplexScalar;
72 using Scalar =
typename ExpressionTraits::Scalar;
74 static constexpr int Flags = ExpressionTraits::Flags;
75 static constexpr int CoeffReadCost = BaseEvaluator::CoeffReadCost;
76 static constexpr int Alignment = BaseEvaluator::Alignment;
77 static constexpr bool IsRowMajor = ExpressionTraits::IsRowMajor;
82 using ComplexCoeffReturnType = std::conditional_t<CoeffByReference, const ComplexScalar&, ComplexScalar>;
83 using CoeffReturnType = std::conditional_t<CoeffByReference, const Scalar&, Scalar>;
85 EIGEN_DEVICE_FUNC
explicit evaluator(XprType realView) : BaseEvaluator(realView.m_xpr) {}
87 template <
bool Enable = CoeffByReference, std::enable_if_t<!Enable,
bool> = true>
88 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index row, Index col)
const {
89 Index r = IsRowMajor ? row : row / 2;
90 Index c = IsRowMajor ? col / 2 : col;
91 bool p = (IsRowMajor ? col : row) & 1;
92 ComplexScalar ccoeff = BaseEvaluator::coeff(r, c);
93 return p ? numext::imag(ccoeff) : numext::real(ccoeff);
95 template <
bool Enable = CoeffByReference, std::enable_if_t<Enable,
bool> = true>
96 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col)
const {
97 Index r = IsRowMajor ? row : row / 2;
98 Index c = IsRowMajor ? col / 2 : col;
99 Index p = (IsRowMajor ? col : row) & 1;
100 ComplexCoeffReturnType ccoeff = BaseEvaluator::coeff(r, c);
101 return reinterpret_cast<const Scalar(&)[2]
>(ccoeff)[p];
103 template <
bool Enable = CoeffByReference, std::enable_if_t<!Enable,
bool> = true>
104 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index index)
const {
105 ComplexScalar ccoeff = BaseEvaluator::coeff(index / 2);
107 return p ? numext::imag(ccoeff) : numext::real(ccoeff);
109 template <
bool Enable = CoeffByReference, std::enable_if_t<Enable,
bool> = true>
110 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
111 ComplexCoeffReturnType ccoeff = BaseEvaluator::coeff(index / 2);
113 return reinterpret_cast<const Scalar(&)[2]
>(ccoeff)[p];
115 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col) {
116 Index r = IsRowMajor ? row : row / 2;
117 Index c = IsRowMajor ? col / 2 : col;
118 Index p = (IsRowMajor ? col : row) & 1;
119 ComplexScalar& ccoeffRef = BaseEvaluator::coeffRef(r, c);
120 return reinterpret_cast<Scalar(&)[2]
>(ccoeffRef)[p];
122 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
123 ComplexScalar& ccoeffRef = BaseEvaluator::coeffRef(index / 2);
125 return reinterpret_cast<Scalar(&)[2]
>(ccoeffRef)[p];
133 template <
int LoadMode,
typename PacketType>
134 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packet(Index row, Index col)
const {
135 constexpr int RealPacketSize = unpacket_traits<PacketType>::size;
136 using ComplexPacket =
typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
137 EIGEN_STATIC_ASSERT((find_packet_by_size<ComplexScalar, RealPacketSize / 2>::value),
138 MISSING COMPATIBLE COMPLEX PACKET TYPE)
139 Index r = IsRowMajor ? row : row / 2;
140 Index c = IsRowMajor ? col / 2 : col;
141 bool p = (IsRowMajor ? col : row) & 1;
142 ComplexPacket cresult = p ? BaseEvaluator::template packet<Unaligned, ComplexPacket>(r, c)
143 : BaseEvaluator::template packet<LoadMode, ComplexPacket>(r, c);
144 PacketType result = preinterpret<PacketType>(cresult);
146 Scalar aux[RealPacketSize + 1];
147 pstoreu(aux, result);
148 Index lastr = IsRowMajor ? row : row + RealPacketSize - 1;
149 Index lastc = IsRowMajor ? col + RealPacketSize - 1 : col;
150 aux[RealPacketSize] = coeff(lastr, lastc);
151 result = ploadu<PacketType>(aux + 1);
156 template <
int LoadMode,
typename PacketType>
157 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packet(Index index)
const {
158 constexpr int RealPacketSize = unpacket_traits<PacketType>::size;
159 using ComplexPacket =
typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
160 EIGEN_STATIC_ASSERT((find_packet_by_size<ComplexScalar, RealPacketSize / 2>::value),
161 MISSING COMPATIBLE COMPLEX PACKET TYPE)
163 ComplexPacket cresult = p ? BaseEvaluator::template packet<Unaligned, ComplexPacket>(index / 2)
164 : BaseEvaluator::template packet<LoadMode, ComplexPacket>(index / 2);
165 PacketType result = preinterpret<PacketType>(cresult);
167 Scalar aux[RealPacketSize + 1];
168 pstoreu(aux, result);
169 aux[RealPacketSize] = coeff(index + RealPacketSize - 1);
170 result = ploadu<PacketType>(aux + 1);
181 template <
int LoadMode,
typename PacketType>
182 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegment(Index row, Index col, Index begin, Index count)
const {
183 constexpr int RealPacketSize = unpacket_traits<PacketType>::size;
184 using ComplexPacket =
typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
185 EIGEN_STATIC_ASSERT((find_packet_by_size<ComplexScalar, RealPacketSize / 2>::value),
186 MISSING COMPATIBLE COMPLEX PACKET TYPE)
187 Index actualBegin = numext::round_down(begin, 2);
188 Index actualEnd = numext::round_down(begin + count + 1, 2);
189 Index actualCount = actualEnd - actualBegin;
190 Index r = IsRowMajor ? row : row / 2;
191 Index c = IsRowMajor ? col / 2 : col;
192 bool p = (IsRowMajor ? col : row) & 1;
193 ComplexPacket cresult =
194 p ? BaseEvaluator::template packetSegment<Unaligned, ComplexPacket>(r, c, actualBegin / 2, actualCount / 2)
195 : BaseEvaluator::template packetSegment<LoadMode, ComplexPacket>(r, c, actualBegin / 2, actualCount / 2);
196 PacketType result = preinterpret<PacketType>(cresult);
198 Scalar aux[RealPacketSize + 1] = {};
199 pstoreu(aux, result);
201 if (actualEnd == begin + count) {
202 Index lastr = IsRowMajor ? row : row + actualEnd - 1;
203 Index lastc = IsRowMajor ? col + actualEnd - 1 : col;
204 aux[actualEnd] = coeff(lastr, lastc);
206 result = ploadu<PacketType>(aux + 1);
211 template <
int LoadMode,
typename PacketType>
212 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegment(Index index, Index begin, Index count)
const {
213 constexpr int RealPacketSize = unpacket_traits<PacketType>::size;
214 using ComplexPacket =
typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
215 EIGEN_STATIC_ASSERT((find_packet_by_size<ComplexScalar, RealPacketSize / 2>::value),
216 MISSING COMPATIBLE COMPLEX PACKET TYPE)
217 Index actualBegin = numext::round_down(begin, 2);
218 Index actualEnd = numext::round_down(begin + count + 1, 2);
219 Index actualCount = actualEnd - actualBegin;
221 ComplexPacket cresult =
222 p ? BaseEvaluator::template packetSegment<Unaligned, ComplexPacket>(index / 2, actualBegin / 2, actualCount / 2)
223 : BaseEvaluator::template packetSegment<LoadMode, ComplexPacket>(index / 2, actualBegin / 2, actualCount / 2);
224 PacketType result = preinterpret<PacketType>(cresult);
226 Scalar aux[RealPacketSize + 1] = {};
227 pstoreu(aux, result);
228 if (actualEnd == begin + count) aux[actualEnd] = coeff(index + actualEnd - 1);
229 result = ploadu<PacketType>(aux + 1);
237 template <
int StoreMode,
typename PacketType>
238 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacket(Index row, Index col,
const PacketType& x) {
239 constexpr int RealPacketSize = unpacket_traits<PacketType>::size;
240 using ComplexPacket =
typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
241 if (((IsRowMajor ? col : row) & 1) == 0) {
242 BaseEvaluator::template writePacket<StoreMode, ComplexPacket>(
243 IsRowMajor ? row : row / 2, IsRowMajor ? col / 2 : col, preinterpret<ComplexPacket>(x));
245 writePacketSegment<StoreMode, PacketType>(row, col, x, 0, RealPacketSize);
249 template <
int StoreMode,
typename PacketType>
250 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacket(Index index,
const PacketType& x) {
251 constexpr int RealPacketSize = unpacket_traits<PacketType>::size;
252 using ComplexPacket =
typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
253 if ((index & 1) == 0) {
254 BaseEvaluator::template writePacket<StoreMode, ComplexPacket>(index / 2, preinterpret<ComplexPacket>(x));
256 writePacketSegment<StoreMode, PacketType>(index, x, 0, RealPacketSize);
260 template <
int StoreMode,
typename PacketType>
261 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacketSegment(Index row, Index col,
const PacketType& x, Index begin,
263 constexpr int RealPacketSize = unpacket_traits<PacketType>::size;
264 using ComplexPacket =
typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
265 EIGEN_STATIC_ASSERT((find_packet_by_size<ComplexScalar, RealPacketSize / 2>::value),
266 MISSING COMPATIBLE COMPLEX PACKET TYPE)
267 const Index inner = IsRowMajor ? col : row;
268 if (((inner | begin | count) & 1) == 0) {
269 const Index r = IsRowMajor ? row : row / 2;
270 const Index c = IsRowMajor ? col / 2 : col;
271 BaseEvaluator::template writePacketSegment<StoreMode, ComplexPacket>(r, c, preinterpret<ComplexPacket>(x),
272 begin / 2, count / 2);
274 Scalar aux[RealPacketSize];
276 for (Index i = begin; i < begin + count; ++i)
277 coeffRef(IsRowMajor ? row : row + i, IsRowMajor ? col + i : col) = aux[i];
281 template <
int StoreMode,
typename PacketType>
282 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacketSegment(Index index,
const PacketType& x, Index begin,
284 constexpr int RealPacketSize = unpacket_traits<PacketType>::size;
285 using ComplexPacket =
typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
286 EIGEN_STATIC_ASSERT((find_packet_by_size<ComplexScalar, RealPacketSize / 2>::value),
287 MISSING COMPATIBLE COMPLEX PACKET TYPE)
288 if (((index | begin | count) & 1) == 0) {
289 BaseEvaluator::template writePacketSegment<StoreMode, ComplexPacket>(index / 2, preinterpret<ComplexPacket>(x),
290 begin / 2, count / 2);
292 Scalar aux[RealPacketSize];
294 for (Index i = begin; i < begin + count; ++i) coeffRef(index + i) = aux[i];
301template <
typename Xpr>
302class RealView :
public internal::dense_xpr_base<RealView<Xpr>>::type {
303 using ExpressionTraits = internal::traits<RealView>;
304 EIGEN_STATIC_ASSERT(NumTraits<typename Xpr::Scalar>::IsComplex, SCALAR MUST BE COMPLEX)
306 using Scalar =
typename ExpressionTraits::Scalar;
307 using Nested = RealView;
309 EIGEN_DEVICE_FUNC
explicit RealView(Xpr& xpr) : m_xpr(xpr) {}
310 EIGEN_DEVICE_FUNC
constexpr Index rows() const noexcept {
return Xpr::IsRowMajor ? m_xpr.rows() : 2 * m_xpr.rows(); }
311 EIGEN_DEVICE_FUNC
constexpr Index cols() const noexcept {
return Xpr::IsRowMajor ? 2 * m_xpr.cols() : m_xpr.cols(); }
312 EIGEN_DEVICE_FUNC
constexpr Index size() const noexcept {
return 2 * m_xpr.size(); }
313 EIGEN_DEVICE_FUNC
constexpr Index innerStride() const noexcept {
return m_xpr.innerStride(); }
314 EIGEN_DEVICE_FUNC
constexpr Index outerStride() const noexcept {
return 2 * m_xpr.outerStride(); }
315 EIGEN_DEVICE_FUNC
void resize(Index rows, Index cols) {
316 m_xpr.resize(Xpr::IsRowMajor ? rows : rows / 2, Xpr::IsRowMajor ? cols / 2 : cols);
318 EIGEN_DEVICE_FUNC
void resize(Index size) { m_xpr.resize(size / 2); }
319 using ScalarWithConstIfNotLvalue = std::conditional_t<internal::is_lvalue<Xpr>::value, Scalar,
const Scalar>;
320 EIGEN_DEVICE_FUNC ScalarWithConstIfNotLvalue* data() {
321 return reinterpret_cast<ScalarWithConstIfNotLvalue*
>(m_xpr.data());
323 EIGEN_DEVICE_FUNC
const Scalar* data()
const {
return reinterpret_cast<const Scalar*
>(m_xpr.data()); }
325 EIGEN_DEVICE_FUNC RealView(
const RealView&) =
default;
327 EIGEN_DEVICE_FUNC RealView& operator=(
const RealView& other);
329 template <
typename OtherDerived>
330 EIGEN_DEVICE_FUNC RealView& operator=(
const RealView<OtherDerived>& other);
332 template <
typename OtherDerived>
333 EIGEN_DEVICE_FUNC RealView& operator=(
const DenseBase<OtherDerived>& other);
336 friend struct internal::evaluator<RealView>;
340template <
typename Xpr>
341EIGEN_DEVICE_FUNC RealView<Xpr>& RealView<Xpr>::operator=(
const RealView& other) {
342 internal::call_assignment(*
this, other);
346template <
typename Xpr>
347template <
typename OtherDerived>
348EIGEN_DEVICE_FUNC RealView<Xpr>& RealView<Xpr>::operator=(
const RealView<OtherDerived>& other) {
349 internal::call_assignment(*
this, other);
353template <
typename Xpr>
354template <
typename OtherDerived>
356 internal::call_assignment(*
this, other.derived());
360template <
typename Derived>
362 return RealViewReturnType(derived());
365template <
typename Derived>
367 return ConstRealViewReturnType(derived());
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:45
RealViewReturnType realView()
Definition RealView.h:361
constexpr unsigned int ActualPacketAccessBit
Definition Constants.h:109
constexpr unsigned int PacketAccessBit
Definition Constants.h:98
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int LinearAccessBit
Definition Constants.h:134
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71