CoreEvaluators.h 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2011 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. // Copyright (C) 2011-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
  6. // Copyright (C) 2011-2012 Jitse Niesen <jitse@maths.leeds.ac.uk>
  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. #ifndef EIGEN_COREEVALUATORS_H
  12. #define EIGEN_COREEVALUATORS_H
  13. namespace Eigen {
  14. namespace internal {
  15. // This class returns the evaluator kind from the expression storage kind.
  16. // Default assumes index based accessors
  17. template<typename StorageKind>
  18. struct storage_kind_to_evaluator_kind {
  19. typedef IndexBased Kind;
  20. };
  21. // This class returns the evaluator shape from the expression storage kind.
  22. // It can be Dense, Sparse, Triangular, Diagonal, SelfAdjoint, Band, etc.
  23. template<typename StorageKind> struct storage_kind_to_shape;
  24. template<> struct storage_kind_to_shape<Dense> { typedef DenseShape Shape; };
  25. template<> struct storage_kind_to_shape<SolverStorage> { typedef SolverShape Shape; };
  26. template<> struct storage_kind_to_shape<PermutationStorage> { typedef PermutationShape Shape; };
  27. template<> struct storage_kind_to_shape<TranspositionsStorage> { typedef TranspositionsShape Shape; };
  28. // Evaluators have to be specialized with respect to various criteria such as:
  29. // - storage/structure/shape
  30. // - scalar type
  31. // - etc.
  32. // Therefore, we need specialization of evaluator providing additional template arguments for each kind of evaluators.
  33. // We currently distinguish the following kind of evaluators:
  34. // - unary_evaluator for expressions taking only one arguments (CwiseUnaryOp, CwiseUnaryView, Transpose, MatrixWrapper, ArrayWrapper, Reverse, Replicate)
  35. // - binary_evaluator for expression taking two arguments (CwiseBinaryOp)
  36. // - ternary_evaluator for expression taking three arguments (CwiseTernaryOp)
  37. // - product_evaluator for linear algebra products (Product); special case of binary_evaluator because it requires additional tags for dispatching.
  38. // - mapbase_evaluator for Map, Block, Ref
  39. // - block_evaluator for Block (special dispatching to a mapbase_evaluator or unary_evaluator)
  40. template< typename T,
  41. typename Arg1Kind = typename evaluator_traits<typename T::Arg1>::Kind,
  42. typename Arg2Kind = typename evaluator_traits<typename T::Arg2>::Kind,
  43. typename Arg3Kind = typename evaluator_traits<typename T::Arg3>::Kind,
  44. typename Arg1Scalar = typename traits<typename T::Arg1>::Scalar,
  45. typename Arg2Scalar = typename traits<typename T::Arg2>::Scalar,
  46. typename Arg3Scalar = typename traits<typename T::Arg3>::Scalar> struct ternary_evaluator;
  47. template< typename T,
  48. typename LhsKind = typename evaluator_traits<typename T::Lhs>::Kind,
  49. typename RhsKind = typename evaluator_traits<typename T::Rhs>::Kind,
  50. typename LhsScalar = typename traits<typename T::Lhs>::Scalar,
  51. typename RhsScalar = typename traits<typename T::Rhs>::Scalar> struct binary_evaluator;
  52. template< typename T,
  53. typename Kind = typename evaluator_traits<typename T::NestedExpression>::Kind,
  54. typename Scalar = typename T::Scalar> struct unary_evaluator;
  55. // evaluator_traits<T> contains traits for evaluator<T>
  56. template<typename T>
  57. struct evaluator_traits_base
  58. {
  59. // by default, get evaluator kind and shape from storage
  60. typedef typename storage_kind_to_evaluator_kind<typename traits<T>::StorageKind>::Kind Kind;
  61. typedef typename storage_kind_to_shape<typename traits<T>::StorageKind>::Shape Shape;
  62. };
  63. // Default evaluator traits
  64. template<typename T>
  65. struct evaluator_traits : public evaluator_traits_base<T>
  66. {
  67. };
  68. template<typename T, typename Shape = typename evaluator_traits<T>::Shape >
  69. struct evaluator_assume_aliasing {
  70. static const bool value = false;
  71. };
  72. // By default, we assume a unary expression:
  73. template<typename T>
  74. struct evaluator : public unary_evaluator<T>
  75. {
  76. typedef unary_evaluator<T> Base;
  77. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  78. explicit evaluator(const T& xpr) : Base(xpr) {}
  79. };
  80. // TODO: Think about const-correctness
  81. template<typename T>
  82. struct evaluator<const T>
  83. : evaluator<T>
  84. {
  85. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  86. explicit evaluator(const T& xpr) : evaluator<T>(xpr) {}
  87. };
  88. // ---------- base class for all evaluators ----------
  89. template<typename ExpressionType>
  90. struct evaluator_base
  91. {
  92. // TODO that's not very nice to have to propagate all these traits. They are currently only needed to handle outer,inner indices.
  93. typedef traits<ExpressionType> ExpressionTraits;
  94. enum {
  95. Alignment = 0
  96. };
  97. // noncopyable:
  98. // Don't make this class inherit noncopyable as this kills EBO (Empty Base Optimization)
  99. // and make complex evaluator much larger than then should do.
  100. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE evaluator_base() {}
  101. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ~evaluator_base() {}
  102. private:
  103. EIGEN_DEVICE_FUNC evaluator_base(const evaluator_base&);
  104. EIGEN_DEVICE_FUNC const evaluator_base& operator=(const evaluator_base&);
  105. };
  106. // -------------------- Matrix and Array --------------------
  107. //
  108. // evaluator<PlainObjectBase> is a common base class for the
  109. // Matrix and Array evaluators.
  110. // Here we directly specialize evaluator. This is not really a unary expression, and it is, by definition, dense,
  111. // so no need for more sophisticated dispatching.
  112. // this helper permits to completely eliminate m_outerStride if it is known at compiletime.
  113. template<typename Scalar,int OuterStride> class plainobjectbase_evaluator_data {
  114. public:
  115. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  116. plainobjectbase_evaluator_data(const Scalar* ptr, Index outerStride) : data(ptr)
  117. {
  118. #ifndef EIGEN_INTERNAL_DEBUGGING
  119. EIGEN_UNUSED_VARIABLE(outerStride);
  120. #endif
  121. eigen_internal_assert(outerStride==OuterStride);
  122. }
  123. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
  124. Index outerStride() const EIGEN_NOEXCEPT { return OuterStride; }
  125. const Scalar *data;
  126. };
  127. template<typename Scalar> class plainobjectbase_evaluator_data<Scalar,Dynamic> {
  128. public:
  129. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  130. plainobjectbase_evaluator_data(const Scalar* ptr, Index outerStride) : data(ptr), m_outerStride(outerStride) {}
  131. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  132. Index outerStride() const { return m_outerStride; }
  133. const Scalar *data;
  134. protected:
  135. Index m_outerStride;
  136. };
  137. template<typename Derived>
  138. struct evaluator<PlainObjectBase<Derived> >
  139. : evaluator_base<Derived>
  140. {
  141. typedef PlainObjectBase<Derived> PlainObjectType;
  142. typedef typename PlainObjectType::Scalar Scalar;
  143. typedef typename PlainObjectType::CoeffReturnType CoeffReturnType;
  144. enum {
  145. IsRowMajor = PlainObjectType::IsRowMajor,
  146. IsVectorAtCompileTime = PlainObjectType::IsVectorAtCompileTime,
  147. RowsAtCompileTime = PlainObjectType::RowsAtCompileTime,
  148. ColsAtCompileTime = PlainObjectType::ColsAtCompileTime,
  149. CoeffReadCost = NumTraits<Scalar>::ReadCost,
  150. Flags = traits<Derived>::EvaluatorFlags,
  151. Alignment = traits<Derived>::Alignment
  152. };
  153. enum {
  154. // We do not need to know the outer stride for vectors
  155. OuterStrideAtCompileTime = IsVectorAtCompileTime ? 0
  156. : int(IsRowMajor) ? ColsAtCompileTime
  157. : RowsAtCompileTime
  158. };
  159. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  160. evaluator()
  161. : m_d(0,OuterStrideAtCompileTime)
  162. {
  163. EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
  164. }
  165. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  166. explicit evaluator(const PlainObjectType& m)
  167. : m_d(m.data(),IsVectorAtCompileTime ? 0 : m.outerStride())
  168. {
  169. EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
  170. }
  171. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  172. CoeffReturnType coeff(Index row, Index col) const
  173. {
  174. if (IsRowMajor)
  175. return m_d.data[row * m_d.outerStride() + col];
  176. else
  177. return m_d.data[row + col * m_d.outerStride()];
  178. }
  179. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  180. CoeffReturnType coeff(Index index) const
  181. {
  182. return m_d.data[index];
  183. }
  184. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  185. Scalar& coeffRef(Index row, Index col)
  186. {
  187. if (IsRowMajor)
  188. return const_cast<Scalar*>(m_d.data)[row * m_d.outerStride() + col];
  189. else
  190. return const_cast<Scalar*>(m_d.data)[row + col * m_d.outerStride()];
  191. }
  192. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  193. Scalar& coeffRef(Index index)
  194. {
  195. return const_cast<Scalar*>(m_d.data)[index];
  196. }
  197. template<int LoadMode, typename PacketType>
  198. EIGEN_STRONG_INLINE
  199. PacketType packet(Index row, Index col) const
  200. {
  201. if (IsRowMajor)
  202. return ploadt<PacketType, LoadMode>(m_d.data + row * m_d.outerStride() + col);
  203. else
  204. return ploadt<PacketType, LoadMode>(m_d.data + row + col * m_d.outerStride());
  205. }
  206. template<int LoadMode, typename PacketType>
  207. EIGEN_STRONG_INLINE
  208. PacketType packet(Index index) const
  209. {
  210. return ploadt<PacketType, LoadMode>(m_d.data + index);
  211. }
  212. template<int StoreMode,typename PacketType>
  213. EIGEN_STRONG_INLINE
  214. void writePacket(Index row, Index col, const PacketType& x)
  215. {
  216. if (IsRowMajor)
  217. return pstoret<Scalar, PacketType, StoreMode>
  218. (const_cast<Scalar*>(m_d.data) + row * m_d.outerStride() + col, x);
  219. else
  220. return pstoret<Scalar, PacketType, StoreMode>
  221. (const_cast<Scalar*>(m_d.data) + row + col * m_d.outerStride(), x);
  222. }
  223. template<int StoreMode, typename PacketType>
  224. EIGEN_STRONG_INLINE
  225. void writePacket(Index index, const PacketType& x)
  226. {
  227. return pstoret<Scalar, PacketType, StoreMode>(const_cast<Scalar*>(m_d.data) + index, x);
  228. }
  229. protected:
  230. plainobjectbase_evaluator_data<Scalar,OuterStrideAtCompileTime> m_d;
  231. };
  232. template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
  233. struct evaluator<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
  234. : evaluator<PlainObjectBase<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> > >
  235. {
  236. typedef Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> XprType;
  237. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  238. evaluator() {}
  239. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  240. explicit evaluator(const XprType& m)
  241. : evaluator<PlainObjectBase<XprType> >(m)
  242. { }
  243. };
  244. template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
  245. struct evaluator<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
  246. : evaluator<PlainObjectBase<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> > >
  247. {
  248. typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> XprType;
  249. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  250. evaluator() {}
  251. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  252. explicit evaluator(const XprType& m)
  253. : evaluator<PlainObjectBase<XprType> >(m)
  254. { }
  255. };
  256. // -------------------- Transpose --------------------
  257. template<typename ArgType>
  258. struct unary_evaluator<Transpose<ArgType>, IndexBased>
  259. : evaluator_base<Transpose<ArgType> >
  260. {
  261. typedef Transpose<ArgType> XprType;
  262. enum {
  263. CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
  264. Flags = evaluator<ArgType>::Flags ^ RowMajorBit,
  265. Alignment = evaluator<ArgType>::Alignment
  266. };
  267. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  268. explicit unary_evaluator(const XprType& t) : m_argImpl(t.nestedExpression()) {}
  269. typedef typename XprType::Scalar Scalar;
  270. typedef typename XprType::CoeffReturnType CoeffReturnType;
  271. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  272. CoeffReturnType coeff(Index row, Index col) const
  273. {
  274. return m_argImpl.coeff(col, row);
  275. }
  276. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  277. CoeffReturnType coeff(Index index) const
  278. {
  279. return m_argImpl.coeff(index);
  280. }
  281. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  282. Scalar& coeffRef(Index row, Index col)
  283. {
  284. return m_argImpl.coeffRef(col, row);
  285. }
  286. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  287. typename XprType::Scalar& coeffRef(Index index)
  288. {
  289. return m_argImpl.coeffRef(index);
  290. }
  291. template<int LoadMode, typename PacketType>
  292. EIGEN_STRONG_INLINE
  293. PacketType packet(Index row, Index col) const
  294. {
  295. return m_argImpl.template packet<LoadMode,PacketType>(col, row);
  296. }
  297. template<int LoadMode, typename PacketType>
  298. EIGEN_STRONG_INLINE
  299. PacketType packet(Index index) const
  300. {
  301. return m_argImpl.template packet<LoadMode,PacketType>(index);
  302. }
  303. template<int StoreMode, typename PacketType>
  304. EIGEN_STRONG_INLINE
  305. void writePacket(Index row, Index col, const PacketType& x)
  306. {
  307. m_argImpl.template writePacket<StoreMode,PacketType>(col, row, x);
  308. }
  309. template<int StoreMode, typename PacketType>
  310. EIGEN_STRONG_INLINE
  311. void writePacket(Index index, const PacketType& x)
  312. {
  313. m_argImpl.template writePacket<StoreMode,PacketType>(index, x);
  314. }
  315. protected:
  316. evaluator<ArgType> m_argImpl;
  317. };
  318. // -------------------- CwiseNullaryOp --------------------
  319. // Like Matrix and Array, this is not really a unary expression, so we directly specialize evaluator.
  320. // Likewise, there is not need to more sophisticated dispatching here.
  321. template<typename Scalar,typename NullaryOp,
  322. bool has_nullary = has_nullary_operator<NullaryOp>::value,
  323. bool has_unary = has_unary_operator<NullaryOp>::value,
  324. bool has_binary = has_binary_operator<NullaryOp>::value>
  325. struct nullary_wrapper
  326. {
  327. template <typename IndexType>
  328. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const NullaryOp& op, IndexType i, IndexType j) const { return op(i,j); }
  329. template <typename IndexType>
  330. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const NullaryOp& op, IndexType i) const { return op(i); }
  331. template <typename T, typename IndexType> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T packetOp(const NullaryOp& op, IndexType i, IndexType j) const { return op.template packetOp<T>(i,j); }
  332. template <typename T, typename IndexType> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T packetOp(const NullaryOp& op, IndexType i) const { return op.template packetOp<T>(i); }
  333. };
  334. template<typename Scalar,typename NullaryOp>
  335. struct nullary_wrapper<Scalar,NullaryOp,true,false,false>
  336. {
  337. template <typename IndexType>
  338. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const NullaryOp& op, IndexType=0, IndexType=0) const { return op(); }
  339. template <typename T, typename IndexType> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T packetOp(const NullaryOp& op, IndexType=0, IndexType=0) const { return op.template packetOp<T>(); }
  340. };
  341. template<typename Scalar,typename NullaryOp>
  342. struct nullary_wrapper<Scalar,NullaryOp,false,false,true>
  343. {
  344. template <typename IndexType>
  345. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const NullaryOp& op, IndexType i, IndexType j=0) const { return op(i,j); }
  346. template <typename T, typename IndexType> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T packetOp(const NullaryOp& op, IndexType i, IndexType j=0) const { return op.template packetOp<T>(i,j); }
  347. };
  348. // We need the following specialization for vector-only functors assigned to a runtime vector,
  349. // for instance, using linspace and assigning a RowVectorXd to a MatrixXd or even a row of a MatrixXd.
  350. // In this case, i==0 and j is used for the actual iteration.
  351. template<typename Scalar,typename NullaryOp>
  352. struct nullary_wrapper<Scalar,NullaryOp,false,true,false>
  353. {
  354. template <typename IndexType>
  355. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const NullaryOp& op, IndexType i, IndexType j) const {
  356. eigen_assert(i==0 || j==0);
  357. return op(i+j);
  358. }
  359. template <typename T, typename IndexType> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T packetOp(const NullaryOp& op, IndexType i, IndexType j) const {
  360. eigen_assert(i==0 || j==0);
  361. return op.template packetOp<T>(i+j);
  362. }
  363. template <typename IndexType>
  364. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const NullaryOp& op, IndexType i) const { return op(i); }
  365. template <typename T, typename IndexType>
  366. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T packetOp(const NullaryOp& op, IndexType i) const { return op.template packetOp<T>(i); }
  367. };
  368. template<typename Scalar,typename NullaryOp>
  369. struct nullary_wrapper<Scalar,NullaryOp,false,false,false> {};
  370. #if 0 && EIGEN_COMP_MSVC>0
  371. // Disable this ugly workaround. This is now handled in traits<Ref>::match,
  372. // but this piece of code might still become handly if some other weird compilation
  373. // erros pop up again.
  374. // MSVC exhibits a weird compilation error when
  375. // compiling:
  376. // Eigen::MatrixXf A = MatrixXf::Random(3,3);
  377. // Ref<const MatrixXf> R = 2.f*A;
  378. // and that has_*ary_operator<scalar_constant_op<float>> have not been instantiated yet.
  379. // The "problem" is that evaluator<2.f*A> is instantiated by traits<Ref>::match<2.f*A>
  380. // and at that time has_*ary_operator<T> returns true regardless of T.
  381. // Then nullary_wrapper is badly instantiated as nullary_wrapper<.,.,true,true,true>.
  382. // The trick is thus to defer the proper instantiation of nullary_wrapper when coeff(),
  383. // and packet() are really instantiated as implemented below:
  384. // This is a simple wrapper around Index to enforce the re-instantiation of
  385. // has_*ary_operator when needed.
  386. template<typename T> struct nullary_wrapper_workaround_msvc {
  387. nullary_wrapper_workaround_msvc(const T&);
  388. operator T()const;
  389. };
  390. template<typename Scalar,typename NullaryOp>
  391. struct nullary_wrapper<Scalar,NullaryOp,true,true,true>
  392. {
  393. template <typename IndexType>
  394. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const NullaryOp& op, IndexType i, IndexType j) const {
  395. return nullary_wrapper<Scalar,NullaryOp,
  396. has_nullary_operator<NullaryOp,nullary_wrapper_workaround_msvc<IndexType> >::value,
  397. has_unary_operator<NullaryOp,nullary_wrapper_workaround_msvc<IndexType> >::value,
  398. has_binary_operator<NullaryOp,nullary_wrapper_workaround_msvc<IndexType> >::value>().operator()(op,i,j);
  399. }
  400. template <typename IndexType>
  401. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const NullaryOp& op, IndexType i) const {
  402. return nullary_wrapper<Scalar,NullaryOp,
  403. has_nullary_operator<NullaryOp,nullary_wrapper_workaround_msvc<IndexType> >::value,
  404. has_unary_operator<NullaryOp,nullary_wrapper_workaround_msvc<IndexType> >::value,
  405. has_binary_operator<NullaryOp,nullary_wrapper_workaround_msvc<IndexType> >::value>().operator()(op,i);
  406. }
  407. template <typename T, typename IndexType>
  408. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T packetOp(const NullaryOp& op, IndexType i, IndexType j) const {
  409. return nullary_wrapper<Scalar,NullaryOp,
  410. has_nullary_operator<NullaryOp,nullary_wrapper_workaround_msvc<IndexType> >::value,
  411. has_unary_operator<NullaryOp,nullary_wrapper_workaround_msvc<IndexType> >::value,
  412. has_binary_operator<NullaryOp,nullary_wrapper_workaround_msvc<IndexType> >::value>().template packetOp<T>(op,i,j);
  413. }
  414. template <typename T, typename IndexType>
  415. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T packetOp(const NullaryOp& op, IndexType i) const {
  416. return nullary_wrapper<Scalar,NullaryOp,
  417. has_nullary_operator<NullaryOp,nullary_wrapper_workaround_msvc<IndexType> >::value,
  418. has_unary_operator<NullaryOp,nullary_wrapper_workaround_msvc<IndexType> >::value,
  419. has_binary_operator<NullaryOp,nullary_wrapper_workaround_msvc<IndexType> >::value>().template packetOp<T>(op,i);
  420. }
  421. };
  422. #endif // MSVC workaround
  423. template<typename NullaryOp, typename PlainObjectType>
  424. struct evaluator<CwiseNullaryOp<NullaryOp,PlainObjectType> >
  425. : evaluator_base<CwiseNullaryOp<NullaryOp,PlainObjectType> >
  426. {
  427. typedef CwiseNullaryOp<NullaryOp,PlainObjectType> XprType;
  428. typedef typename internal::remove_all<PlainObjectType>::type PlainObjectTypeCleaned;
  429. enum {
  430. CoeffReadCost = internal::functor_traits<NullaryOp>::Cost,
  431. Flags = (evaluator<PlainObjectTypeCleaned>::Flags
  432. & ( HereditaryBits
  433. | (functor_has_linear_access<NullaryOp>::ret ? LinearAccessBit : 0)
  434. | (functor_traits<NullaryOp>::PacketAccess ? PacketAccessBit : 0)))
  435. | (functor_traits<NullaryOp>::IsRepeatable ? 0 : EvalBeforeNestingBit),
  436. Alignment = AlignedMax
  437. };
  438. EIGEN_DEVICE_FUNC explicit evaluator(const XprType& n)
  439. : m_functor(n.functor()), m_wrapper()
  440. {
  441. EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
  442. }
  443. typedef typename XprType::CoeffReturnType CoeffReturnType;
  444. template <typename IndexType>
  445. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  446. CoeffReturnType coeff(IndexType row, IndexType col) const
  447. {
  448. return m_wrapper(m_functor, row, col);
  449. }
  450. template <typename IndexType>
  451. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  452. CoeffReturnType coeff(IndexType index) const
  453. {
  454. return m_wrapper(m_functor,index);
  455. }
  456. template<int LoadMode, typename PacketType, typename IndexType>
  457. EIGEN_STRONG_INLINE
  458. PacketType packet(IndexType row, IndexType col) const
  459. {
  460. return m_wrapper.template packetOp<PacketType>(m_functor, row, col);
  461. }
  462. template<int LoadMode, typename PacketType, typename IndexType>
  463. EIGEN_STRONG_INLINE
  464. PacketType packet(IndexType index) const
  465. {
  466. return m_wrapper.template packetOp<PacketType>(m_functor, index);
  467. }
  468. protected:
  469. const NullaryOp m_functor;
  470. const internal::nullary_wrapper<CoeffReturnType,NullaryOp> m_wrapper;
  471. };
  472. // -------------------- CwiseUnaryOp --------------------
  473. template<typename UnaryOp, typename ArgType>
  474. struct unary_evaluator<CwiseUnaryOp<UnaryOp, ArgType>, IndexBased >
  475. : evaluator_base<CwiseUnaryOp<UnaryOp, ArgType> >
  476. {
  477. typedef CwiseUnaryOp<UnaryOp, ArgType> XprType;
  478. enum {
  479. CoeffReadCost = int(evaluator<ArgType>::CoeffReadCost) + int(functor_traits<UnaryOp>::Cost),
  480. Flags = evaluator<ArgType>::Flags
  481. & (HereditaryBits | LinearAccessBit | (functor_traits<UnaryOp>::PacketAccess ? PacketAccessBit : 0)),
  482. Alignment = evaluator<ArgType>::Alignment
  483. };
  484. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  485. explicit unary_evaluator(const XprType& op) : m_d(op)
  486. {
  487. EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<UnaryOp>::Cost);
  488. EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
  489. }
  490. typedef typename XprType::CoeffReturnType CoeffReturnType;
  491. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  492. CoeffReturnType coeff(Index row, Index col) const
  493. {
  494. return m_d.func()(m_d.argImpl.coeff(row, col));
  495. }
  496. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  497. CoeffReturnType coeff(Index index) const
  498. {
  499. return m_d.func()(m_d.argImpl.coeff(index));
  500. }
  501. template<int LoadMode, typename PacketType>
  502. EIGEN_STRONG_INLINE
  503. PacketType packet(Index row, Index col) const
  504. {
  505. return m_d.func().packetOp(m_d.argImpl.template packet<LoadMode, PacketType>(row, col));
  506. }
  507. template<int LoadMode, typename PacketType>
  508. EIGEN_STRONG_INLINE
  509. PacketType packet(Index index) const
  510. {
  511. return m_d.func().packetOp(m_d.argImpl.template packet<LoadMode, PacketType>(index));
  512. }
  513. protected:
  514. // this helper permits to completely eliminate the functor if it is empty
  515. struct Data
  516. {
  517. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  518. Data(const XprType& xpr) : op(xpr.functor()), argImpl(xpr.nestedExpression()) {}
  519. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  520. const UnaryOp& func() const { return op; }
  521. UnaryOp op;
  522. evaluator<ArgType> argImpl;
  523. };
  524. Data m_d;
  525. };
  526. // -------------------- CwiseTernaryOp --------------------
  527. // this is a ternary expression
  528. template<typename TernaryOp, typename Arg1, typename Arg2, typename Arg3>
  529. struct evaluator<CwiseTernaryOp<TernaryOp, Arg1, Arg2, Arg3> >
  530. : public ternary_evaluator<CwiseTernaryOp<TernaryOp, Arg1, Arg2, Arg3> >
  531. {
  532. typedef CwiseTernaryOp<TernaryOp, Arg1, Arg2, Arg3> XprType;
  533. typedef ternary_evaluator<CwiseTernaryOp<TernaryOp, Arg1, Arg2, Arg3> > Base;
  534. EIGEN_DEVICE_FUNC explicit evaluator(const XprType& xpr) : Base(xpr) {}
  535. };
  536. template<typename TernaryOp, typename Arg1, typename Arg2, typename Arg3>
  537. struct ternary_evaluator<CwiseTernaryOp<TernaryOp, Arg1, Arg2, Arg3>, IndexBased, IndexBased>
  538. : evaluator_base<CwiseTernaryOp<TernaryOp, Arg1, Arg2, Arg3> >
  539. {
  540. typedef CwiseTernaryOp<TernaryOp, Arg1, Arg2, Arg3> XprType;
  541. enum {
  542. CoeffReadCost = int(evaluator<Arg1>::CoeffReadCost) + int(evaluator<Arg2>::CoeffReadCost) + int(evaluator<Arg3>::CoeffReadCost) + int(functor_traits<TernaryOp>::Cost),
  543. Arg1Flags = evaluator<Arg1>::Flags,
  544. Arg2Flags = evaluator<Arg2>::Flags,
  545. Arg3Flags = evaluator<Arg3>::Flags,
  546. SameType = is_same<typename Arg1::Scalar,typename Arg2::Scalar>::value && is_same<typename Arg1::Scalar,typename Arg3::Scalar>::value,
  547. StorageOrdersAgree = (int(Arg1Flags)&RowMajorBit)==(int(Arg2Flags)&RowMajorBit) && (int(Arg1Flags)&RowMajorBit)==(int(Arg3Flags)&RowMajorBit),
  548. Flags0 = (int(Arg1Flags) | int(Arg2Flags) | int(Arg3Flags)) & (
  549. HereditaryBits
  550. | (int(Arg1Flags) & int(Arg2Flags) & int(Arg3Flags) &
  551. ( (StorageOrdersAgree ? LinearAccessBit : 0)
  552. | (functor_traits<TernaryOp>::PacketAccess && StorageOrdersAgree && SameType ? PacketAccessBit : 0)
  553. )
  554. )
  555. ),
  556. Flags = (Flags0 & ~RowMajorBit) | (Arg1Flags & RowMajorBit),
  557. Alignment = EIGEN_PLAIN_ENUM_MIN(
  558. EIGEN_PLAIN_ENUM_MIN(evaluator<Arg1>::Alignment, evaluator<Arg2>::Alignment),
  559. evaluator<Arg3>::Alignment)
  560. };
  561. EIGEN_DEVICE_FUNC explicit ternary_evaluator(const XprType& xpr) : m_d(xpr)
  562. {
  563. EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<TernaryOp>::Cost);
  564. EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
  565. }
  566. typedef typename XprType::CoeffReturnType CoeffReturnType;
  567. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  568. CoeffReturnType coeff(Index row, Index col) const
  569. {
  570. return m_d.func()(m_d.arg1Impl.coeff(row, col), m_d.arg2Impl.coeff(row, col), m_d.arg3Impl.coeff(row, col));
  571. }
  572. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  573. CoeffReturnType coeff(Index index) const
  574. {
  575. return m_d.func()(m_d.arg1Impl.coeff(index), m_d.arg2Impl.coeff(index), m_d.arg3Impl.coeff(index));
  576. }
  577. template<int LoadMode, typename PacketType>
  578. EIGEN_STRONG_INLINE
  579. PacketType packet(Index row, Index col) const
  580. {
  581. return m_d.func().packetOp(m_d.arg1Impl.template packet<LoadMode,PacketType>(row, col),
  582. m_d.arg2Impl.template packet<LoadMode,PacketType>(row, col),
  583. m_d.arg3Impl.template packet<LoadMode,PacketType>(row, col));
  584. }
  585. template<int LoadMode, typename PacketType>
  586. EIGEN_STRONG_INLINE
  587. PacketType packet(Index index) const
  588. {
  589. return m_d.func().packetOp(m_d.arg1Impl.template packet<LoadMode,PacketType>(index),
  590. m_d.arg2Impl.template packet<LoadMode,PacketType>(index),
  591. m_d.arg3Impl.template packet<LoadMode,PacketType>(index));
  592. }
  593. protected:
  594. // this helper permits to completely eliminate the functor if it is empty
  595. struct Data
  596. {
  597. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  598. Data(const XprType& xpr) : op(xpr.functor()), arg1Impl(xpr.arg1()), arg2Impl(xpr.arg2()), arg3Impl(xpr.arg3()) {}
  599. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  600. const TernaryOp& func() const { return op; }
  601. TernaryOp op;
  602. evaluator<Arg1> arg1Impl;
  603. evaluator<Arg2> arg2Impl;
  604. evaluator<Arg3> arg3Impl;
  605. };
  606. Data m_d;
  607. };
  608. // -------------------- CwiseBinaryOp --------------------
  609. // this is a binary expression
  610. template<typename BinaryOp, typename Lhs, typename Rhs>
  611. struct evaluator<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
  612. : public binary_evaluator<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
  613. {
  614. typedef CwiseBinaryOp<BinaryOp, Lhs, Rhs> XprType;
  615. typedef binary_evaluator<CwiseBinaryOp<BinaryOp, Lhs, Rhs> > Base;
  616. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  617. explicit evaluator(const XprType& xpr) : Base(xpr) {}
  618. };
  619. template<typename BinaryOp, typename Lhs, typename Rhs>
  620. struct binary_evaluator<CwiseBinaryOp<BinaryOp, Lhs, Rhs>, IndexBased, IndexBased>
  621. : evaluator_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
  622. {
  623. typedef CwiseBinaryOp<BinaryOp, Lhs, Rhs> XprType;
  624. enum {
  625. CoeffReadCost = int(evaluator<Lhs>::CoeffReadCost) + int(evaluator<Rhs>::CoeffReadCost) + int(functor_traits<BinaryOp>::Cost),
  626. LhsFlags = evaluator<Lhs>::Flags,
  627. RhsFlags = evaluator<Rhs>::Flags,
  628. SameType = is_same<typename Lhs::Scalar,typename Rhs::Scalar>::value,
  629. StorageOrdersAgree = (int(LhsFlags)&RowMajorBit)==(int(RhsFlags)&RowMajorBit),
  630. Flags0 = (int(LhsFlags) | int(RhsFlags)) & (
  631. HereditaryBits
  632. | (int(LhsFlags) & int(RhsFlags) &
  633. ( (StorageOrdersAgree ? LinearAccessBit : 0)
  634. | (functor_traits<BinaryOp>::PacketAccess && StorageOrdersAgree && SameType ? PacketAccessBit : 0)
  635. )
  636. )
  637. ),
  638. Flags = (Flags0 & ~RowMajorBit) | (LhsFlags & RowMajorBit),
  639. Alignment = EIGEN_PLAIN_ENUM_MIN(evaluator<Lhs>::Alignment,evaluator<Rhs>::Alignment)
  640. };
  641. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  642. explicit binary_evaluator(const XprType& xpr) : m_d(xpr)
  643. {
  644. EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<BinaryOp>::Cost);
  645. EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
  646. }
  647. typedef typename XprType::CoeffReturnType CoeffReturnType;
  648. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  649. CoeffReturnType coeff(Index row, Index col) const
  650. {
  651. return m_d.func()(m_d.lhsImpl.coeff(row, col), m_d.rhsImpl.coeff(row, col));
  652. }
  653. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  654. CoeffReturnType coeff(Index index) const
  655. {
  656. return m_d.func()(m_d.lhsImpl.coeff(index), m_d.rhsImpl.coeff(index));
  657. }
  658. template<int LoadMode, typename PacketType>
  659. EIGEN_STRONG_INLINE
  660. PacketType packet(Index row, Index col) const
  661. {
  662. return m_d.func().packetOp(m_d.lhsImpl.template packet<LoadMode,PacketType>(row, col),
  663. m_d.rhsImpl.template packet<LoadMode,PacketType>(row, col));
  664. }
  665. template<int LoadMode, typename PacketType>
  666. EIGEN_STRONG_INLINE
  667. PacketType packet(Index index) const
  668. {
  669. return m_d.func().packetOp(m_d.lhsImpl.template packet<LoadMode,PacketType>(index),
  670. m_d.rhsImpl.template packet<LoadMode,PacketType>(index));
  671. }
  672. protected:
  673. // this helper permits to completely eliminate the functor if it is empty
  674. struct Data
  675. {
  676. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  677. Data(const XprType& xpr) : op(xpr.functor()), lhsImpl(xpr.lhs()), rhsImpl(xpr.rhs()) {}
  678. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  679. const BinaryOp& func() const { return op; }
  680. BinaryOp op;
  681. evaluator<Lhs> lhsImpl;
  682. evaluator<Rhs> rhsImpl;
  683. };
  684. Data m_d;
  685. };
  686. // -------------------- CwiseUnaryView --------------------
  687. template<typename UnaryOp, typename ArgType>
  688. struct unary_evaluator<CwiseUnaryView<UnaryOp, ArgType>, IndexBased>
  689. : evaluator_base<CwiseUnaryView<UnaryOp, ArgType> >
  690. {
  691. typedef CwiseUnaryView<UnaryOp, ArgType> XprType;
  692. enum {
  693. CoeffReadCost = int(evaluator<ArgType>::CoeffReadCost) + int(functor_traits<UnaryOp>::Cost),
  694. Flags = (evaluator<ArgType>::Flags & (HereditaryBits | LinearAccessBit | DirectAccessBit)),
  695. Alignment = 0 // FIXME it is not very clear why alignment is necessarily lost...
  696. };
  697. EIGEN_DEVICE_FUNC explicit unary_evaluator(const XprType& op) : m_d(op)
  698. {
  699. EIGEN_INTERNAL_CHECK_COST_VALUE(functor_traits<UnaryOp>::Cost);
  700. EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
  701. }
  702. typedef typename XprType::Scalar Scalar;
  703. typedef typename XprType::CoeffReturnType CoeffReturnType;
  704. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  705. CoeffReturnType coeff(Index row, Index col) const
  706. {
  707. return m_d.func()(m_d.argImpl.coeff(row, col));
  708. }
  709. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  710. CoeffReturnType coeff(Index index) const
  711. {
  712. return m_d.func()(m_d.argImpl.coeff(index));
  713. }
  714. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  715. Scalar& coeffRef(Index row, Index col)
  716. {
  717. return m_d.func()(m_d.argImpl.coeffRef(row, col));
  718. }
  719. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  720. Scalar& coeffRef(Index index)
  721. {
  722. return m_d.func()(m_d.argImpl.coeffRef(index));
  723. }
  724. protected:
  725. // this helper permits to completely eliminate the functor if it is empty
  726. struct Data
  727. {
  728. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  729. Data(const XprType& xpr) : op(xpr.functor()), argImpl(xpr.nestedExpression()) {}
  730. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  731. const UnaryOp& func() const { return op; }
  732. UnaryOp op;
  733. evaluator<ArgType> argImpl;
  734. };
  735. Data m_d;
  736. };
  737. // -------------------- Map --------------------
  738. // FIXME perhaps the PlainObjectType could be provided by Derived::PlainObject ?
  739. // but that might complicate template specialization
  740. template<typename Derived, typename PlainObjectType>
  741. struct mapbase_evaluator;
  742. template<typename Derived, typename PlainObjectType>
  743. struct mapbase_evaluator : evaluator_base<Derived>
  744. {
  745. typedef Derived XprType;
  746. typedef typename XprType::PointerType PointerType;
  747. typedef typename XprType::Scalar Scalar;
  748. typedef typename XprType::CoeffReturnType CoeffReturnType;
  749. enum {
  750. IsRowMajor = XprType::RowsAtCompileTime,
  751. ColsAtCompileTime = XprType::ColsAtCompileTime,
  752. CoeffReadCost = NumTraits<Scalar>::ReadCost
  753. };
  754. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  755. explicit mapbase_evaluator(const XprType& map)
  756. : m_data(const_cast<PointerType>(map.data())),
  757. m_innerStride(map.innerStride()),
  758. m_outerStride(map.outerStride())
  759. {
  760. EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(evaluator<Derived>::Flags&PacketAccessBit, internal::inner_stride_at_compile_time<Derived>::ret==1),
  761. PACKET_ACCESS_REQUIRES_TO_HAVE_INNER_STRIDE_FIXED_TO_1);
  762. EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
  763. }
  764. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  765. CoeffReturnType coeff(Index row, Index col) const
  766. {
  767. return m_data[col * colStride() + row * rowStride()];
  768. }
  769. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  770. CoeffReturnType coeff(Index index) const
  771. {
  772. return m_data[index * m_innerStride.value()];
  773. }
  774. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  775. Scalar& coeffRef(Index row, Index col)
  776. {
  777. return m_data[col * colStride() + row * rowStride()];
  778. }
  779. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  780. Scalar& coeffRef(Index index)
  781. {
  782. return m_data[index * m_innerStride.value()];
  783. }
  784. template<int LoadMode, typename PacketType>
  785. EIGEN_STRONG_INLINE
  786. PacketType packet(Index row, Index col) const
  787. {
  788. PointerType ptr = m_data + row * rowStride() + col * colStride();
  789. return internal::ploadt<PacketType, LoadMode>(ptr);
  790. }
  791. template<int LoadMode, typename PacketType>
  792. EIGEN_STRONG_INLINE
  793. PacketType packet(Index index) const
  794. {
  795. return internal::ploadt<PacketType, LoadMode>(m_data + index * m_innerStride.value());
  796. }
  797. template<int StoreMode, typename PacketType>
  798. EIGEN_STRONG_INLINE
  799. void writePacket(Index row, Index col, const PacketType& x)
  800. {
  801. PointerType ptr = m_data + row * rowStride() + col * colStride();
  802. return internal::pstoret<Scalar, PacketType, StoreMode>(ptr, x);
  803. }
  804. template<int StoreMode, typename PacketType>
  805. EIGEN_STRONG_INLINE
  806. void writePacket(Index index, const PacketType& x)
  807. {
  808. internal::pstoret<Scalar, PacketType, StoreMode>(m_data + index * m_innerStride.value(), x);
  809. }
  810. protected:
  811. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
  812. Index rowStride() const EIGEN_NOEXCEPT {
  813. return XprType::IsRowMajor ? m_outerStride.value() : m_innerStride.value();
  814. }
  815. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
  816. Index colStride() const EIGEN_NOEXCEPT {
  817. return XprType::IsRowMajor ? m_innerStride.value() : m_outerStride.value();
  818. }
  819. PointerType m_data;
  820. const internal::variable_if_dynamic<Index, XprType::InnerStrideAtCompileTime> m_innerStride;
  821. const internal::variable_if_dynamic<Index, XprType::OuterStrideAtCompileTime> m_outerStride;
  822. };
  823. template<typename PlainObjectType, int MapOptions, typename StrideType>
  824. struct evaluator<Map<PlainObjectType, MapOptions, StrideType> >
  825. : public mapbase_evaluator<Map<PlainObjectType, MapOptions, StrideType>, PlainObjectType>
  826. {
  827. typedef Map<PlainObjectType, MapOptions, StrideType> XprType;
  828. typedef typename XprType::Scalar Scalar;
  829. // TODO: should check for smaller packet types once we can handle multi-sized packet types
  830. typedef typename packet_traits<Scalar>::type PacketScalar;
  831. enum {
  832. InnerStrideAtCompileTime = StrideType::InnerStrideAtCompileTime == 0
  833. ? int(PlainObjectType::InnerStrideAtCompileTime)
  834. : int(StrideType::InnerStrideAtCompileTime),
  835. OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0
  836. ? int(PlainObjectType::OuterStrideAtCompileTime)
  837. : int(StrideType::OuterStrideAtCompileTime),
  838. HasNoInnerStride = InnerStrideAtCompileTime == 1,
  839. HasNoOuterStride = StrideType::OuterStrideAtCompileTime == 0,
  840. HasNoStride = HasNoInnerStride && HasNoOuterStride,
  841. IsDynamicSize = PlainObjectType::SizeAtCompileTime==Dynamic,
  842. PacketAccessMask = bool(HasNoInnerStride) ? ~int(0) : ~int(PacketAccessBit),
  843. LinearAccessMask = bool(HasNoStride) || bool(PlainObjectType::IsVectorAtCompileTime) ? ~int(0) : ~int(LinearAccessBit),
  844. Flags = int( evaluator<PlainObjectType>::Flags) & (LinearAccessMask&PacketAccessMask),
  845. Alignment = int(MapOptions)&int(AlignedMask)
  846. };
  847. EIGEN_DEVICE_FUNC explicit evaluator(const XprType& map)
  848. : mapbase_evaluator<XprType, PlainObjectType>(map)
  849. { }
  850. };
  851. // -------------------- Ref --------------------
  852. template<typename PlainObjectType, int RefOptions, typename StrideType>
  853. struct evaluator<Ref<PlainObjectType, RefOptions, StrideType> >
  854. : public mapbase_evaluator<Ref<PlainObjectType, RefOptions, StrideType>, PlainObjectType>
  855. {
  856. typedef Ref<PlainObjectType, RefOptions, StrideType> XprType;
  857. enum {
  858. Flags = evaluator<Map<PlainObjectType, RefOptions, StrideType> >::Flags,
  859. Alignment = evaluator<Map<PlainObjectType, RefOptions, StrideType> >::Alignment
  860. };
  861. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  862. explicit evaluator(const XprType& ref)
  863. : mapbase_evaluator<XprType, PlainObjectType>(ref)
  864. { }
  865. };
  866. // -------------------- Block --------------------
  867. template<typename ArgType, int BlockRows, int BlockCols, bool InnerPanel,
  868. bool HasDirectAccess = internal::has_direct_access<ArgType>::ret> struct block_evaluator;
  869. template<typename ArgType, int BlockRows, int BlockCols, bool InnerPanel>
  870. struct evaluator<Block<ArgType, BlockRows, BlockCols, InnerPanel> >
  871. : block_evaluator<ArgType, BlockRows, BlockCols, InnerPanel>
  872. {
  873. typedef Block<ArgType, BlockRows, BlockCols, InnerPanel> XprType;
  874. typedef typename XprType::Scalar Scalar;
  875. // TODO: should check for smaller packet types once we can handle multi-sized packet types
  876. typedef typename packet_traits<Scalar>::type PacketScalar;
  877. enum {
  878. CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
  879. RowsAtCompileTime = traits<XprType>::RowsAtCompileTime,
  880. ColsAtCompileTime = traits<XprType>::ColsAtCompileTime,
  881. MaxRowsAtCompileTime = traits<XprType>::MaxRowsAtCompileTime,
  882. MaxColsAtCompileTime = traits<XprType>::MaxColsAtCompileTime,
  883. ArgTypeIsRowMajor = (int(evaluator<ArgType>::Flags)&RowMajorBit) != 0,
  884. IsRowMajor = (MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1) ? 1
  885. : (MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1) ? 0
  886. : ArgTypeIsRowMajor,
  887. HasSameStorageOrderAsArgType = (IsRowMajor == ArgTypeIsRowMajor),
  888. InnerSize = IsRowMajor ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
  889. InnerStrideAtCompileTime = HasSameStorageOrderAsArgType
  890. ? int(inner_stride_at_compile_time<ArgType>::ret)
  891. : int(outer_stride_at_compile_time<ArgType>::ret),
  892. OuterStrideAtCompileTime = HasSameStorageOrderAsArgType
  893. ? int(outer_stride_at_compile_time<ArgType>::ret)
  894. : int(inner_stride_at_compile_time<ArgType>::ret),
  895. MaskPacketAccessBit = (InnerStrideAtCompileTime == 1 || HasSameStorageOrderAsArgType) ? PacketAccessBit : 0,
  896. FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1 || (InnerPanel && (evaluator<ArgType>::Flags&LinearAccessBit))) ? LinearAccessBit : 0,
  897. FlagsRowMajorBit = XprType::Flags&RowMajorBit,
  898. Flags0 = evaluator<ArgType>::Flags & ( (HereditaryBits & ~RowMajorBit) |
  899. DirectAccessBit |
  900. MaskPacketAccessBit),
  901. Flags = Flags0 | FlagsLinearAccessBit | FlagsRowMajorBit,
  902. PacketAlignment = unpacket_traits<PacketScalar>::alignment,
  903. Alignment0 = (InnerPanel && (OuterStrideAtCompileTime!=Dynamic)
  904. && (OuterStrideAtCompileTime!=0)
  905. && (((OuterStrideAtCompileTime * int(sizeof(Scalar))) % int(PacketAlignment)) == 0)) ? int(PacketAlignment) : 0,
  906. Alignment = EIGEN_PLAIN_ENUM_MIN(evaluator<ArgType>::Alignment, Alignment0)
  907. };
  908. typedef block_evaluator<ArgType, BlockRows, BlockCols, InnerPanel> block_evaluator_type;
  909. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  910. explicit evaluator(const XprType& block) : block_evaluator_type(block)
  911. {
  912. EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
  913. }
  914. };
  915. // no direct-access => dispatch to a unary evaluator
  916. template<typename ArgType, int BlockRows, int BlockCols, bool InnerPanel>
  917. struct block_evaluator<ArgType, BlockRows, BlockCols, InnerPanel, /*HasDirectAccess*/ false>
  918. : unary_evaluator<Block<ArgType, BlockRows, BlockCols, InnerPanel> >
  919. {
  920. typedef Block<ArgType, BlockRows, BlockCols, InnerPanel> XprType;
  921. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  922. explicit block_evaluator(const XprType& block)
  923. : unary_evaluator<XprType>(block)
  924. {}
  925. };
  926. template<typename ArgType, int BlockRows, int BlockCols, bool InnerPanel>
  927. struct unary_evaluator<Block<ArgType, BlockRows, BlockCols, InnerPanel>, IndexBased>
  928. : evaluator_base<Block<ArgType, BlockRows, BlockCols, InnerPanel> >
  929. {
  930. typedef Block<ArgType, BlockRows, BlockCols, InnerPanel> XprType;
  931. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  932. explicit unary_evaluator(const XprType& block)
  933. : m_argImpl(block.nestedExpression()),
  934. m_startRow(block.startRow()),
  935. m_startCol(block.startCol()),
  936. m_linear_offset(ForwardLinearAccess?(ArgType::IsRowMajor ? block.startRow()*block.nestedExpression().cols() + block.startCol() : block.startCol()*block.nestedExpression().rows() + block.startRow()):0)
  937. { }
  938. typedef typename XprType::Scalar Scalar;
  939. typedef typename XprType::CoeffReturnType CoeffReturnType;
  940. enum {
  941. RowsAtCompileTime = XprType::RowsAtCompileTime,
  942. ForwardLinearAccess = (InnerPanel || int(XprType::IsRowMajor)==int(ArgType::IsRowMajor)) && bool(evaluator<ArgType>::Flags&LinearAccessBit)
  943. };
  944. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  945. CoeffReturnType coeff(Index row, Index col) const
  946. {
  947. return m_argImpl.coeff(m_startRow.value() + row, m_startCol.value() + col);
  948. }
  949. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  950. CoeffReturnType coeff(Index index) const
  951. {
  952. return linear_coeff_impl(index, bool_constant<ForwardLinearAccess>());
  953. }
  954. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  955. Scalar& coeffRef(Index row, Index col)
  956. {
  957. return m_argImpl.coeffRef(m_startRow.value() + row, m_startCol.value() + col);
  958. }
  959. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  960. Scalar& coeffRef(Index index)
  961. {
  962. return linear_coeffRef_impl(index, bool_constant<ForwardLinearAccess>());
  963. }
  964. template<int LoadMode, typename PacketType>
  965. EIGEN_STRONG_INLINE
  966. PacketType packet(Index row, Index col) const
  967. {
  968. return m_argImpl.template packet<LoadMode,PacketType>(m_startRow.value() + row, m_startCol.value() + col);
  969. }
  970. template<int LoadMode, typename PacketType>
  971. EIGEN_STRONG_INLINE
  972. PacketType packet(Index index) const
  973. {
  974. if (ForwardLinearAccess)
  975. return m_argImpl.template packet<LoadMode,PacketType>(m_linear_offset.value() + index);
  976. else
  977. return packet<LoadMode,PacketType>(RowsAtCompileTime == 1 ? 0 : index,
  978. RowsAtCompileTime == 1 ? index : 0);
  979. }
  980. template<int StoreMode, typename PacketType>
  981. EIGEN_STRONG_INLINE
  982. void writePacket(Index row, Index col, const PacketType& x)
  983. {
  984. return m_argImpl.template writePacket<StoreMode,PacketType>(m_startRow.value() + row, m_startCol.value() + col, x);
  985. }
  986. template<int StoreMode, typename PacketType>
  987. EIGEN_STRONG_INLINE
  988. void writePacket(Index index, const PacketType& x)
  989. {
  990. if (ForwardLinearAccess)
  991. return m_argImpl.template writePacket<StoreMode,PacketType>(m_linear_offset.value() + index, x);
  992. else
  993. return writePacket<StoreMode,PacketType>(RowsAtCompileTime == 1 ? 0 : index,
  994. RowsAtCompileTime == 1 ? index : 0,
  995. x);
  996. }
  997. protected:
  998. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  999. CoeffReturnType linear_coeff_impl(Index index, internal::true_type /* ForwardLinearAccess */) const
  1000. {
  1001. return m_argImpl.coeff(m_linear_offset.value() + index);
  1002. }
  1003. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1004. CoeffReturnType linear_coeff_impl(Index index, internal::false_type /* not ForwardLinearAccess */) const
  1005. {
  1006. return coeff(RowsAtCompileTime == 1 ? 0 : index, RowsAtCompileTime == 1 ? index : 0);
  1007. }
  1008. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1009. Scalar& linear_coeffRef_impl(Index index, internal::true_type /* ForwardLinearAccess */)
  1010. {
  1011. return m_argImpl.coeffRef(m_linear_offset.value() + index);
  1012. }
  1013. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1014. Scalar& linear_coeffRef_impl(Index index, internal::false_type /* not ForwardLinearAccess */)
  1015. {
  1016. return coeffRef(RowsAtCompileTime == 1 ? 0 : index, RowsAtCompileTime == 1 ? index : 0);
  1017. }
  1018. evaluator<ArgType> m_argImpl;
  1019. const variable_if_dynamic<Index, (ArgType::RowsAtCompileTime == 1 && BlockRows==1) ? 0 : Dynamic> m_startRow;
  1020. const variable_if_dynamic<Index, (ArgType::ColsAtCompileTime == 1 && BlockCols==1) ? 0 : Dynamic> m_startCol;
  1021. const variable_if_dynamic<Index, ForwardLinearAccess ? Dynamic : 0> m_linear_offset;
  1022. };
  1023. // TODO: This evaluator does not actually use the child evaluator;
  1024. // all action is via the data() as returned by the Block expression.
  1025. template<typename ArgType, int BlockRows, int BlockCols, bool InnerPanel>
  1026. struct block_evaluator<ArgType, BlockRows, BlockCols, InnerPanel, /* HasDirectAccess */ true>
  1027. : mapbase_evaluator<Block<ArgType, BlockRows, BlockCols, InnerPanel>,
  1028. typename Block<ArgType, BlockRows, BlockCols, InnerPanel>::PlainObject>
  1029. {
  1030. typedef Block<ArgType, BlockRows, BlockCols, InnerPanel> XprType;
  1031. typedef typename XprType::Scalar Scalar;
  1032. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1033. explicit block_evaluator(const XprType& block)
  1034. : mapbase_evaluator<XprType, typename XprType::PlainObject>(block)
  1035. {
  1036. // TODO: for the 3.3 release, this should be turned to an internal assertion, but let's keep it as is for the beta lifetime
  1037. eigen_assert(((internal::UIntPtr(block.data()) % EIGEN_PLAIN_ENUM_MAX(1,evaluator<XprType>::Alignment)) == 0) && "data is not aligned");
  1038. }
  1039. };
  1040. // -------------------- Select --------------------
  1041. // NOTE shall we introduce a ternary_evaluator?
  1042. // TODO enable vectorization for Select
  1043. template<typename ConditionMatrixType, typename ThenMatrixType, typename ElseMatrixType>
  1044. struct evaluator<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >
  1045. : evaluator_base<Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> >
  1046. {
  1047. typedef Select<ConditionMatrixType, ThenMatrixType, ElseMatrixType> XprType;
  1048. enum {
  1049. CoeffReadCost = evaluator<ConditionMatrixType>::CoeffReadCost
  1050. + EIGEN_PLAIN_ENUM_MAX(evaluator<ThenMatrixType>::CoeffReadCost,
  1051. evaluator<ElseMatrixType>::CoeffReadCost),
  1052. Flags = (unsigned int)evaluator<ThenMatrixType>::Flags & evaluator<ElseMatrixType>::Flags & HereditaryBits,
  1053. Alignment = EIGEN_PLAIN_ENUM_MIN(evaluator<ThenMatrixType>::Alignment, evaluator<ElseMatrixType>::Alignment)
  1054. };
  1055. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1056. explicit evaluator(const XprType& select)
  1057. : m_conditionImpl(select.conditionMatrix()),
  1058. m_thenImpl(select.thenMatrix()),
  1059. m_elseImpl(select.elseMatrix())
  1060. {
  1061. EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
  1062. }
  1063. typedef typename XprType::CoeffReturnType CoeffReturnType;
  1064. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1065. CoeffReturnType coeff(Index row, Index col) const
  1066. {
  1067. if (m_conditionImpl.coeff(row, col))
  1068. return m_thenImpl.coeff(row, col);
  1069. else
  1070. return m_elseImpl.coeff(row, col);
  1071. }
  1072. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1073. CoeffReturnType coeff(Index index) const
  1074. {
  1075. if (m_conditionImpl.coeff(index))
  1076. return m_thenImpl.coeff(index);
  1077. else
  1078. return m_elseImpl.coeff(index);
  1079. }
  1080. protected:
  1081. evaluator<ConditionMatrixType> m_conditionImpl;
  1082. evaluator<ThenMatrixType> m_thenImpl;
  1083. evaluator<ElseMatrixType> m_elseImpl;
  1084. };
  1085. // -------------------- Replicate --------------------
  1086. template<typename ArgType, int RowFactor, int ColFactor>
  1087. struct unary_evaluator<Replicate<ArgType, RowFactor, ColFactor> >
  1088. : evaluator_base<Replicate<ArgType, RowFactor, ColFactor> >
  1089. {
  1090. typedef Replicate<ArgType, RowFactor, ColFactor> XprType;
  1091. typedef typename XprType::CoeffReturnType CoeffReturnType;
  1092. enum {
  1093. Factor = (RowFactor==Dynamic || ColFactor==Dynamic) ? Dynamic : RowFactor*ColFactor
  1094. };
  1095. typedef typename internal::nested_eval<ArgType,Factor>::type ArgTypeNested;
  1096. typedef typename internal::remove_all<ArgTypeNested>::type ArgTypeNestedCleaned;
  1097. enum {
  1098. CoeffReadCost = evaluator<ArgTypeNestedCleaned>::CoeffReadCost,
  1099. LinearAccessMask = XprType::IsVectorAtCompileTime ? LinearAccessBit : 0,
  1100. Flags = (evaluator<ArgTypeNestedCleaned>::Flags & (HereditaryBits|LinearAccessMask) & ~RowMajorBit) | (traits<XprType>::Flags & RowMajorBit),
  1101. Alignment = evaluator<ArgTypeNestedCleaned>::Alignment
  1102. };
  1103. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1104. explicit unary_evaluator(const XprType& replicate)
  1105. : m_arg(replicate.nestedExpression()),
  1106. m_argImpl(m_arg),
  1107. m_rows(replicate.nestedExpression().rows()),
  1108. m_cols(replicate.nestedExpression().cols())
  1109. {}
  1110. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1111. CoeffReturnType coeff(Index row, Index col) const
  1112. {
  1113. // try to avoid using modulo; this is a pure optimization strategy
  1114. const Index actual_row = internal::traits<XprType>::RowsAtCompileTime==1 ? 0
  1115. : RowFactor==1 ? row
  1116. : row % m_rows.value();
  1117. const Index actual_col = internal::traits<XprType>::ColsAtCompileTime==1 ? 0
  1118. : ColFactor==1 ? col
  1119. : col % m_cols.value();
  1120. return m_argImpl.coeff(actual_row, actual_col);
  1121. }
  1122. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1123. CoeffReturnType coeff(Index index) const
  1124. {
  1125. // try to avoid using modulo; this is a pure optimization strategy
  1126. const Index actual_index = internal::traits<XprType>::RowsAtCompileTime==1
  1127. ? (ColFactor==1 ? index : index%m_cols.value())
  1128. : (RowFactor==1 ? index : index%m_rows.value());
  1129. return m_argImpl.coeff(actual_index);
  1130. }
  1131. template<int LoadMode, typename PacketType>
  1132. EIGEN_STRONG_INLINE
  1133. PacketType packet(Index row, Index col) const
  1134. {
  1135. const Index actual_row = internal::traits<XprType>::RowsAtCompileTime==1 ? 0
  1136. : RowFactor==1 ? row
  1137. : row % m_rows.value();
  1138. const Index actual_col = internal::traits<XprType>::ColsAtCompileTime==1 ? 0
  1139. : ColFactor==1 ? col
  1140. : col % m_cols.value();
  1141. return m_argImpl.template packet<LoadMode,PacketType>(actual_row, actual_col);
  1142. }
  1143. template<int LoadMode, typename PacketType>
  1144. EIGEN_STRONG_INLINE
  1145. PacketType packet(Index index) const
  1146. {
  1147. const Index actual_index = internal::traits<XprType>::RowsAtCompileTime==1
  1148. ? (ColFactor==1 ? index : index%m_cols.value())
  1149. : (RowFactor==1 ? index : index%m_rows.value());
  1150. return m_argImpl.template packet<LoadMode,PacketType>(actual_index);
  1151. }
  1152. protected:
  1153. const ArgTypeNested m_arg;
  1154. evaluator<ArgTypeNestedCleaned> m_argImpl;
  1155. const variable_if_dynamic<Index, ArgType::RowsAtCompileTime> m_rows;
  1156. const variable_if_dynamic<Index, ArgType::ColsAtCompileTime> m_cols;
  1157. };
  1158. // -------------------- MatrixWrapper and ArrayWrapper --------------------
  1159. //
  1160. // evaluator_wrapper_base<T> is a common base class for the
  1161. // MatrixWrapper and ArrayWrapper evaluators.
  1162. template<typename XprType>
  1163. struct evaluator_wrapper_base
  1164. : evaluator_base<XprType>
  1165. {
  1166. typedef typename remove_all<typename XprType::NestedExpressionType>::type ArgType;
  1167. enum {
  1168. CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
  1169. Flags = evaluator<ArgType>::Flags,
  1170. Alignment = evaluator<ArgType>::Alignment
  1171. };
  1172. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1173. explicit evaluator_wrapper_base(const ArgType& arg) : m_argImpl(arg) {}
  1174. typedef typename ArgType::Scalar Scalar;
  1175. typedef typename ArgType::CoeffReturnType CoeffReturnType;
  1176. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1177. CoeffReturnType coeff(Index row, Index col) const
  1178. {
  1179. return m_argImpl.coeff(row, col);
  1180. }
  1181. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1182. CoeffReturnType coeff(Index index) const
  1183. {
  1184. return m_argImpl.coeff(index);
  1185. }
  1186. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1187. Scalar& coeffRef(Index row, Index col)
  1188. {
  1189. return m_argImpl.coeffRef(row, col);
  1190. }
  1191. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1192. Scalar& coeffRef(Index index)
  1193. {
  1194. return m_argImpl.coeffRef(index);
  1195. }
  1196. template<int LoadMode, typename PacketType>
  1197. EIGEN_STRONG_INLINE
  1198. PacketType packet(Index row, Index col) const
  1199. {
  1200. return m_argImpl.template packet<LoadMode,PacketType>(row, col);
  1201. }
  1202. template<int LoadMode, typename PacketType>
  1203. EIGEN_STRONG_INLINE
  1204. PacketType packet(Index index) const
  1205. {
  1206. return m_argImpl.template packet<LoadMode,PacketType>(index);
  1207. }
  1208. template<int StoreMode, typename PacketType>
  1209. EIGEN_STRONG_INLINE
  1210. void writePacket(Index row, Index col, const PacketType& x)
  1211. {
  1212. m_argImpl.template writePacket<StoreMode>(row, col, x);
  1213. }
  1214. template<int StoreMode, typename PacketType>
  1215. EIGEN_STRONG_INLINE
  1216. void writePacket(Index index, const PacketType& x)
  1217. {
  1218. m_argImpl.template writePacket<StoreMode>(index, x);
  1219. }
  1220. protected:
  1221. evaluator<ArgType> m_argImpl;
  1222. };
  1223. template<typename TArgType>
  1224. struct unary_evaluator<MatrixWrapper<TArgType> >
  1225. : evaluator_wrapper_base<MatrixWrapper<TArgType> >
  1226. {
  1227. typedef MatrixWrapper<TArgType> XprType;
  1228. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1229. explicit unary_evaluator(const XprType& wrapper)
  1230. : evaluator_wrapper_base<MatrixWrapper<TArgType> >(wrapper.nestedExpression())
  1231. { }
  1232. };
  1233. template<typename TArgType>
  1234. struct unary_evaluator<ArrayWrapper<TArgType> >
  1235. : evaluator_wrapper_base<ArrayWrapper<TArgType> >
  1236. {
  1237. typedef ArrayWrapper<TArgType> XprType;
  1238. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1239. explicit unary_evaluator(const XprType& wrapper)
  1240. : evaluator_wrapper_base<ArrayWrapper<TArgType> >(wrapper.nestedExpression())
  1241. { }
  1242. };
  1243. // -------------------- Reverse --------------------
  1244. // defined in Reverse.h:
  1245. template<typename PacketType, bool ReversePacket> struct reverse_packet_cond;
  1246. template<typename ArgType, int Direction>
  1247. struct unary_evaluator<Reverse<ArgType, Direction> >
  1248. : evaluator_base<Reverse<ArgType, Direction> >
  1249. {
  1250. typedef Reverse<ArgType, Direction> XprType;
  1251. typedef typename XprType::Scalar Scalar;
  1252. typedef typename XprType::CoeffReturnType CoeffReturnType;
  1253. enum {
  1254. IsRowMajor = XprType::IsRowMajor,
  1255. IsColMajor = !IsRowMajor,
  1256. ReverseRow = (Direction == Vertical) || (Direction == BothDirections),
  1257. ReverseCol = (Direction == Horizontal) || (Direction == BothDirections),
  1258. ReversePacket = (Direction == BothDirections)
  1259. || ((Direction == Vertical) && IsColMajor)
  1260. || ((Direction == Horizontal) && IsRowMajor),
  1261. CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
  1262. // let's enable LinearAccess only with vectorization because of the product overhead
  1263. // FIXME enable DirectAccess with negative strides?
  1264. Flags0 = evaluator<ArgType>::Flags,
  1265. LinearAccess = ( (Direction==BothDirections) && (int(Flags0)&PacketAccessBit) )
  1266. || ((ReverseRow && XprType::ColsAtCompileTime==1) || (ReverseCol && XprType::RowsAtCompileTime==1))
  1267. ? LinearAccessBit : 0,
  1268. Flags = int(Flags0) & (HereditaryBits | PacketAccessBit | LinearAccess),
  1269. Alignment = 0 // FIXME in some rare cases, Alignment could be preserved, like a Vector4f.
  1270. };
  1271. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1272. explicit unary_evaluator(const XprType& reverse)
  1273. : m_argImpl(reverse.nestedExpression()),
  1274. m_rows(ReverseRow ? reverse.nestedExpression().rows() : 1),
  1275. m_cols(ReverseCol ? reverse.nestedExpression().cols() : 1)
  1276. { }
  1277. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1278. CoeffReturnType coeff(Index row, Index col) const
  1279. {
  1280. return m_argImpl.coeff(ReverseRow ? m_rows.value() - row - 1 : row,
  1281. ReverseCol ? m_cols.value() - col - 1 : col);
  1282. }
  1283. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1284. CoeffReturnType coeff(Index index) const
  1285. {
  1286. return m_argImpl.coeff(m_rows.value() * m_cols.value() - index - 1);
  1287. }
  1288. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1289. Scalar& coeffRef(Index row, Index col)
  1290. {
  1291. return m_argImpl.coeffRef(ReverseRow ? m_rows.value() - row - 1 : row,
  1292. ReverseCol ? m_cols.value() - col - 1 : col);
  1293. }
  1294. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1295. Scalar& coeffRef(Index index)
  1296. {
  1297. return m_argImpl.coeffRef(m_rows.value() * m_cols.value() - index - 1);
  1298. }
  1299. template<int LoadMode, typename PacketType>
  1300. EIGEN_STRONG_INLINE
  1301. PacketType packet(Index row, Index col) const
  1302. {
  1303. enum {
  1304. PacketSize = unpacket_traits<PacketType>::size,
  1305. OffsetRow = ReverseRow && IsColMajor ? PacketSize : 1,
  1306. OffsetCol = ReverseCol && IsRowMajor ? PacketSize : 1
  1307. };
  1308. typedef internal::reverse_packet_cond<PacketType,ReversePacket> reverse_packet;
  1309. return reverse_packet::run(m_argImpl.template packet<LoadMode,PacketType>(
  1310. ReverseRow ? m_rows.value() - row - OffsetRow : row,
  1311. ReverseCol ? m_cols.value() - col - OffsetCol : col));
  1312. }
  1313. template<int LoadMode, typename PacketType>
  1314. EIGEN_STRONG_INLINE
  1315. PacketType packet(Index index) const
  1316. {
  1317. enum { PacketSize = unpacket_traits<PacketType>::size };
  1318. return preverse(m_argImpl.template packet<LoadMode,PacketType>(m_rows.value() * m_cols.value() - index - PacketSize));
  1319. }
  1320. template<int LoadMode, typename PacketType>
  1321. EIGEN_STRONG_INLINE
  1322. void writePacket(Index row, Index col, const PacketType& x)
  1323. {
  1324. // FIXME we could factorize some code with packet(i,j)
  1325. enum {
  1326. PacketSize = unpacket_traits<PacketType>::size,
  1327. OffsetRow = ReverseRow && IsColMajor ? PacketSize : 1,
  1328. OffsetCol = ReverseCol && IsRowMajor ? PacketSize : 1
  1329. };
  1330. typedef internal::reverse_packet_cond<PacketType,ReversePacket> reverse_packet;
  1331. m_argImpl.template writePacket<LoadMode>(
  1332. ReverseRow ? m_rows.value() - row - OffsetRow : row,
  1333. ReverseCol ? m_cols.value() - col - OffsetCol : col,
  1334. reverse_packet::run(x));
  1335. }
  1336. template<int LoadMode, typename PacketType>
  1337. EIGEN_STRONG_INLINE
  1338. void writePacket(Index index, const PacketType& x)
  1339. {
  1340. enum { PacketSize = unpacket_traits<PacketType>::size };
  1341. m_argImpl.template writePacket<LoadMode>
  1342. (m_rows.value() * m_cols.value() - index - PacketSize, preverse(x));
  1343. }
  1344. protected:
  1345. evaluator<ArgType> m_argImpl;
  1346. // If we do not reverse rows, then we do not need to know the number of rows; same for columns
  1347. // Nonetheless, in this case it is important to set to 1 such that the coeff(index) method works fine for vectors.
  1348. const variable_if_dynamic<Index, ReverseRow ? ArgType::RowsAtCompileTime : 1> m_rows;
  1349. const variable_if_dynamic<Index, ReverseCol ? ArgType::ColsAtCompileTime : 1> m_cols;
  1350. };
  1351. // -------------------- Diagonal --------------------
  1352. template<typename ArgType, int DiagIndex>
  1353. struct evaluator<Diagonal<ArgType, DiagIndex> >
  1354. : evaluator_base<Diagonal<ArgType, DiagIndex> >
  1355. {
  1356. typedef Diagonal<ArgType, DiagIndex> XprType;
  1357. enum {
  1358. CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
  1359. Flags = (unsigned int)(evaluator<ArgType>::Flags & (HereditaryBits | DirectAccessBit) & ~RowMajorBit) | LinearAccessBit,
  1360. Alignment = 0
  1361. };
  1362. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1363. explicit evaluator(const XprType& diagonal)
  1364. : m_argImpl(diagonal.nestedExpression()),
  1365. m_index(diagonal.index())
  1366. { }
  1367. typedef typename XprType::Scalar Scalar;
  1368. typedef typename XprType::CoeffReturnType CoeffReturnType;
  1369. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1370. CoeffReturnType coeff(Index row, Index) const
  1371. {
  1372. return m_argImpl.coeff(row + rowOffset(), row + colOffset());
  1373. }
  1374. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1375. CoeffReturnType coeff(Index index) const
  1376. {
  1377. return m_argImpl.coeff(index + rowOffset(), index + colOffset());
  1378. }
  1379. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1380. Scalar& coeffRef(Index row, Index)
  1381. {
  1382. return m_argImpl.coeffRef(row + rowOffset(), row + colOffset());
  1383. }
  1384. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  1385. Scalar& coeffRef(Index index)
  1386. {
  1387. return m_argImpl.coeffRef(index + rowOffset(), index + colOffset());
  1388. }
  1389. protected:
  1390. evaluator<ArgType> m_argImpl;
  1391. const internal::variable_if_dynamicindex<Index, XprType::DiagIndex> m_index;
  1392. private:
  1393. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
  1394. Index rowOffset() const { return m_index.value() > 0 ? 0 : -m_index.value(); }
  1395. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
  1396. Index colOffset() const { return m_index.value() > 0 ? m_index.value() : 0; }
  1397. };
  1398. //----------------------------------------------------------------------
  1399. // deprecated code
  1400. //----------------------------------------------------------------------
  1401. // -------------------- EvalToTemp --------------------
  1402. // expression class for evaluating nested expression to a temporary
  1403. template<typename ArgType> class EvalToTemp;
  1404. template<typename ArgType>
  1405. struct traits<EvalToTemp<ArgType> >
  1406. : public traits<ArgType>
  1407. { };
  1408. template<typename ArgType>
  1409. class EvalToTemp
  1410. : public dense_xpr_base<EvalToTemp<ArgType> >::type
  1411. {
  1412. public:
  1413. typedef typename dense_xpr_base<EvalToTemp>::type Base;
  1414. EIGEN_GENERIC_PUBLIC_INTERFACE(EvalToTemp)
  1415. explicit EvalToTemp(const ArgType& arg)
  1416. : m_arg(arg)
  1417. { }
  1418. const ArgType& arg() const
  1419. {
  1420. return m_arg;
  1421. }
  1422. EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
  1423. {
  1424. return m_arg.rows();
  1425. }
  1426. EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
  1427. {
  1428. return m_arg.cols();
  1429. }
  1430. private:
  1431. const ArgType& m_arg;
  1432. };
  1433. template<typename ArgType>
  1434. struct evaluator<EvalToTemp<ArgType> >
  1435. : public evaluator<typename ArgType::PlainObject>
  1436. {
  1437. typedef EvalToTemp<ArgType> XprType;
  1438. typedef typename ArgType::PlainObject PlainObject;
  1439. typedef evaluator<PlainObject> Base;
  1440. EIGEN_DEVICE_FUNC explicit evaluator(const XprType& xpr)
  1441. : m_result(xpr.arg())
  1442. {
  1443. ::new (static_cast<Base*>(this)) Base(m_result);
  1444. }
  1445. // This constructor is used when nesting an EvalTo evaluator in another evaluator
  1446. EIGEN_DEVICE_FUNC evaluator(const ArgType& arg)
  1447. : m_result(arg)
  1448. {
  1449. ::new (static_cast<Base*>(this)) Base(m_result);
  1450. }
  1451. protected:
  1452. PlainObject m_result;
  1453. };
  1454. } // namespace internal
  1455. } // end namespace Eigen
  1456. #endif // EIGEN_COREEVALUATORS_H