Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Block.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_BLOCK_H
13#define EIGEN_BLOCK_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21template <typename XprType_, int BlockRows, int BlockCols, bool InnerPanel_>
22struct traits<Block<XprType_, BlockRows, BlockCols, InnerPanel_>> : traits<XprType_> {
23 using Scalar = typename traits<XprType_>::Scalar;
24 using StorageKind = typename traits<XprType_>::StorageKind;
25 using XprKind = typename traits<XprType_>::XprKind;
26 using XprTypeNested = typename ref_selector<XprType_>::type;
27 using XprTypeNested_ = std::remove_reference_t<XprTypeNested>;
28 enum {
29 MatrixRows = traits<XprType_>::RowsAtCompileTime,
30 MatrixCols = traits<XprType_>::ColsAtCompileTime,
31 RowsAtCompileTime = MatrixRows == 0 ? 0 : BlockRows,
32 ColsAtCompileTime = MatrixCols == 0 ? 0 : BlockCols,
33 MaxRowsAtCompileTime = BlockRows == 0 ? 0
34 : RowsAtCompileTime != Dynamic ? int(RowsAtCompileTime)
35 : int(traits<XprType_>::MaxRowsAtCompileTime),
36 MaxColsAtCompileTime = BlockCols == 0 ? 0
37 : ColsAtCompileTime != Dynamic ? int(ColsAtCompileTime)
38 : int(traits<XprType_>::MaxColsAtCompileTime),
39
40 XprTypeIsRowMajor = (int(traits<XprType_>::Flags) & RowMajorBit) != 0,
41 IsRowMajor = (MaxRowsAtCompileTime == 1 && MaxColsAtCompileTime != 1) ? 1
42 : (MaxColsAtCompileTime == 1 && MaxRowsAtCompileTime != 1) ? 0
43 : XprTypeIsRowMajor,
44 HasSameStorageOrderAsXprType = (IsRowMajor == XprTypeIsRowMajor),
45 InnerSize = IsRowMajor ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
46 InnerStrideAtCompileTime = HasSameStorageOrderAsXprType ? int(inner_stride_at_compile_time<XprType_>::value)
47 : int(outer_stride_at_compile_time<XprType_>::value),
48 OuterStrideAtCompileTime = HasSameStorageOrderAsXprType ? int(outer_stride_at_compile_time<XprType_>::value)
49 : int(inner_stride_at_compile_time<XprType_>::value),
50
51 // FIXME, this traits is rather specialized for dense object and it needs to be cleaned further
52 FlagsLvalueBit = is_lvalue<XprType_>::value ? LvalueBit : 0,
53 FlagsRowMajorBit = IsRowMajor ? RowMajorBit : 0,
54 Flags = (traits<XprType_>::Flags & (DirectAccessBit | (InnerPanel_ ? CompressedAccessBit : 0))) | FlagsLvalueBit |
55 FlagsRowMajorBit,
56 // FIXME DirectAccessBit should not be handled by expressions
57 //
58 // Alignment is needed by MapBase's assertions
59 // We can safely set it to false here. Internal alignment errors will be detected by an eigen_internal_assert in the
60 // respective evaluator
61 Alignment = 0,
62 InnerPanel = InnerPanel_ ? 1 : 0
63 };
64};
65
66template <typename XprType, int BlockRows = Dynamic, int BlockCols = Dynamic, bool InnerPanel = false,
67 bool HasDirectAccess = internal::has_direct_access<XprType>::value>
68class BlockImpl_dense;
69
70} // end namespace internal
71
72template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel, typename StorageKind>
73class BlockImpl;
74
109template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
110class Block
111 : public BlockImpl<XprType, BlockRows, BlockCols, InnerPanel, typename internal::traits<XprType>::StorageKind> {
112 using Impl = BlockImpl<XprType, BlockRows, BlockCols, InnerPanel, typename internal::traits<XprType>::StorageKind>;
113 using BlockHelper = internal::block_xpr_helper<Block>;
114
115 public:
116 // typedef typename Impl::Base Base;
117 using Base = Impl;
118 EIGEN_GENERIC_PUBLIC_INTERFACE(Block)
119 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Block)
120
121 using NestedExpression = internal::remove_all_t<XprType>;
122
125 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Block(XprType& xpr, Index i) : Impl(xpr, i) {
126 eigen_assert((i >= 0) && (((BlockRows == 1) && (BlockCols == XprType::ColsAtCompileTime) && i < xpr.rows()) ||
127 ((BlockRows == XprType::RowsAtCompileTime) && (BlockCols == 1) && i < xpr.cols())));
128 }
129
132 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Block(XprType& xpr, Index startRow, Index startCol)
133 : Impl(xpr, startRow, startCol) {
134 EIGEN_STATIC_ASSERT(RowsAtCompileTime != Dynamic && ColsAtCompileTime != Dynamic,
135 THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE)
136 eigen_assert(startRow >= 0 && BlockRows >= 0 && startRow + BlockRows <= xpr.rows() && startCol >= 0 &&
137 BlockCols >= 0 && startCol + BlockCols <= xpr.cols());
138 }
139
142 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Block(XprType& xpr, Index startRow, Index startCol, Index blockRows,
143 Index blockCols)
144 : Impl(xpr, startRow, startCol, blockRows, blockCols) {
145 eigen_assert((RowsAtCompileTime == Dynamic || RowsAtCompileTime == blockRows) &&
146 (ColsAtCompileTime == Dynamic || ColsAtCompileTime == blockCols));
147 eigen_assert(startRow >= 0 && blockRows >= 0 && startRow <= xpr.rows() - blockRows && startCol >= 0 &&
148 blockCols >= 0 && startCol <= xpr.cols() - blockCols);
149 }
150
151 // convert nested blocks (e.g. Block<Block<MatrixType>>) to a simple block expression (Block<MatrixType>)
152
153 static constexpr bool UnwoundInnerPanel = BlockHelper::is_inner_panel(true);
156
157 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ConstUnwindReturnType unwind() const {
158 return ConstUnwindReturnType(BlockHelper::base(*this), BlockHelper::row(*this, 0), BlockHelper::col(*this, 0),
159 this->rows(), this->cols());
160 }
161
162 template <typename T = Block, typename EnableIf = std::enable_if_t<!std::is_const<T>::value>>
163 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE UnwindReturnType unwind() {
164 return UnwindReturnType(BlockHelper::base(*this), BlockHelper::row(*this, 0), BlockHelper::col(*this, 0),
165 this->rows(), this->cols());
166 }
167};
168
169// The generic default implementation for dense block simply forward to the internal::BlockImpl_dense
170// that must be specialized for direct and non-direct access...
171template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
172class BlockImpl<XprType, BlockRows, BlockCols, InnerPanel, Dense>
173 : public internal::BlockImpl_dense<XprType, BlockRows, BlockCols, InnerPanel> {
174 using Impl = internal::BlockImpl_dense<XprType, BlockRows, BlockCols, InnerPanel>;
175 using StorageIndex = typename XprType::StorageIndex;
176
177 public:
178 using Base = Impl;
179 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
180 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE BlockImpl(XprType& xpr, Index i) : Impl(xpr, i) {}
181 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE BlockImpl(XprType& xpr, Index startRow, Index startCol)
182 : Impl(xpr, startRow, startCol) {}
183 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE BlockImpl(XprType& xpr, Index startRow, Index startCol,
184 Index blockRows, Index blockCols)
185 : Impl(xpr, startRow, startCol, blockRows, blockCols) {}
186};
187
188namespace internal {
189
193template <typename XprType, int BlockRows, int BlockCols>
194struct block_vector_start {
195 static constexpr Index row(Index i) { return (BlockRows == 1) && (BlockCols == XprType::ColsAtCompileTime) ? i : 0; }
196 static constexpr Index col(Index i) { return (BlockRows == XprType::RowsAtCompileTime) && (BlockCols == 1) ? i : 0; }
197};
198
201template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel, bool HasDirectAccess>
202class BlockImpl_dense : public internal::dense_xpr_base<Block<XprType, BlockRows, BlockCols, InnerPanel>>::type {
203 using BlockType = Block<XprType, BlockRows, BlockCols, InnerPanel>;
204 using XprTypeNested = typename internal::ref_selector<XprType>::non_const_type;
205 using VectorStart = block_vector_start<XprType, BlockRows, BlockCols>;
206
207 public:
208 using Base = typename internal::dense_xpr_base<BlockType>::type;
209 EIGEN_DENSE_PUBLIC_INTERFACE(BlockType)
210 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl_dense)
211
212
214 EIGEN_DEVICE_FUNC constexpr BlockImpl_dense(XprType& xpr, Index i)
215 : m_xpr(xpr),
216 m_startRow(VectorStart::row(i)),
217 m_startCol(VectorStart::col(i)),
218 m_blockRows(BlockRows == 1 ? 1 : xpr.rows()),
219 m_blockCols(BlockCols == 1 ? 1 : xpr.cols()) {}
220
223 EIGEN_DEVICE_FUNC constexpr BlockImpl_dense(XprType& xpr, Index startRow, Index startCol)
224 : m_xpr(xpr), m_startRow(startRow), m_startCol(startCol), m_blockRows(BlockRows), m_blockCols(BlockCols) {}
225
228 EIGEN_DEVICE_FUNC constexpr BlockImpl_dense(XprType& xpr, Index startRow, Index startCol, Index blockRows,
229 Index blockCols)
230 : m_xpr(xpr), m_startRow(startRow), m_startCol(startCol), m_blockRows(blockRows), m_blockCols(blockCols) {}
231
232 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_blockRows.value(); }
233 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_blockCols.value(); }
234
235#ifdef EIGEN_PARSED_BY_DOXYGEN
237 EIGEN_DEVICE_FUNC constexpr const Scalar* data() const;
238 EIGEN_DEVICE_FUNC inline Index innerStride() const;
239 EIGEN_DEVICE_FUNC inline Index outerStride() const;
240#endif
241
242 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const internal::remove_all_t<XprTypeNested>& nestedExpression() const {
243 return m_xpr;
244 }
245
246 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE XprType& nestedExpression() { return m_xpr; }
247
248 EIGEN_DEVICE_FUNC constexpr StorageIndex startRow() const noexcept { return m_startRow.value(); }
249
250 EIGEN_DEVICE_FUNC constexpr StorageIndex startCol() const noexcept { return m_startCol.value(); }
251
252 protected:
253 XprTypeNested m_xpr;
254 const internal::variable_if_dynamic<StorageIndex, (XprType::RowsAtCompileTime == 1 && BlockRows == 1) ? 0 : Dynamic>
255 m_startRow;
256 const internal::variable_if_dynamic<StorageIndex, (XprType::ColsAtCompileTime == 1 && BlockCols == 1) ? 0 : Dynamic>
257 m_startCol;
258 const internal::variable_if_dynamic<StorageIndex, RowsAtCompileTime> m_blockRows;
259 const internal::variable_if_dynamic<StorageIndex, ColsAtCompileTime> m_blockCols;
260};
261
263template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
264class BlockImpl_dense<XprType, BlockRows, BlockCols, InnerPanel, true>
265 : public MapBase<Block<XprType, BlockRows, BlockCols, InnerPanel>> {
266 using BlockType = Block<XprType, BlockRows, BlockCols, InnerPanel>;
267 using XprTypeNested = typename internal::ref_selector<XprType>::non_const_type;
268 using VectorStart = block_vector_start<XprType, BlockRows, BlockCols>;
269
272 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE static auto block_data(XprType& xpr, Index startRow, Index startCol,
273 Index blockRows, Index blockCols)
274 -> decltype(xpr.data()) {
275 if (blockRows == 0 || blockCols == 0 || xpr.data() == nullptr) {
276 decltype(xpr.data()) data = nullptr;
277#if EIGEN_GNUC_STRICT_AT_LEAST(13, 0, 0) && defined(__ARM_FEATURE_SVE2) && !defined(EIGEN_GPU_COMPILE_PHASE)
278 // GCC 13 and later can emit WHILEWR with invalid WZR operands for known-null block pointers.
279 asm("" : "+r"(data));
280#endif
281 return data;
282 }
283 return xpr.data() + startRow * xpr.rowStride() + startCol * xpr.colStride();
284 }
285
286 public:
287 using Base = MapBase<BlockType>;
288 EIGEN_DENSE_PUBLIC_INTERFACE(BlockType)
289 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl_dense)
290
291
293 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE BlockImpl_dense(XprType& xpr, Index i)
294 : Base(block_data(xpr, VectorStart::row(i), VectorStart::col(i), BlockRows == 1 ? 1 : xpr.rows(),
295 BlockCols == 1 ? 1 : xpr.cols()),
296 BlockRows == 1 ? 1 : xpr.rows(), BlockCols == 1 ? 1 : xpr.cols()),
297 m_xpr(xpr),
298 m_startRow(VectorStart::row(i)),
299 m_startCol(VectorStart::col(i)),
300 m_outerStride(outer_stride(xpr)) {}
301
304 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE BlockImpl_dense(XprType& xpr, Index startRow, Index startCol)
305 : Base(block_data(xpr, startRow, startCol, BlockRows, BlockCols)),
306 m_xpr(xpr),
307 m_startRow(startRow),
308 m_startCol(startCol),
309 m_outerStride(outer_stride(xpr)) {}
310
313 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE BlockImpl_dense(XprType& xpr, Index startRow, Index startCol, Index blockRows,
314 Index blockCols)
315 : Base(block_data(xpr, startRow, startCol, blockRows, blockCols), blockRows, blockCols),
316 m_xpr(xpr),
317 m_startRow(startRow),
318 m_startCol(startCol),
319 m_outerStride(outer_stride(xpr)) {}
320
321 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const internal::remove_all_t<XprTypeNested>& nestedExpression() const noexcept {
322 return m_xpr;
323 }
324
325 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE XprType& nestedExpression() { return m_xpr; }
326
328 EIGEN_DEVICE_FUNC constexpr Index innerStride() const noexcept {
329 return internal::traits<BlockType>::HasSameStorageOrderAsXprType ? m_xpr.innerStride() : m_xpr.outerStride();
330 }
331
333 EIGEN_DEVICE_FUNC constexpr Index outerStride() const noexcept { return m_outerStride; }
334
335 EIGEN_DEVICE_FUNC constexpr StorageIndex startRow() const noexcept { return m_startRow.value(); }
336
337 EIGEN_DEVICE_FUNC constexpr StorageIndex startCol() const noexcept { return m_startCol.value(); }
338
339 protected:
340 EIGEN_DEVICE_FUNC static constexpr Index outer_stride(const XprType& xpr) {
341 return internal::traits<BlockType>::HasSameStorageOrderAsXprType ? xpr.outerStride() : xpr.innerStride();
342 }
343
344 XprTypeNested m_xpr;
345 const internal::variable_if_dynamic<StorageIndex, (XprType::RowsAtCompileTime == 1 && BlockRows == 1) ? 0 : Dynamic>
346 m_startRow;
347 const internal::variable_if_dynamic<StorageIndex, (XprType::ColsAtCompileTime == 1 && BlockCols == 1) ? 0 : Dynamic>
348 m_startCol;
349 Index m_outerStride;
350};
351
352} // end namespace internal
353
354} // end namespace Eigen
355
356#endif // EIGEN_BLOCK_H
Expression of a fixed-size or dynamic-size block.
Definition Block.h:111
constexpr Block(XprType &xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
Definition Block.h:142
constexpr Block(XprType &xpr, Index i)
Definition Block.h:125
constexpr Block(XprType &xpr, Index startRow, Index startCol)
Definition Block.h:132
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71
constexpr unsigned int CompressedAccessBit
Definition Constants.h:196