From f5bcca6cdf39c2aa184c8b52fa33cf145becf2f9 Mon Sep 17 00:00:00 2001 From: Azusa Yamaguchi Date: Sun, 14 Jun 2015 01:07:25 +0100 Subject: [PATCH] Where and many other functions (sin cos abs log exp) into ET system --- lib/lattice/Lattice_ET.h | 118 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/lib/lattice/Lattice_ET.h b/lib/lattice/Lattice_ET.h index 2a7172ea..caa45e69 100644 --- a/lib/lattice/Lattice_ET.h +++ b/lib/lattice/Lattice_ET.h @@ -9,6 +9,37 @@ namespace Grid { + //////////////////////////////////////////////////// + // Predicated where support + //////////////////////////////////////////////////// + template + inline vobj predicatedWhere(const iobj &predicate,const vobj &iftrue,const robj &iffalse) { + + typename std::remove_const::type ret; + + typedef typename vobj::scalar_object scalar_object; + typedef typename vobj::scalar_type scalar_type; + typedef typename vobj::vector_type vector_type; + + const int Nsimd = vobj::vector_type::Nsimd(); + const int words = sizeof(vobj)/sizeof(vector_type); + + std::vector mask(Nsimd); + std::vector truevals (Nsimd); + std::vector falsevals(Nsimd); + + extract(iftrue ,truevals); + extract(iffalse ,falsevals); + extract(TensorRemove(predicate),mask); + + for(int s=0;s struct name\ }; GridUnopClass(UnarySub,-a); +GridUnopClass(UnaryNot,Not(a)); GridUnopClass(UnaryAdj,adj(a)); GridUnopClass(UnaryConj,conjugate(a)); GridUnopClass(UnaryTrace,trace(a)); GridUnopClass(UnaryTranspose,transpose(a)); +GridUnopClass(UnaryTa,Ta(a)); +GridUnopClass(UnaryReal,real(a)); +GridUnopClass(UnaryImag,imag(a)); +GridUnopClass(UnaryToReal,toReal(a)); +GridUnopClass(UnaryToComplex,toComplex(a)); +GridUnopClass(UnaryAbs,abs(a)); +GridUnopClass(UnarySqrt,sqrt(a)); +GridUnopClass(UnaryRsqrt,rsqrt(a)); +GridUnopClass(UnarySin,sin(a)); +GridUnopClass(UnaryCos,cos(a)); +GridUnopClass(UnaryLog,log(a)); +GridUnopClass(UnaryExp,exp(a)); //////////////////////////////////////////// // Binary operators @@ -163,6 +207,28 @@ GridBinOpClass(BinaryAdd,lhs+rhs); GridBinOpClass(BinarySub,lhs-rhs); GridBinOpClass(BinaryMul,lhs*rhs); +GridBinOpClass(BinaryAnd ,lhs&rhs); +GridBinOpClass(BinaryOr ,lhs|rhs); +GridBinOpClass(BinaryAndAnd,lhs&&rhs); +GridBinOpClass(BinaryOrOr ,lhs||rhs); + +//////////////////////////////////////////////////// +// Trinary conditional op +//////////////////////////////////////////////////// +#define GridTrinOpClass(name,combination)\ +template \ +struct name\ +{\ + static auto inline func(const predicate &pred,const left &lhs,const right &rhs)-> decltype(combination) const \ + {\ + return combination;\ + }\ +} + +GridTrinOpClass(TrinaryWhere,(predicatedWhere::type, \ + typename std::remove_reference::type> (pred,lhs,rhs))); + //////////////////////////////////////////// // Operator syntactical glue //////////////////////////////////////////// @@ -218,15 +284,67 @@ template inline auto op(const T1 &pred,con //////////////////////// GRID_DEF_UNOP(operator -,UnarySub); +GRID_DEF_UNOP(Not,UnaryNot); +GRID_DEF_UNOP(operator !,UnaryNot); GRID_DEF_UNOP(adj,UnaryAdj); GRID_DEF_UNOP(conjugate,UnaryConj); GRID_DEF_UNOP(trace,UnaryTrace); GRID_DEF_UNOP(transpose,UnaryTranspose); +GRID_DEF_UNOP(Ta,UnaryTa); +GRID_DEF_UNOP(real,UnaryReal); +GRID_DEF_UNOP(imag,UnaryImag); +GRID_DEF_UNOP(toReal,UnaryToReal); +GRID_DEF_UNOP(toComplex,UnaryToComplex); +GRID_DEF_UNOP(abs ,UnaryAbs); //abs overloaded in cmath C++98; DON'T do the abs-fabs-dabs-labs thing +GRID_DEF_UNOP(sqrt ,UnarySqrt); +GRID_DEF_UNOP(rsqrt,UnarySqrt); +GRID_DEF_UNOP(sin ,UnarySin); +GRID_DEF_UNOP(cos ,UnaryCos); +GRID_DEF_UNOP(log ,UnaryLog); +GRID_DEF_UNOP(exp ,UnaryExp); GRID_DEF_BINOP(operator+,BinaryAdd); GRID_DEF_BINOP(operator-,BinarySub); GRID_DEF_BINOP(operator*,BinaryMul); +GRID_DEF_BINOP(operator&,BinaryAnd); +GRID_DEF_BINOP(operator|,BinaryOr); +GRID_DEF_BINOP(operator&&,BinaryAndAnd); +GRID_DEF_BINOP(operator||,BinaryOrOr); + +GRID_DEF_TRINOP(where,TrinaryWhere); + +///////////////////////////////////////////////////////////// +// Closure convenience to force expression to evaluate +///////////////////////////////////////////////////////////// +template + auto closure(const LatticeUnaryExpression & expr) + -> Lattice(expr.second))))> +{ + Lattice(expr.second))))> ret(expr); + return ret; +} +template + auto closure(const LatticeBinaryExpression & expr) + -> Lattice(expr.second)), + eval(0,std::get<1>(expr.second))))> +{ + Lattice(expr.second)), + eval(0,std::get<1>(expr.second))))> ret(expr); + return ret; +} +template + auto closure(const LatticeTrinaryExpression & expr) + -> Lattice(expr.second)), + eval(0,std::get<1>(expr.second)), + eval(0,std::get<2>(expr.second))))> +{ + Lattice(expr.second)), + eval(0,std::get<1>(expr.second)), + eval(0,std::get<2>(expr.second))))> ret(expr); + return ret; +} + #undef GRID_UNOP #undef GRID_BINOP #undef GRID_TRINOP