1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2008-2016 Gael Guennebaud <gael.guennebaud@inria.fr> 5 // 6 // This Source Code Form is subject to the terms of the Mozilla 7 // Public License v. 2.0. If a copy of the MPL was not distributed 8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 10 #ifndef EIGEN_NULLARY_FUNCTORS_H 11 #define EIGEN_NULLARY_FUNCTORS_H 12 13 namespace Eigen { 14 15 namespace internal { 16 17 template<typename Scalar> 18 struct scalar_constant_op { scalar_constant_opscalar_constant_op19 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const scalar_constant_op& other) : m_other(other.m_other) { } scalar_constant_opscalar_constant_op20 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const Scalar& other) : m_other(other) { } operatorscalar_constant_op21 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() () const { return m_other; } 22 template<typename PacketType> packetOpscalar_constant_op23 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packetOp() const { return internal::pset1<PacketType>(m_other); } 24 const Scalar m_other; 25 }; 26 template<typename Scalar> 27 struct functor_traits<scalar_constant_op<Scalar> > 28 { enum { Cost = 0 /* as the constant value should be loaded in register only once for the whole expression */, 29 PacketAccess = packet_traits<Scalar>::Vectorizable, IsRepeatable = true }; }; 30 31 template<typename Scalar> struct scalar_identity_op { 32 EIGEN_EMPTY_STRUCT_CTOR(scalar_identity_op) 33 template<typename IndexType> 34 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType row, IndexType col) const { return row==col ? Scalar(1) : Scalar(0); } 35 }; 36 template<typename Scalar> 37 struct functor_traits<scalar_identity_op<Scalar> > 38 { enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = false, IsRepeatable = true }; }; 39 40 template <typename Scalar, typename Packet, bool IsInteger> struct linspaced_op_impl; 41 42 template <typename Scalar, typename Packet> 43 struct linspaced_op_impl<Scalar,Packet,/*IsInteger*/false> 44 { 45 linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) : 46 m_low(low), m_high(high), m_size1(num_steps==1 ? 1 : num_steps-1), m_step(num_steps==1 ? Scalar() : (high-low)/Scalar(num_steps-1)), 47 m_interPacket(plset<Packet>(0)), 48 m_flip(numext::abs(high)<numext::abs(low)) 49 {} 50 51 template<typename IndexType> 52 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType i) const { 53 if(m_flip) 54 return (i==0)? m_low : (m_high - (m_size1-i)*m_step); 55 else 56 return (i==m_size1)? m_high : (m_low + i*m_step); 57 } 58 59 template<typename IndexType> 60 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const 61 { 62 // Principle: 63 // [low, ..., low] + ( [step, ..., step] * ( [i, ..., i] + [0, ..., size] ) ) 64 if(m_flip) 65 { 66 Packet pi = padd(pset1<Packet>(Scalar(i-m_size1)),m_interPacket); 67 Packet res = padd(pset1<Packet>(m_high), pmul(pset1<Packet>(m_step), pi)); 68 if(i==0) 69 res = pinsertfirst(res, m_low); 70 return res; 71 } 72 else 73 { 74 Packet pi = padd(pset1<Packet>(Scalar(i)),m_interPacket); 75 Packet res = padd(pset1<Packet>(m_low), pmul(pset1<Packet>(m_step), pi)); 76 if(i==m_size1-unpacket_traits<Packet>::size+1) 77 res = pinsertlast(res, m_high); 78 return res; 79 } 80 } 81 82 const Scalar m_low; 83 const Scalar m_high; 84 const Index m_size1; 85 const Scalar m_step; 86 const Packet m_interPacket; 87 const bool m_flip; 88 }; 89 90 template <typename Scalar, typename Packet> 91 struct linspaced_op_impl<Scalar,Packet,/*IsInteger*/true> 92 { 93 linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) : 94 m_low(low), 95 m_multiplier((high-low)/convert_index<Scalar>(num_steps<=1 ? 1 : num_steps-1)), 96 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))), 97 m_use_divisor(num_steps>1 && (numext::abs(high-low)+1)<num_steps) 98 {} 99 100 template<typename IndexType> 101 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 102 const Scalar operator() (IndexType i) const 103 { 104 if(m_use_divisor) return m_low + convert_index<Scalar>(i)/m_divisor; 105 else return m_low + convert_index<Scalar>(i)*m_multiplier; 106 } 107 108 const Scalar m_low; 109 const Scalar m_multiplier; 110 const Scalar m_divisor; 111 const bool m_use_divisor; 112 }; 113 114 // ----- Linspace functor ---------------------------------------------------------------- 115 116 // Forward declaration (we default to random access which does not really give 117 // us a speed gain when using packet access but it allows to use the functor in 118 // nested expressions). 119 template <typename Scalar, typename PacketType> struct linspaced_op; 120 template <typename Scalar, typename PacketType> struct functor_traits< linspaced_op<Scalar,PacketType> > 121 { 122 enum 123 { 124 Cost = 1, 125 PacketAccess = (!NumTraits<Scalar>::IsInteger) && packet_traits<Scalar>::HasSetLinear && packet_traits<Scalar>::HasBlend, 126 /*&& ((!NumTraits<Scalar>::IsInteger) || packet_traits<Scalar>::HasDiv),*/ // <- vectorization for integer is currently disabled 127 IsRepeatable = true 128 }; 129 }; 130 template <typename Scalar, typename PacketType> struct linspaced_op 131 { 132 linspaced_op(const Scalar& low, const Scalar& high, Index num_steps) 133 : impl((num_steps==1 ? high : low),high,num_steps) 134 {} 135 136 template<typename IndexType> 137 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType i) const { return impl(i); } 138 139 template<typename Packet,typename IndexType> 140 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const { return impl.packetOp(i); } 141 142 // This proxy object handles the actual required temporaries and the different 143 // implementations (integer vs. floating point). 144 const linspaced_op_impl<Scalar,PacketType,NumTraits<Scalar>::IsInteger> impl; 145 }; 146 147 // Linear access is automatically determined from the operator() prototypes available for the given functor. 148 // If it exposes an operator()(i,j), then we assume the i and j coefficients are required independently 149 // and linear access is not possible. In all other cases, linear access is enabled. 150 // Users should not have to deal with this structure. 151 template<typename Functor> struct functor_has_linear_access { enum { ret = !has_binary_operator<Functor>::value }; }; 152 153 // For unreliable compilers, let's specialize the has_*ary_operator 154 // helpers so that at least built-in nullary functors work fine. 155 #if !( (EIGEN_COMP_MSVC>1600) || (EIGEN_GNUC_AT_LEAST(4,8)) || (EIGEN_COMP_ICC>=1600)) 156 template<typename Scalar,typename IndexType> 157 struct has_nullary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 1}; }; 158 template<typename Scalar,typename IndexType> 159 struct has_unary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 0}; }; 160 template<typename Scalar,typename IndexType> 161 struct has_binary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 0}; }; 162 163 template<typename Scalar,typename IndexType> 164 struct has_nullary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 0}; }; 165 template<typename Scalar,typename IndexType> 166 struct has_unary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 0}; }; 167 template<typename Scalar,typename IndexType> 168 struct has_binary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 1}; }; 169 170 template<typename Scalar, typename PacketType,typename IndexType> 171 struct has_nullary_operator<linspaced_op<Scalar,PacketType>,IndexType> { enum { value = 0}; }; 172 template<typename Scalar, typename PacketType,typename IndexType> 173 struct has_unary_operator<linspaced_op<Scalar,PacketType>,IndexType> { enum { value = 1}; }; 174 template<typename Scalar, typename PacketType,typename IndexType> 175 struct has_binary_operator<linspaced_op<Scalar,PacketType>,IndexType> { enum { value = 0}; }; 176 177 template<typename Scalar,typename IndexType> 178 struct has_nullary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 1}; }; 179 template<typename Scalar,typename IndexType> 180 struct has_unary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 0}; }; 181 template<typename Scalar,typename IndexType> 182 struct has_binary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 0}; }; 183 #endif 184 185 } // end namespace internal 186 187 } // end namespace Eigen 188 189 #endif // EIGEN_NULLARY_FUNCTORS_H 190