// This file is part of Eigen, a lightweight C++ template library // for linear algebra. Eigen itself is part of the KDE project. // // Copyright (C) 2007 Michael Olbrich // Copyright (C) 2006-2008 Benoit Jacob // Copyright (C) 2008 Gael Guennebaud // // Eigen is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 3 of the License, or (at your option) any later version. // // Alternatively, you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of // the License, or (at your option) any later version. // // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the // GNU General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License and a copy of the GNU General Public License along with // Eigen. If not, see . #ifndef EIGEN_ASSIGN_H #define EIGEN_ASSIGN_H template struct ei_matrix_assignment_unroller { enum { col = (UnrollCount-1) / Derived1::RowsAtCompileTime, row = (UnrollCount-1) % Derived1::RowsAtCompileTime }; inline static void run(Derived1 &dst, const Derived2 &src) { ei_matrix_assignment_unroller::run(dst, src); dst.coeffRef(row, col) = src.coeff(row, col); } }; template struct ei_matrix_assignment_unroller { inline static void run(Derived1 &dst, const Derived2 &src) { dst.coeffRef(0, 0) = src.coeff(0, 0); } }; // prevent buggy user code from causing an infinite recursion template struct ei_matrix_assignment_unroller { inline static void run(Derived1 &, const Derived2 &) {} }; template struct ei_matrix_assignment_unroller { inline static void run(Derived1 &, const Derived2 &) {} }; //---- template struct ei_matrix_assignment_packet_unroller { enum { row = int(Derived1::Flags)&RowMajorBit ? Index / int(Derived1::ColsAtCompileTime) : Index % Derived1::RowsAtCompileTime, col = int(Derived1::Flags)&RowMajorBit ? Index % int(Derived1::ColsAtCompileTime) : Index / Derived1::RowsAtCompileTime }; inline static void run(Derived1 &dst, const Derived2 &src) { ei_matrix_assignment_packet_unroller::size>::run(dst, src); dst.template writePacketCoeff(row, col, src.template packetCoeff(row, col)); } }; template struct ei_matrix_assignment_packet_unroller { inline static void run(Derived1 &dst, const Derived2 &src) { dst.template writePacketCoeff(0, 0, src.template packetCoeff(0, 0)); } }; template struct ei_matrix_assignment_packet_unroller { inline static void run(Derived1 &, const Derived2 &) { ei_internal_assert(false && "ei_matrix_assignment_packet_unroller"); } }; //---- template ::size==0) : int(Derived::RowsAtCompileTime)!=Dynamic && (int(Derived::RowsAtCompileTime)%ei_packet_traits::size==0)) ), bool Unroll = Derived::SizeAtCompileTime * OtherDerived::CoeffReadCost <= EIGEN_UNROLLING_LIMIT> struct ei_assignment_impl; template template inline Derived& MatrixBase ::lazyAssign(const MatrixBase& other) { ei_assert(rows() == other.rows() && cols() == other.cols()); ei_assignment_impl::run(derived(),other.derived()); return derived(); } template struct ei_assign_selector; template struct ei_assign_selector { static Derived& run(Derived& dst, const OtherDerived& other) { return dst.lazyAssign(other.transpose().eval()); } }; template struct ei_assign_selector { static Derived& run(Derived& dst, const OtherDerived& other) { return dst.lazyAssign(other.eval()); } }; template struct ei_assign_selector { static Derived& run(Derived& dst, const OtherDerived& other) { return dst.lazyAssign(other.transpose()); } }; template struct ei_assign_selector { static Derived& run(Derived& dst, const OtherDerived& other) { return dst.lazyAssign(other.derived()); } }; template template inline Derived& MatrixBase ::operator=(const MatrixBase& other) { return ei_assign_selector::run(derived(), other.derived()); } //---- template struct ei_assignment_impl // no vec + unrolling { static void run(Derived & dst, const OtherDerived & src) { ei_matrix_assignment_unroller ::run(dst.derived(), src.derived()); } }; template struct ei_assignment_impl // no vec + no unrolling + col major order { static void run(Derived & dst, const OtherDerived & src) { if(Derived::ColsAtCompileTime == Dynamic || Derived::RowsAtCompileTime != Dynamic) { for(int j = 0; j < dst.cols(); j++) for(int i = 0; i < dst.rows(); i++) dst.coeffRef(i, j) = src.coeff(i, j); } else { // traverse in row-major order // in order to allow the compiler to unroll the inner loop for(int i = 0; i < dst.rows(); i++) for(int j = 0; j < dst.cols(); j++) dst.coeffRef(i, j) = src.coeff(i, j); } } }; //---- template struct ei_assignment_impl // vec + unrolling { static void run(Derived & dst, const OtherDerived & src) { ei_matrix_assignment_packet_unroller ::size) >::run(dst.const_cast_derived(), src.derived()); } }; template ::size!=0) ) : ( (Derived::Flags & OtherDerived::Flags & Like1DArrayBit) && ( Derived::RowsAtCompileTime==Dynamic || Derived::RowsAtCompileTime%ei_packet_traits::size!=0))> struct ei_packet_assignment_seclector; template struct ei_assignment_impl // vec + no-unrolling { static void run(Derived & dst, const OtherDerived & src) { ei_packet_assignment_seclector::run(dst,src); } }; template struct ei_packet_assignment_seclector // row-major + complex 1D array { static void run(Derived & dst, const OtherDerived & src) { const int size = dst.rows() * dst.cols(); const int alignedSize = (size/ei_packet_traits::size) * ei_packet_traits::size; int index = 0; for ( ; index::size) { // FIXME the following is not really efficient int i = index/dst.cols(); int j = index%dst.cols(); dst.template writePacketCoeff(i, j, src.template packetCoeff(i, j)); } for(int i = alignedSize/dst.cols(); i < dst.rows(); i++) for(int j = alignedSize%dst.cols(); j < dst.cols(); j++) dst.coeffRef(i, j) = src.coeff(i, j); } }; template struct ei_packet_assignment_seclector // row-major + normal { static void run(Derived & dst, const OtherDerived & src) { for(int i = 0; i < dst.rows(); i++) for(int j = 0; j < dst.cols(); j+=ei_packet_traits::size) dst.template writePacketCoeff(i, j, src.template packetCoeff(i, j)); } }; template struct ei_packet_assignment_seclector // col-major + complex 1D array like { static void run(Derived & dst, const OtherDerived & src) { const int size = dst.rows() * dst.cols(); const int alignedSize = (size/ei_packet_traits::size)*ei_packet_traits::size; int index = 0; for ( ; index::size) { // FIXME the following is not really efficient int i = index%dst.rows(); int j = index/dst.rows(); dst.template writePacketCoeff(i, j, src.template packetCoeff(i, j)); } for(int j = alignedSize/dst.rows(); j < dst.cols(); j++) for(int i = alignedSize%dst.rows(); i < dst.rows(); i++) dst.coeffRef(i, j) = src.coeff(i, j); } }; template struct ei_packet_assignment_seclector // col-major + normal { static void run(Derived & dst, const OtherDerived & src) { for(int j = 0; j < dst.cols(); j++) for(int i = 0; i < dst.rows(); i+=ei_packet_traits::size) dst.template writePacketCoeff(i, j, src.template packetCoeff(i, j)); } }; #endif // EIGEN_ASSIGN_H