Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:13:27

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008-2016 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_NULLARY_FUNCTORS_H
0011 #define EIGEN_NULLARY_FUNCTORS_H
0012 
0013 namespace Eigen {
0014 
0015 namespace internal {
0016 
0017 template<typename Scalar>
0018 struct scalar_constant_op {
0019   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const scalar_constant_op& other) : m_other(other.m_other) { }
0020   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const Scalar& other) : m_other(other) { }
0021   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() () const { return m_other; }
0022   template<typename PacketType>
0023   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packetOp() const { return internal::pset1<PacketType>(m_other); }
0024   const Scalar m_other;
0025 };
0026 template<typename Scalar>
0027 struct functor_traits<scalar_constant_op<Scalar> >
0028 { enum { Cost = 0 /* as the constant value should be loaded in register only once for the whole expression */,
0029          PacketAccess = packet_traits<Scalar>::Vectorizable, IsRepeatable = true }; };
0030 
0031 template<typename Scalar> struct scalar_identity_op {
0032   EIGEN_EMPTY_STRUCT_CTOR(scalar_identity_op)
0033   template<typename IndexType>
0034   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType row, IndexType col) const { return row==col ? Scalar(1) : Scalar(0); }
0035 };
0036 template<typename Scalar>
0037 struct functor_traits<scalar_identity_op<Scalar> >
0038 { enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = false, IsRepeatable = true }; };
0039 
0040 template <typename Scalar, bool IsInteger> struct linspaced_op_impl;
0041 
0042 template <typename Scalar>
0043 struct linspaced_op_impl<Scalar,/*IsInteger*/false>
0044 {
0045   typedef typename NumTraits<Scalar>::Real RealScalar;
0046 
0047   EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
0048     m_low(low), m_high(high), m_size1(num_steps==1 ? 1 : num_steps-1), m_step(num_steps==1 ? Scalar() : Scalar((high-low)/RealScalar(num_steps-1))),
0049     m_flip(numext::abs(high)<numext::abs(low))
0050   {}
0051 
0052   template<typename IndexType>
0053   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType i) const {
0054     if(m_flip)
0055       return (i==0)? m_low : Scalar(m_high - RealScalar(m_size1-i)*m_step);
0056     else
0057       return (i==m_size1)? m_high : Scalar(m_low + RealScalar(i)*m_step);
0058   }
0059 
0060   template<typename Packet, typename IndexType>
0061   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const
0062   {
0063     // Principle:
0064     // [low, ..., low] + ( [step, ..., step] * ( [i, ..., i] + [0, ..., size] ) )
0065     if(m_flip)
0066     {
0067       Packet pi = plset<Packet>(Scalar(i-m_size1));
0068       Packet res = padd(pset1<Packet>(m_high), pmul(pset1<Packet>(m_step), pi));
0069       if (EIGEN_PREDICT_TRUE(i != 0)) return res;
0070       Packet mask = pcmp_lt(pset1<Packet>(0), plset<Packet>(0));
0071       return pselect<Packet>(mask, res, pset1<Packet>(m_low));
0072     }
0073     else
0074     {
0075       Packet pi = plset<Packet>(Scalar(i));
0076       Packet res = padd(pset1<Packet>(m_low), pmul(pset1<Packet>(m_step), pi));
0077       if(EIGEN_PREDICT_TRUE(i != m_size1-unpacket_traits<Packet>::size+1)) return res;
0078       Packet mask = pcmp_lt(plset<Packet>(0), pset1<Packet>(unpacket_traits<Packet>::size-1));
0079       return pselect<Packet>(mask, res, pset1<Packet>(m_high));
0080     }
0081   }
0082 
0083   const Scalar m_low;
0084   const Scalar m_high;
0085   const Index m_size1;
0086   const Scalar m_step;
0087   const bool m_flip;
0088 };
0089 
0090 template <typename Scalar>
0091 struct linspaced_op_impl<Scalar,/*IsInteger*/true>
0092 {
0093   EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
0094     m_low(low),
0095     m_multiplier((high-low)/convert_index<Scalar>(num_steps<=1 ? 1 : num_steps-1)),
0096     m_divisor(convert_index<Scalar>((high>=low?num_steps:-num_steps)+(high-low))/((numext::abs(high-low)+1)==0?1:(numext::abs(high-low)+1))),
0097     m_use_divisor(num_steps>1 && (numext::abs(high-low)+1)<num_steps)
0098   {}
0099 
0100   template<typename IndexType>
0101   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0102   const Scalar operator() (IndexType i) const
0103   {
0104     if(m_use_divisor) return m_low + convert_index<Scalar>(i)/m_divisor;
0105     else              return m_low + convert_index<Scalar>(i)*m_multiplier;
0106   }
0107 
0108   const Scalar m_low;
0109   const Scalar m_multiplier;
0110   const Scalar m_divisor;
0111   const bool m_use_divisor;
0112 };
0113 
0114 // ----- Linspace functor ----------------------------------------------------------------
0115 
0116 // Forward declaration (we default to random access which does not really give
0117 // us a speed gain when using packet access but it allows to use the functor in
0118 // nested expressions).
0119 template <typename Scalar> struct linspaced_op;
0120 template <typename Scalar> struct functor_traits< linspaced_op<Scalar> >
0121 {
0122   enum
0123   {
0124     Cost = 1,
0125     PacketAccess =   (!NumTraits<Scalar>::IsInteger) && packet_traits<Scalar>::HasSetLinear && packet_traits<Scalar>::HasBlend,
0126                   /*&& ((!NumTraits<Scalar>::IsInteger) || packet_traits<Scalar>::HasDiv),*/ // <- vectorization for integer is currently disabled
0127     IsRepeatable = true
0128   };
0129 };
0130 template <typename Scalar> struct linspaced_op
0131 {
0132   EIGEN_DEVICE_FUNC linspaced_op(const Scalar& low, const Scalar& high, Index num_steps)
0133     : impl((num_steps==1 ? high : low),high,num_steps)
0134   {}
0135 
0136   template<typename IndexType>
0137   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType i) const { return impl(i); }
0138 
0139   template<typename Packet,typename IndexType>
0140   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const { return impl.template packetOp<Packet>(i); }
0141 
0142   // This proxy object handles the actual required temporaries and the different
0143   // implementations (integer vs. floating point).
0144   const linspaced_op_impl<Scalar,NumTraits<Scalar>::IsInteger> impl;
0145 };
0146 
0147 // Linear access is automatically determined from the operator() prototypes available for the given functor.
0148 // If it exposes an operator()(i,j), then we assume the i and j coefficients are required independently
0149 // and linear access is not possible. In all other cases, linear access is enabled.
0150 // Users should not have to deal with this structure.
0151 template<typename Functor> struct functor_has_linear_access { enum { ret = !has_binary_operator<Functor>::value }; };
0152 
0153 // For unreliable compilers, let's specialize the has_*ary_operator
0154 // helpers so that at least built-in nullary functors work fine.
0155 #if !( (EIGEN_COMP_MSVC>1600) || (EIGEN_GNUC_AT_LEAST(4,8)) || (EIGEN_COMP_ICC>=1600))
0156 template<typename Scalar,typename IndexType>
0157 struct has_nullary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 1}; };
0158 template<typename Scalar,typename IndexType>
0159 struct has_unary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 0}; };
0160 template<typename Scalar,typename IndexType>
0161 struct has_binary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 0}; };
0162 
0163 template<typename Scalar,typename IndexType>
0164 struct has_nullary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 0}; };
0165 template<typename Scalar,typename IndexType>
0166 struct has_unary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 0}; };
0167 template<typename Scalar,typename IndexType>
0168 struct has_binary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 1}; };
0169 
0170 template<typename Scalar,typename IndexType>
0171 struct has_nullary_operator<linspaced_op<Scalar>,IndexType> { enum { value = 0}; };
0172 template<typename Scalar,typename IndexType>
0173 struct has_unary_operator<linspaced_op<Scalar>,IndexType> { enum { value = 1}; };
0174 template<typename Scalar,typename IndexType>
0175 struct has_binary_operator<linspaced_op<Scalar>,IndexType> { enum { value = 0}; };
0176 
0177 template<typename Scalar,typename IndexType>
0178 struct has_nullary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 1}; };
0179 template<typename Scalar,typename IndexType>
0180 struct has_unary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 0}; };
0181 template<typename Scalar,typename IndexType>
0182 struct has_binary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 0}; };
0183 #endif
0184 
0185 } // end namespace internal
0186 
0187 } // end namespace Eigen
0188 
0189 #endif // EIGEN_NULLARY_FUNCTORS_H