$darkmode
Eigen  5.0.1-dev
MapBase.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2007-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
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 
11 #ifndef EIGEN_MAPBASE_H
12 #define EIGEN_MAPBASE_H
13 
14 #define EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived) \
15  EIGEN_STATIC_ASSERT((int(internal::evaluator<Derived>::Flags) & LinearAccessBit) || Derived::IsVectorAtCompileTime, \
16  YOU_ARE_TRYING_TO_USE_AN_INDEX_BASED_ACCESSOR_ON_AN_EXPRESSION_THAT_DOES_NOT_SUPPORT_THAT)
17 
18 // IWYU pragma: private
19 #include "./InternalHeaderCheck.h"
20 
21 namespace Eigen {
22 
40 template <typename Derived>
41 class MapBase<Derived, ReadOnlyAccessors> : public internal::dense_xpr_base<Derived>::type {
42  public:
43  typedef typename internal::dense_xpr_base<Derived>::type Base;
44  enum {
45  RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
46  ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
47  InnerStrideAtCompileTime = internal::traits<Derived>::InnerStrideAtCompileTime,
48  SizeAtCompileTime = Base::SizeAtCompileTime
49  };
50 
51  typedef typename internal::traits<Derived>::StorageKind StorageKind;
52  typedef typename internal::traits<Derived>::Scalar Scalar;
53  typedef typename internal::packet_traits<Scalar>::type PacketScalar;
54  typedef typename NumTraits<Scalar>::Real RealScalar;
55  typedef std::conditional_t<bool(internal::is_lvalue<Derived>::value), Scalar*, const Scalar*> PointerType;
56 
57  using Base::derived;
58  // using Base::RowsAtCompileTime;
59  // using Base::ColsAtCompileTime;
60  // using Base::SizeAtCompileTime;
61  using Base::Flags;
62  using Base::IsRowMajor;
63  using Base::IsVectorAtCompileTime;
64  using Base::MaxColsAtCompileTime;
65  using Base::MaxRowsAtCompileTime;
66  using Base::MaxSizeAtCompileTime;
67 
68  using Base::coeff;
69  using Base::coeffRef;
70  using Base::cols;
71  using Base::eval;
72  using Base::lazyAssign;
73  using Base::rows;
74  using Base::size;
75 
76  using Base::colStride;
77  using Base::innerStride;
78  using Base::outerStride;
79  using Base::rowStride;
80 
81  // bug 217 - compile error on ICC 11.1
82  using Base::operator=;
83 
84  typedef typename Base::CoeffReturnType CoeffReturnType;
85 
87  EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_rows.value(); }
89  EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_cols.value(); }
90 
97  EIGEN_DEVICE_FUNC constexpr const Scalar* data() const { return m_data; }
98 
100  EIGEN_DEVICE_FUNC inline const Scalar& coeff(Index rowId, Index colId) const {
101  return m_data[colId * colStride() + rowId * rowStride()];
102  }
103 
105  EIGEN_DEVICE_FUNC inline const Scalar& coeff(Index index) const {
106  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
107  return m_data[index * innerStride()];
108  }
109 
111  EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index rowId, Index colId) const {
112  return this->m_data[colId * colStride() + rowId * rowStride()];
113  }
114 
116  EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index index) const {
117  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
118  return this->m_data[index * innerStride()];
119  }
120 
122  template <int LoadMode>
123  inline PacketScalar packet(Index rowId, Index colId) const {
124  return internal::ploadt<PacketScalar, LoadMode>(m_data + (colId * colStride() + rowId * rowStride()));
125  }
126 
128  template <int LoadMode>
129  inline PacketScalar packet(Index index) const {
130  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
131  return internal::ploadt<PacketScalar, LoadMode>(m_data + index * innerStride());
132  }
133 
135  EIGEN_DEVICE_FUNC explicit inline MapBase(PointerType dataPtr)
136  : m_data(dataPtr), m_rows(RowsAtCompileTime), m_cols(ColsAtCompileTime) {
137  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
138  checkSanity<Derived>();
139  }
140 
142  EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index vecSize)
143  : m_data(dataPtr),
144  m_rows(RowsAtCompileTime == Dynamic ? vecSize : Index(RowsAtCompileTime)),
145  m_cols(ColsAtCompileTime == Dynamic ? vecSize : Index(ColsAtCompileTime)) {
146  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
147  eigen_assert(vecSize >= 0);
148  eigen_assert(dataPtr == 0 || SizeAtCompileTime == Dynamic || SizeAtCompileTime == vecSize);
149  checkSanity<Derived>();
150  }
151 
153  EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index rows, Index cols)
154  : m_data(dataPtr), m_rows(rows), m_cols(cols) {
155  eigen_assert((dataPtr == 0) || (rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows) &&
156  cols >= 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)));
157  checkSanity<Derived>();
158  }
159 
160 #ifdef EIGEN_MAPBASE_PLUGIN
161 #include EIGEN_MAPBASE_PLUGIN
162 #endif
163 
164  protected:
165  EIGEN_DEFAULT_COPY_CONSTRUCTOR(MapBase)
166  EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(MapBase)
167 
168  template <typename T>
169  EIGEN_DEVICE_FUNC void checkSanity(std::enable_if_t<(internal::traits<T>::Alignment > 0), void*> = 0) const {
170 // Temporary macro to allow scalars to not be properly aligned. This is while we sort out failures
171 // in TensorFlow Lite that are currently relying on this UB.
172 #ifndef EIGEN_ALLOW_UNALIGNED_SCALARS
173  // Pointer must be aligned to the Scalar type, otherwise we get UB.
174  eigen_assert((std::uintptr_t(m_data) % alignof(Scalar) == 0) && "data is not scalar-aligned");
175 #endif
176 #if EIGEN_MAX_ALIGN_BYTES > 0
177  // innerStride() is not set yet when this function is called, so we optimistically assume the lowest plausible
178  // value:
179  const Index minInnerStride = InnerStrideAtCompileTime == Dynamic ? 1 : Index(InnerStrideAtCompileTime);
180  EIGEN_ONLY_USED_FOR_DEBUG(minInnerStride);
181  eigen_assert((((std::uintptr_t(m_data) % internal::traits<Derived>::Alignment) == 0) ||
182  (cols() * rows() * minInnerStride * sizeof(Scalar)) < internal::traits<Derived>::Alignment) &&
183  "data is not aligned");
184 #endif
185  }
186 
187  template <typename T>
188  EIGEN_DEVICE_FUNC void checkSanity(std::enable_if_t<internal::traits<T>::Alignment == 0, void*> = 0) const {
189 #ifndef EIGEN_ALLOW_UNALIGNED_SCALARS
190  // Pointer must be aligned to the Scalar type, otherwise we get UB.
191  eigen_assert((std::uintptr_t(m_data) % alignof(Scalar) == 0) && "data is not scalar-aligned");
192 #endif
193  }
194 
195  PointerType m_data;
196  const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
197  const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
198 };
199 
210 template <typename Derived>
211 class MapBase<Derived, WriteAccessors> : public MapBase<Derived, ReadOnlyAccessors> {
213 
214  public:
216 
217  typedef typename Base::Scalar Scalar;
218  typedef typename Base::PacketScalar PacketScalar;
219  typedef typename Base::StorageIndex StorageIndex;
220  typedef typename Base::PointerType PointerType;
221 
222  using Base::coeff;
223  using Base::coeffRef;
224  using Base::cols;
225  using Base::derived;
226  using Base::rows;
227  using Base::size;
228 
229  using Base::colStride;
230  using Base::innerStride;
231  using Base::outerStride;
232  using Base::rowStride;
233 
234  typedef std::conditional_t<internal::is_lvalue<Derived>::value, Scalar, const Scalar> ScalarWithConstIfNotLvalue;
235 
236  EIGEN_DEVICE_FUNC constexpr const Scalar* data() const { return this->m_data; }
237  EIGEN_DEVICE_FUNC constexpr ScalarWithConstIfNotLvalue* data() {
238  return this->m_data;
239  } // no const-cast here so non-const-correct code will give a compile error
240 
241  EIGEN_DEVICE_FUNC inline ScalarWithConstIfNotLvalue& coeffRef(Index row, Index col) {
242  return this->m_data[col * colStride() + row * rowStride()];
243  }
244 
245  EIGEN_DEVICE_FUNC inline ScalarWithConstIfNotLvalue& coeffRef(Index index) {
246  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
247  return this->m_data[index * innerStride()];
248  }
249 
250  template <int StoreMode>
251  inline void writePacket(Index row, Index col, const PacketScalar& val) {
252  internal::pstoret<Scalar, PacketScalar, StoreMode>(this->m_data + (col * colStride() + row * rowStride()), val);
253  }
254 
255  template <int StoreMode>
256  inline void writePacket(Index index, const PacketScalar& val) {
257  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
258  internal::pstoret<Scalar, PacketScalar, StoreMode>(this->m_data + index * innerStride(), val);
259  }
260 
261  EIGEN_DEVICE_FUNC explicit inline MapBase(PointerType dataPtr) : Base(dataPtr) {}
262  EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index vecSize) : Base(dataPtr, vecSize) {}
263  EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index rows, Index cols) : Base(dataPtr, rows, cols) {}
264 
265  EIGEN_DEVICE_FUNC Derived& operator=(const MapBase& other) {
266  ReadOnlyMapBase::Base::operator=(other);
267  return derived();
268  }
269 
270  // In theory we could simply refer to Base:Base::operator=, but MSVC does not like Base::Base,
271  // see bugs 821 and 920.
272  using ReadOnlyMapBase::Base::operator=;
273 
274  protected:
275  EIGEN_DEFAULT_COPY_CONSTRUCTOR(MapBase)
276  EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(MapBase)
277 };
278 
279 #undef EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS
280 
281 } // end namespace Eigen
282 
283 #endif // EIGEN_MAPBASE_H
Definition: Constants.h:374
constexpr Index rows() const noexcept
Definition: MapBase.h:87
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
const Scalar & coeffRef(Index index) const
Definition: MapBase.h:116
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
const Scalar & coeff(Index index) const
Definition: MapBase.h:105
Definition: Constants.h:372
constexpr const Scalar * data() const
Definition: MapBase.h:97
Base class for dense Map and Block expression with direct access.
Definition: MapBase.h:41
const Scalar & coeffRef(Index rowId, Index colId) const
Definition: MapBase.h:111
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
constexpr Index cols() const noexcept
Definition: MapBase.h:89
const int Dynamic
Definition: Constants.h:25
const Scalar & coeff(Index rowId, Index colId) const
Definition: MapBase.h:100