From a0e8577b49693df630b9b35b0fee2c89a9ec12ad Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Fri, 18 Oct 2013 14:56:36 +0200 Subject: Fix bug #684: optimize vectorization of array-scalar and scalar-array --- Eigen/src/Core/Functors.h | 41 +++++++++++++++++++++++++++++++-- Eigen/src/plugins/ArrayCwiseBinaryOps.h | 8 +++---- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Eigen/src/Core/Functors.h b/Eigen/src/Core/Functors.h index 69d95ea30..d0e05fcf2 100644 --- a/Eigen/src/Core/Functors.h +++ b/Eigen/src/Core/Functors.h @@ -22,6 +22,7 @@ namespace internal { * \sa class CwiseBinaryOp, MatrixBase::operator+, class VectorwiseOp, DenseBase::sum() */ template struct scalar_sum_op { +// typedef Scalar result_type; EIGEN_EMPTY_STRUCT_CTOR(scalar_sum_op) EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const Scalar& b) const { return a + b; } template @@ -45,8 +46,8 @@ struct functor_traits > { * \sa DenseBase::count(), DenseBase::any(), ArrayBase::cast(), MatrixBase::cast() */ template<> struct scalar_sum_op : scalar_sum_op { - EIGEN_DEPRECATED - scalar_sum_op() {} + EIGEN_DEPRECATED + scalar_sum_op() {} }; @@ -689,6 +690,42 @@ template struct functor_traits > { enum { Cost = NumTraits::AddCost, PacketAccess = packet_traits::HasAdd }; }; +/** \internal + * \brief Template functor to subtract a fixed scalar to another one + * \sa class CwiseUnaryOp, Array::operator-, struct scalar_add_op, struct scalar_rsub_op + */ +template +struct scalar_sub_op { + typedef typename packet_traits::type Packet; + inline scalar_sub_op(const scalar_sub_op& other) : m_other(other.m_other) { } + inline scalar_sub_op(const Scalar& other) : m_other(other) { } + inline Scalar operator() (const Scalar& a) const { return a - m_other; } + inline const Packet packetOp(const Packet& a) const + { return internal::psub(a, pset1(m_other)); } + const Scalar m_other; +}; +template +struct functor_traits > +{ enum { Cost = NumTraits::AddCost, PacketAccess = packet_traits::HasAdd }; }; + +/** \internal + * \brief Template functor to subtract a scalar to fixed another one + * \sa class CwiseUnaryOp, Array::operator-, struct scalar_add_op, struct scalar_sub_op + */ +template +struct scalar_rsub_op { + typedef typename packet_traits::type Packet; + inline scalar_rsub_op(const scalar_rsub_op& other) : m_other(other.m_other) { } + inline scalar_rsub_op(const Scalar& other) : m_other(other) { } + inline Scalar operator() (const Scalar& a) const { return m_other - a; } + inline const Packet packetOp(const Packet& a) const + { return internal::psub(pset1(m_other), a); } + const Scalar m_other; +}; +template +struct functor_traits > +{ enum { Cost = NumTraits::AddCost, PacketAccess = packet_traits::HasAdd }; }; + /** \internal * \brief Template functor to compute the square root of a scalar * \sa class CwiseUnaryOp, Cwise::sqrt() diff --git a/Eigen/src/plugins/ArrayCwiseBinaryOps.h b/Eigen/src/plugins/ArrayCwiseBinaryOps.h index 5c8c476ee..65d198749 100644 --- a/Eigen/src/plugins/ArrayCwiseBinaryOps.h +++ b/Eigen/src/plugins/ArrayCwiseBinaryOps.h @@ -162,16 +162,16 @@ operator+(const Scalar& scalar,const EIGEN_CURRENT_STORAGE_BASE_CLASS& * * \sa operator+(), operator-=() */ -inline const CwiseUnaryOp, const Derived> +inline const CwiseUnaryOp, const Derived> operator-(const Scalar& scalar) const { - return *this + (-scalar); + return CwiseUnaryOp, const Derived>(derived(), internal::scalar_sub_op(scalar));; } -friend inline const CwiseUnaryOp, const CwiseUnaryOp, const Derived> > +friend inline const CwiseUnaryOp, const Derived> operator-(const Scalar& scalar,const EIGEN_CURRENT_STORAGE_BASE_CLASS& other) { - return (-other) + scalar; + return CwiseUnaryOp, const Derived>(other.derived(), internal::scalar_rsub_op(scalar));; } /** \returns an expression of the coefficient-wise && operator of *this and \a other -- cgit v1.2.3