Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
RealView.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2025 Charlie Schlosser <cs.schlosser@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_REALVIEW_H
12#define EIGEN_REALVIEW_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21// Write access and vectorization requires array-oriented access to the real and imaginary components.
22// From https://en.cppreference.com/w/cpp/numeric/complex.html:
23// For any pointer to an element of an array of std::complex<T> named p and any valid array index i,
24// reinterpret_cast<T*>(p)[2 * i] is the real part of the complex number p[i], and
25// reinterpret_cast<T*>(p)[2 * i + 1] is the imaginary part of the complex number p[i].
26
27template <typename T>
28struct complex_array_access : std::false_type {};
29template <typename T>
30struct complex_array_access<std::complex<T>> : std::true_type {};
31
32template <typename Xpr>
33struct traits<RealView<Xpr>> : public traits<Xpr> {
34 template <typename T>
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;
39 }
40
41 using Base = traits<Xpr>;
42 using ComplexScalar = typename Base::Scalar;
43 using Scalar = typename NumTraits<ComplexScalar>::Real;
44
45 static constexpr bool ArrayAccess = complex_array_access<ComplexScalar>::value;
46 // Real and imaginary parts interleave with a uniform stride only when the complex coefficients are adjacent.
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;
50 static constexpr int ActualPacketAccessBit = packet_traits<Scalar>::Vectorizable ? PacketAccessBit : 0;
51 static constexpr int FlagMask =
52 ActualDirectAccessBit | ActualLvalueBit | ActualPacketAccessBit | HereditaryBits | LinearAccessBit;
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;
64};
65
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;
73
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;
78 // DenseCoeffsBase::CoeffReturnType is a reference for LvalueBit as well as DirectAccessBit; returning a value
79 // there would dangle.
80 static constexpr bool CoeffByReference = (Flags & (LvalueBit | DirectAccessBit)) != 0;
81
82 using ComplexCoeffReturnType = std::conditional_t<CoeffByReference, const ComplexScalar&, ComplexScalar>;
83 using CoeffReturnType = std::conditional_t<CoeffByReference, const Scalar&, Scalar>;
84
85 EIGEN_DEVICE_FUNC explicit evaluator(XprType realView) : BaseEvaluator(realView.m_xpr) {}
86
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);
94 }
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];
102 }
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);
106 bool p = index & 1;
107 return p ? numext::imag(ccoeff) : numext::real(ccoeff);
108 }
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);
112 Index p = index & 1;
113 return reinterpret_cast<const Scalar(&)[2]>(ccoeff)[p];
114 }
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];
121 }
122 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
123 ComplexScalar& ccoeffRef = BaseEvaluator::coeffRef(index / 2);
124 Index p = index & 1;
125 return reinterpret_cast<Scalar(&)[2]>(ccoeffRef)[p];
126 }
127
128 // If the first index is odd (imaginary), discard the first scalar
129 // in 'result' and assign the missing scalar.
130 // This operation is safe as the real component of the first scalar must exist.
131 // The complex load then starts one real scalar before the requested address, so it cannot keep an aligned mode.
132
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);
145 if (p) {
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);
152 }
153 return result;
154 }
155
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)
162 bool p = index & 1;
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);
166 if (p) {
167 Scalar aux[RealPacketSize + 1];
168 pstoreu(aux, result);
169 aux[RealPacketSize] = coeff(index + RealPacketSize - 1);
170 result = ploadu<PacketType>(aux + 1);
171 }
172 return result;
173 }
174
175 // The requested real packet segment forms the half-open interval [begin, end), where 'end' = 'begin' + 'count'.
176 // In order to access the underlying complex array, even indices must be aligned with the real components
177 // of the complex scalars. 'begin' and 'count' must be modified as follows:
178 // a) 'begin' must be rounded down to the nearest even number; and
179 // b) 'end' must be rounded up to the nearest even number.
180
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);
197 if (p) {
198 Scalar aux[RealPacketSize + 1] = {};
199 pstoreu(aux, result);
200 // aux[actualEnd] is in the segment only when begin + count is even; otherwise it lies past the end.
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);
205 }
206 result = ploadu<PacketType>(aux + 1);
207 }
208 return result;
209 }
210
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;
220 bool p = index & 1;
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);
225 if (p) {
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);
230 }
231 return result;
232 }
233
234 // Stores of whole complex coefficients go to the underlying evaluator as complex packets; a store that starts or ends
235 // inside a coefficient (an odd real index or count) falls back to scalar stores. The swap kernel requires these.
236
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));
244 } else {
245 writePacketSegment<StoreMode, PacketType>(row, col, x, 0, RealPacketSize);
246 }
247 }
248
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));
255 } else {
256 writePacketSegment<StoreMode, PacketType>(index, x, 0, RealPacketSize);
257 }
258 }
259
260 template <int StoreMode, typename PacketType>
261 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writePacketSegment(Index row, Index col, const PacketType& x, Index begin,
262 Index count) {
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);
273 } else {
274 Scalar aux[RealPacketSize];
275 pstoreu(aux, x);
276 for (Index i = begin; i < begin + count; ++i)
277 coeffRef(IsRowMajor ? row : row + i, IsRowMajor ? col + i : col) = aux[i];
278 }
279 }
280
281 template <int StoreMode, typename PacketType>
282 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writePacketSegment(Index index, const PacketType& x, Index begin,
283 Index count) {
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);
291 } else {
292 Scalar aux[RealPacketSize];
293 pstoreu(aux, x);
294 for (Index i = begin; i < begin + count; ++i) coeffRef(index + i) = aux[i];
295 }
296 }
297};
298
299} // namespace internal
300
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)
305 public:
306 using Scalar = typename ExpressionTraits::Scalar;
307 using Nested = RealView;
308
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);
317 }
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());
322 }
323 EIGEN_DEVICE_FUNC const Scalar* data() const { return reinterpret_cast<const Scalar*>(m_xpr.data()); }
324
325 EIGEN_DEVICE_FUNC RealView(const RealView&) = default;
326
327 EIGEN_DEVICE_FUNC RealView& operator=(const RealView& other);
328
329 template <typename OtherDerived>
330 EIGEN_DEVICE_FUNC RealView& operator=(const RealView<OtherDerived>& other);
331
332 template <typename OtherDerived>
333 EIGEN_DEVICE_FUNC RealView& operator=(const DenseBase<OtherDerived>& other);
334
335 protected:
336 friend struct internal::evaluator<RealView>;
337 Xpr& m_xpr;
338};
339
340template <typename Xpr>
341EIGEN_DEVICE_FUNC RealView<Xpr>& RealView<Xpr>::operator=(const RealView& other) {
342 internal::call_assignment(*this, other);
343 return *this;
344}
345
346template <typename Xpr>
347template <typename OtherDerived>
348EIGEN_DEVICE_FUNC RealView<Xpr>& RealView<Xpr>::operator=(const RealView<OtherDerived>& other) {
349 internal::call_assignment(*this, other);
350 return *this;
351}
352
353template <typename Xpr>
354template <typename OtherDerived>
355EIGEN_DEVICE_FUNC RealView<Xpr>& RealView<Xpr>::operator=(const DenseBase<OtherDerived>& other) {
356 internal::call_assignment(*this, other.derived());
357 return *this;
358}
359
360template <typename Derived>
361EIGEN_DEVICE_FUNC typename DenseBase<Derived>::RealViewReturnType DenseBase<Derived>::realView() {
362 return RealViewReturnType(derived());
363}
364
365template <typename Derived>
366EIGEN_DEVICE_FUNC typename DenseBase<Derived>::ConstRealViewReturnType DenseBase<Derived>::realView() const {
367 return ConstRealViewReturnType(derived());
368}
369
370} // namespace Eigen
371
372#endif // EIGEN_REALVIEW_H
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