diff --git a/src/tools/LoopUnroller.h b/src/tools/LoopUnroller.h index 6d16761da0..b53e5151ee 100644 --- a/src/tools/LoopUnroller.h +++ b/src/tools/LoopUnroller.h @@ -47,114 +47,89 @@ Implementation is made using template metaprogramming, that is: Here xxx is any of the methods of the class. */ -template +//the typename is the second parameter argument so that it can be deduced +template class LoopUnroller { public: /// Set to zero. /// Same as `for(unsigned i=0;i -void LoopUnroller::_zero(double*d) { - LoopUnroller::_zero(d); - d[n-1]=0.0; +template +void LoopUnroller::_zero(T*d) { + if constexpr (n>1) { + LoopUnroller::_zero(d); + } + d[n-1]=T(0); } -template<> -inline -void LoopUnroller<1>::_zero(double*d) { - d[0]=0.0; -} - -template -void LoopUnroller::_add(double*d,const double*a) { - LoopUnroller::_add(d,a); +template +void LoopUnroller::_add(T*d,const T*a) { + if constexpr (n>1) { + LoopUnroller::_add(d,a); + } d[n-1]+=a[n-1]; } -template<> -inline -void LoopUnroller<1>::_add(double*d,const double*a) { - d[0]+=a[0]; -} - -template -void LoopUnroller::_sub(double*d,const double*a) { - LoopUnroller::_sub(d,a); +template +void LoopUnroller::_sub(T*d,const T*a) { + if constexpr (n>1) { + LoopUnroller::_sub(d,a); + } d[n-1]-=a[n-1]; } -template<> -inline -void LoopUnroller<1>::_sub(double*d,const double*a) { - d[0]-=a[0]; -} - -template -void LoopUnroller::_mul(double*d,const double s) { - LoopUnroller::_mul(d,s); +template +void LoopUnroller::_mul(T*d,const T s) { + if constexpr (n>1) { + LoopUnroller::_mul(d,s); + } d[n-1]*=s; } -template<> -inline -void LoopUnroller<1>::_mul(double*d,const double s) { - d[0]*=s; -} - -template -void LoopUnroller::_neg(double*d,const double*a ) { - LoopUnroller::_neg(d,a); +template +void LoopUnroller::_neg(T*d,const T*a ) { + if constexpr (n>1) { + LoopUnroller::_neg(d,a); + } d[n-1]=-a[n-1]; } -template<> -inline -void LoopUnroller<1>::_neg(double*d,const double*a) { - d[0]=-a[0]; +template +T LoopUnroller::_sum2(const T*d) { + if constexpr (n>1) { + return LoopUnroller::_sum2(d)+d[n-1]*d[n-1]; + } else { + return d[0]*d[0]; + } } - -template -double LoopUnroller::_sum2(const double*d) { - return LoopUnroller::_sum2(d)+d[n-1]*d[n-1]; -} - -template<> -inline -double LoopUnroller<1>::_sum2(const double*d) { - return d[0]*d[0]; -} - -template -double LoopUnroller::_dot(const double*d,const double*v) { - return LoopUnroller::_dot(d,v)+d[n-1]*v[n-1]; -} - -template<> -inline -double LoopUnroller<1>::_dot(const double*d,const double*v) { - return d[0]*v[0]; -} - +template +T LoopUnroller::_dot(const T*d,const T*v) { + if constexpr (n>1) { + return LoopUnroller::_dot(d,v)+d[n-1]*v[n-1]; + } else { + return d[0]*v[0]; + } } +} //PLMD -#endif +#endif //__PLUMED_tools_LoopUnroller_h diff --git a/src/tools/Tensor.h b/src/tools/Tensor.h index a4f53c8a9b..cac5460af7 100644 --- a/src/tools/Tensor.h +++ b/src/tools/Tensor.h @@ -228,7 +228,7 @@ TensorGeneric::TensorGeneric(double first,Args... arg) template TensorGeneric::TensorGeneric() { - LoopUnroller::_zero(d.data()); + LoopUnroller::_zero(d.data()); } template @@ -238,7 +238,7 @@ TensorGeneric::TensorGeneric(const VectorGeneric&v1,const VectorGeneric< template void TensorGeneric::zero() { - LoopUnroller::_zero(d.data()); + LoopUnroller::_zero(d.data()); } template @@ -261,25 +261,25 @@ const double & TensorGeneric::operator() (unsigned i,unsigned j)const { template TensorGeneric& TensorGeneric::operator +=(const TensorGeneric& b) { - LoopUnroller::_add(d.data(),b.d.data()); + LoopUnroller::_add(d.data(),b.d.data()); return *this; } template TensorGeneric& TensorGeneric::operator -=(const TensorGeneric& b) { - LoopUnroller::_sub(d.data(),b.d.data()); + LoopUnroller::_sub(d.data(),b.d.data()); return *this; } template TensorGeneric& TensorGeneric::operator *=(double s) { - LoopUnroller::_mul(d.data(),s); + LoopUnroller::_mul(d.data(),s); return *this; } template TensorGeneric& TensorGeneric::operator /=(double s) { - LoopUnroller::_mul(d.data(),1.0/s); + LoopUnroller::_mul(d.data(),1.0/s); return *this; } @@ -291,7 +291,7 @@ TensorGeneric TensorGeneric::operator+()const { template TensorGeneric TensorGeneric::operator-()const { TensorGeneric r; - LoopUnroller::_neg(r.d.data(),d.data()); + LoopUnroller::_neg(r.d.data(),d.data()); return r; } diff --git a/src/tools/Vector.h b/src/tools/Vector.h index cbebc64b21..0911a4ec30 100644 --- a/src/tools/Vector.h +++ b/src/tools/Vector.h @@ -176,12 +176,12 @@ VectorGeneric::VectorGeneric(double first,Args... arg) template void VectorGeneric::zero() { - LoopUnroller::_zero(d.data()); + LoopUnroller::_zero(d.data()); } template VectorGeneric::VectorGeneric() { - LoopUnroller::_zero(d.data()); + LoopUnroller::_zero(d.data()); } template @@ -206,25 +206,25 @@ const double & VectorGeneric::operator()(unsigned i)const { template VectorGeneric& VectorGeneric::operator +=(const VectorGeneric& b) { - LoopUnroller::_add(d.data(),b.d.data()); + LoopUnroller::_add(d.data(),b.d.data()); return *this; } template VectorGeneric& VectorGeneric::operator -=(const VectorGeneric& b) { - LoopUnroller::_sub(d.data(),b.d.data()); + LoopUnroller::_sub(d.data(),b.d.data()); return *this; } template VectorGeneric& VectorGeneric::operator *=(double s) { - LoopUnroller::_mul(d.data(),s); + LoopUnroller::_mul(d.data(),s); return *this; } template VectorGeneric& VectorGeneric::operator /=(double s) { - LoopUnroller::_mul(d.data(),1.0/s); + LoopUnroller::_mul(d.data(),1.0/s); return *this; } @@ -236,7 +236,7 @@ VectorGeneric VectorGeneric::operator +()const { template VectorGeneric VectorGeneric::operator -()const { VectorGeneric r; - LoopUnroller::_neg(r.d.data(),d.data()); + LoopUnroller::_neg(r.d.data(),d.data()); return r; } @@ -275,12 +275,12 @@ VectorGeneric delta(const VectorGeneric&v1,const VectorGeneric&v2) { template double VectorGeneric::modulo2()const { - return LoopUnroller::_sum2(d.data()); + return LoopUnroller::_sum2(d.data()); } template double dotProduct(const VectorGeneric& v1,const VectorGeneric& v2) { - return LoopUnroller::_dot(v1.d.data(),v2.d.data()); + return LoopUnroller::_dot(v1.d.data(),v2.d.data()); } inline