1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-27 14:15:55 +01:00
This commit is contained in:
paboyle 2018-01-12 18:25:39 +00:00
parent ec89714cce
commit 00c49d4c17

View File

@ -25,14 +25,14 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
See the full license in the file "LICENSE" in the top level distribution directory See the full license in the file "LICENSE" in the top level distribution directory
******************************************************************************/ ******************************************************************************/
#ifndef GEN_SIMD_WIDTH #ifndef GEN_SIMD_WIDTH
#define GEN_SIMD_WIDTH 32u #define GEN_SIMD_WIDTH 32u
#endif #endif
#include "Grid_generic_types.h" // Definitions for simulated integer SIMD. #include "Grid_generic_types.h" // Definitions for simulated integer SIMD.
namespace Grid { NAMESPACE_BEGIN(Grid);
#ifdef QPX #ifdef QPX
#include <spi/include/kernel/location.h> #include <spi/include/kernel/location.h>
@ -41,25 +41,26 @@ namespace Grid {
#include <hwi/include/bqc/A2_inlines.h> #include <hwi/include/bqc/A2_inlines.h>
#endif #endif
namespace Optimization { NAMESPACE_BEGIN(Optimization);
typedef struct
{
float v0,v1,v2,v3;
} vector4float;
inline std::ostream & operator<<(std::ostream& stream, const vector4double a) typedef struct
{ {
float v0,v1,v2,v3;
} vector4float;
inline std::ostream & operator<<(std::ostream& stream, const vector4double a)
{
stream << "{"<<vec_extract(a,0)<<","<<vec_extract(a,1)<<","<<vec_extract(a,2)<<","<<vec_extract(a,3)<<"}"; stream << "{"<<vec_extract(a,0)<<","<<vec_extract(a,1)<<","<<vec_extract(a,2)<<","<<vec_extract(a,3)<<"}";
return stream; return stream;
}; };
inline std::ostream & operator<<(std::ostream& stream, const vector4float a) inline std::ostream & operator<<(std::ostream& stream, const vector4float a)
{ {
stream << "{"<< a.v0 <<","<< a.v1 <<","<< a.v2 <<","<< a.v3 <<"}"; stream << "{"<< a.v0 <<","<< a.v1 <<","<< a.v2 <<","<< a.v3 <<"}";
return stream; return stream;
}; };
struct Vsplat{ struct Vsplat{
//Complex float //Complex float
inline vector4float operator()(float a, float b){ inline vector4float operator()(float a, float b){
return (vector4float){a, b, a, b}; return (vector4float){a, b, a, b};
@ -87,9 +88,9 @@ namespace Optimization {
return out; return out;
} }
}; };
struct Vstore{ struct Vstore{
//Float //Float
inline void operator()(vector4double a, float *f){ inline void operator()(vector4double a, float *f){
vec_st(a, 0, f); vec_st(a, 0, f);
@ -115,9 +116,9 @@ namespace Optimization {
inline void operator()(veci a, Integer *i){ inline void operator()(veci a, Integer *i){
*((veci *)i) = a; *((veci *)i) = a;
} }
}; };
struct Vstream{ struct Vstream{
//Float //Float
inline void operator()(float *f, vector4double a){ inline void operator()(float *f, vector4double a){
vec_st(a, 0, f); vec_st(a, 0, f);
@ -138,9 +139,9 @@ namespace Optimization {
vec_st(a, 0, d); vec_st(a, 0, d);
} }
}; };
struct Vset{ struct Vset{
// Complex float // Complex float
inline vector4float operator()(Grid::ComplexF *a){ inline vector4float operator()(Grid::ComplexF *a){
return (vector4float){a[0].real(), a[0].imag(), a[1].real(), a[1].imag()}; return (vector4float){a[0].real(), a[0].imag(), a[1].real(), a[1].imag()};
@ -171,9 +172,9 @@ namespace Optimization {
return out; return out;
} }
}; };
template <typename Out_type, typename In_type> template <typename Out_type, typename In_type>
struct Reduce{ struct Reduce{
//Need templated class to overload output type //Need templated class to overload output type
//General form must generate error if compiled //General form must generate error if compiled
@ -184,53 +185,53 @@ namespace Optimization {
} }
}; };
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
// Arithmetic operations // Arithmetic operations
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
#define FLOAT_WRAP_3(fn, pref)\ #define FLOAT_WRAP_3(fn, pref) \
pref vector4float fn(vector4float a, vector4float b, vector4float c) \ pref vector4float fn(vector4float a, vector4float b, vector4float c) \
{\ { \
vector4double ad, bd, rd, cd; \ vector4double ad, bd, rd, cd; \
vector4float r;\ vector4float r; \
\ \
ad = Vset()(a);\ ad = Vset()(a); \
bd = Vset()(b);\ bd = Vset()(b); \
cd = Vset()(c);\ cd = Vset()(c); \
rd = fn(ad, bd, cd); \ rd = fn(ad, bd, cd); \
Vstore()(rd, r);\ Vstore()(rd, r); \
\ \
return r;\ return r; \
} }
#define FLOAT_WRAP_2(fn, pref)\ #define FLOAT_WRAP_2(fn, pref) \
pref vector4float fn(vector4float a, vector4float b)\ pref vector4float fn(vector4float a, vector4float b) \
{\ { \
vector4double ad, bd, rd;\ vector4double ad, bd, rd; \
vector4float r;\ vector4float r; \
\ \
ad = Vset()(a);\ ad = Vset()(a); \
bd = Vset()(b);\ bd = Vset()(b); \
rd = fn(ad, bd);\ rd = fn(ad, bd); \
Vstore()(rd, r);\ Vstore()(rd, r); \
\ \
return r;\ return r; \
} }
#define FLOAT_WRAP_1(fn, pref)\ #define FLOAT_WRAP_1(fn, pref) \
pref vector4float fn(vector4float a)\ pref vector4float fn(vector4float a) \
{\ { \
vector4double ad, rd;\ vector4double ad, rd; \
vector4float r;\ vector4float r; \
\ \
ad = Vset()(a);\ ad = Vset()(a); \
rd = fn(ad);\ rd = fn(ad); \
Vstore()(rd, r);\ Vstore()(rd, r); \
\ \
return r;\ return r; \
} }
struct Sum{ struct Sum{
//Complex/Real double //Complex/Real double
inline vector4double operator()(vector4double a, vector4double b){ inline vector4double operator()(vector4double a, vector4double b){
return vec_add(a, b); return vec_add(a, b);
@ -250,9 +251,9 @@ namespace Optimization {
return out; return out;
} }
}; };
struct Sub{ struct Sub{
//Complex/Real double //Complex/Real double
inline vector4double operator()(vector4double a, vector4double b){ inline vector4double operator()(vector4double a, vector4double b){
return vec_sub(a, b); return vec_sub(a, b);
@ -272,24 +273,24 @@ namespace Optimization {
return out; return out;
} }
}; };
struct MultRealPart{ struct MultRealPart{
// Complex double // Complex double
inline vector4double operator()(vector4double a, vector4double b){ inline vector4double operator()(vector4double a, vector4double b){
// return vec_xmul(b, a); // return vec_xmul(b, a);
return vec_xmul(a, b); return vec_xmul(a, b);
} }
FLOAT_WRAP_2(operator(), inline) FLOAT_WRAP_2(operator(), inline)
}; };
struct MaddRealPart{ struct MaddRealPart{
// Complex double // Complex double
inline vector4double operator()(vector4double a, vector4double b,vector4double c){ inline vector4double operator()(vector4double a, vector4double b,vector4double c){
return vec_xmadd(a, b, c); return vec_xmadd(a, b, c);
} }
FLOAT_WRAP_3(operator(), inline) FLOAT_WRAP_3(operator(), inline)
}; };
struct MultComplex{ struct MultComplex{
// Complex double // Complex double
inline vector4double operator()(vector4double a, vector4double b){ inline vector4double operator()(vector4double a, vector4double b){
return vec_xxnpmadd(a, b, vec_xmul(b, a)); return vec_xxnpmadd(a, b, vec_xmul(b, a));
@ -297,9 +298,9 @@ namespace Optimization {
// Complex float // Complex float
FLOAT_WRAP_2(operator(), inline) FLOAT_WRAP_2(operator(), inline)
}; };
struct Mult{ struct Mult{
// Real double // Real double
inline vector4double operator()(vector4double a, vector4double b){ inline vector4double operator()(vector4double a, vector4double b){
return vec_mul(a, b); return vec_mul(a, b);
@ -319,9 +320,9 @@ namespace Optimization {
return out; return out;
} }
}; };
struct Div{ struct Div{
// Real double // Real double
inline vector4double operator()(vector4double a, vector4double b){ inline vector4double operator()(vector4double a, vector4double b){
return vec_swdiv(a, b); return vec_swdiv(a, b);
@ -341,9 +342,9 @@ namespace Optimization {
return out; return out;
} }
}; };
struct Conj{ struct Conj{
// Complex double // Complex double
inline vector4double operator()(vector4double v){ inline vector4double operator()(vector4double v){
return vec_mul(v, (vector4double){1., -1., 1., -1.}); return vec_mul(v, (vector4double){1., -1., 1., -1.});
@ -351,9 +352,9 @@ namespace Optimization {
// Complex float // Complex float
FLOAT_WRAP_1(operator(), inline) FLOAT_WRAP_1(operator(), inline)
}; };
struct TimesMinusI{ struct TimesMinusI{
//Complex double //Complex double
inline vector4double operator()(vector4double v, vector4double ret){ inline vector4double operator()(vector4double v, vector4double ret){
return vec_xxcpnmadd(v, (vector4double){1., 1., 1., 1.}, return vec_xxcpnmadd(v, (vector4double){1., 1., 1., 1.},
@ -362,9 +363,9 @@ namespace Optimization {
// Complex float // Complex float
FLOAT_WRAP_2(operator(), inline) FLOAT_WRAP_2(operator(), inline)
}; };
struct TimesI{ struct TimesI{
//Complex double //Complex double
inline vector4double operator()(vector4double v, vector4double ret){ inline vector4double operator()(vector4double v, vector4double ret){
return vec_xxcpnmadd(v, (vector4double){-1., -1., -1., -1.}, return vec_xxcpnmadd(v, (vector4double){-1., -1., -1., -1.},
@ -373,9 +374,9 @@ namespace Optimization {
// Complex float // Complex float
FLOAT_WRAP_2(operator(), inline) FLOAT_WRAP_2(operator(), inline)
}; };
#define USE_FP16 #define USE_FP16
struct PrecisionChange { struct PrecisionChange {
static inline vech StoH (const vector4float &a, const vector4float &b) { static inline vech StoH (const vector4float &a, const vector4float &b) {
vech ret; vech ret;
std::cout << GridLogError << "QPX single to half precision conversion not yet supported." << std::endl; std::cout << GridLogError << "QPX single to half precision conversion not yet supported." << std::endl;
@ -408,10 +409,10 @@ namespace Optimization {
std::cout << GridLogError << "QPX half to double precision conversion not yet supported." << std::endl; std::cout << GridLogError << "QPX half to double precision conversion not yet supported." << std::endl;
assert(0); assert(0);
} }
}; };
////////////////////////////////////////////// //////////////////////////////////////////////
// Exchange support // Exchange support
#define FLOAT_WRAP_EXCHANGE(fn) \ #define FLOAT_WRAP_EXCHANGE(fn) \
static inline void fn(vector4float &out1, vector4float &out2, \ static inline void fn(vector4float &out1, vector4float &out2, \
vector4float in1, vector4float in2) \ vector4float in1, vector4float in2) \
@ -424,7 +425,7 @@ namespace Optimization {
Vstore()(out2d, out2); \ Vstore()(out2d, out2); \
} }
struct Exchange{ struct Exchange{
// double precision // double precision
static inline void Exchange0(vector4double &out1, vector4double &out2, static inline void Exchange0(vector4double &out1, vector4double &out2,
@ -451,9 +452,9 @@ namespace Optimization {
FLOAT_WRAP_EXCHANGE(Exchange1); FLOAT_WRAP_EXCHANGE(Exchange1);
FLOAT_WRAP_EXCHANGE(Exchange2); FLOAT_WRAP_EXCHANGE(Exchange2);
FLOAT_WRAP_EXCHANGE(Exchange3); FLOAT_WRAP_EXCHANGE(Exchange3);
}; };
struct Permute{ struct Permute{
//Complex double //Complex double
static inline vector4double Permute0(vector4double v){ //0123 -> 2301 static inline vector4double Permute0(vector4double v){ //0123 -> 2301
return vec_perm(v, v, vec_gpci(02301)); return vec_perm(v, v, vec_gpci(02301));
@ -473,9 +474,9 @@ namespace Optimization {
FLOAT_WRAP_1(Permute1, static inline) FLOAT_WRAP_1(Permute1, static inline)
FLOAT_WRAP_1(Permute2, static inline) FLOAT_WRAP_1(Permute2, static inline)
FLOAT_WRAP_1(Permute3, static inline) FLOAT_WRAP_1(Permute3, static inline)
}; };
struct Rotate{ struct Rotate{
template<int n> static inline vector4double tRotate(vector4double v){ template<int n> static inline vector4double tRotate(vector4double v){
if ( n==1 ) return vec_perm(v, v, vec_gpci(01230)); if ( n==1 ) return vec_perm(v, v, vec_gpci(01230));
@ -519,23 +520,23 @@ namespace Optimization {
Vstore()(rd, r); Vstore()(rd, r);
return r; return r;
} }
}; };
//Complex float Reduce //Complex float Reduce
template<> template<>
inline Grid::ComplexF inline Grid::ComplexF
Reduce<Grid::ComplexF, vector4float>::operator()(vector4float v) { //2 complex Reduce<Grid::ComplexF, vector4float>::operator()(vector4float v) { //2 complex
vector4float v1,v2; vector4float v1,v2;
v1 = Optimization::Permute::Permute0(v); v1 = Optimization::Permute::Permute0(v);
v1 = Optimization::Sum()(v1, v); v1 = Optimization::Sum()(v1, v);
return Grid::ComplexF(v1.v0, v1.v1); return Grid::ComplexF(v1.v0, v1.v1);
} }
//Real float Reduce //Real float Reduce
template<> template<>
inline Grid::RealF inline Grid::RealF
Reduce<Grid::RealF, vector4float>::operator()(vector4float v){ //4 floats Reduce<Grid::RealF, vector4float>::operator()(vector4float v){ //4 floats
vector4float v1,v2; vector4float v1,v2;
v1 = Optimization::Permute::Permute0(v); v1 = Optimization::Permute::Permute0(v);
@ -544,25 +545,25 @@ namespace Optimization {
v1 = Optimization::Sum()(v1, v2); v1 = Optimization::Sum()(v1, v2);
return v1.v0; return v1.v0;
} }
//Complex double Reduce //Complex double Reduce
template<> template<>
inline Grid::ComplexD inline Grid::ComplexD
Reduce<Grid::ComplexD, vector4double>::operator()(vector4double v){ //2 complex Reduce<Grid::ComplexD, vector4double>::operator()(vector4double v){ //2 complex
vector4double v1; vector4double v1;
v1 = Optimization::Permute::Permute0(v); v1 = Optimization::Permute::Permute0(v);
v1 = vec_add(v1, v); v1 = vec_add(v1, v);
return Grid::ComplexD(vec_extract(v1, 0), vec_extract(v1, 1)); return Grid::ComplexD(vec_extract(v1, 0), vec_extract(v1, 1));
} }
//Real double Reduce //Real double Reduce
template<> template<>
inline Grid::RealD inline Grid::RealD
Reduce<Grid::RealD, vector4double>::operator()(vector4double v){ //4 doubles Reduce<Grid::RealD, vector4double>::operator()(vector4double v){ //4 doubles
vector4double v1,v2; vector4double v1,v2;
v1 = Optimization::Permute::Permute0(v); v1 = Optimization::Permute::Permute0(v);
@ -571,20 +572,21 @@ namespace Optimization {
v1 = vec_add(v1, v2); v1 = vec_add(v1, v2);
return vec_extract(v1, 0); return vec_extract(v1, 0);
} }
//Integer Reduce //Integer Reduce
template<> template<>
inline Integer Reduce<Integer, veci>::operator()(veci in){ inline Integer Reduce<Integer, veci>::operator()(veci in){
Integer a = 0; Integer a = 0;
for (unsigned int i = 0; i < W<Integer>::r; ++i) for (unsigned int i = 0; i < W<Integer>::r; ++i)
{ {
a += in.v[i]; a += in.v[i];
} }
return a; return a;
}
} }
NAMESPACE_END(Optimization);
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Here assign types // Here assign types
typedef Optimization::vech SIMD_Htype; // Half precision type typedef Optimization::vech SIMD_Htype; // Half precision type
@ -596,7 +598,6 @@ typedef Optimization::veci SIMD_Itype; // Integer type
inline void v_prefetch0(int size, const char *ptr){}; inline void v_prefetch0(int size, const char *ptr){};
inline void prefetch_HINT_T0(const char *ptr){}; inline void prefetch_HINT_T0(const char *ptr){};
// Function name aliases // Function name aliases
typedef Optimization::Vsplat VsplatSIMD; typedef Optimization::Vsplat VsplatSIMD;
typedef Optimization::Vstore VstoreSIMD; typedef Optimization::Vstore VstoreSIMD;
@ -616,4 +617,4 @@ typedef Optimization::Conj ConjSIMD;
typedef Optimization::TimesMinusI TimesMinusISIMD; typedef Optimization::TimesMinusI TimesMinusISIMD;
typedef Optimization::TimesI TimesISIMD; typedef Optimization::TimesI TimesISIMD;
} NAMESPACE_END(Grid)