QUDA  0.9.0
convert.h
Go to the documentation of this file.
1 #pragma once
2 
11 #include <quda_internal.h> // for MAX_SHORT
12 
13 template <typename type> inline int vecLength() { return 0; }
14 
15 template<> inline int vecLength<short>() { return 1; }
16 template<> inline int vecLength<float>() { return 1; }
17 template<> inline int vecLength<double>() { return 1; }
18 
19 template<> inline int vecLength<short2>() { return 2; }
20 template<> inline int vecLength<float2>() { return 2; }
21 template<> inline int vecLength<double2>() { return 2; }
22 
23 template<> inline int vecLength<short4>() { return 4; }
24 template<> inline int vecLength<float4>() { return 4; }
25 template<> inline int vecLength<double4>() { return 4; }
26 
27 // MAX_SHORT 32767
28 #define MAX_SHORT_INV 3.051850948e-5
29 static inline __device__ float s2f(const short &a) { return static_cast<float>(a) * MAX_SHORT_INV; }
30 static inline __device__ double s2d(const short &a) { return static_cast<double>(a) * MAX_SHORT_INV; }
31 
32 template <typename FloatN>
33 __device__ inline void copyFloatN(FloatN &a, const FloatN &b) { a = b; }
34 
35 // This is emulating the texture normalized return
36 __device__ inline void copyFloatN(float2 &a, const short2 &b) { a = make_float2(s2f(b.x), s2f(b.y)); }
37 __device__ inline void copyFloatN(float4 &a, const short4 &b) { a = make_float4(s2f(b.x), s2f(b.y), s2f(b.z), s2f(b.w)); }
38 __device__ inline void copyFloatN(double2 &a, const short2 &b) { a = make_double2(s2d(b.x), s2d(b.y)); }
39 __device__ inline void copyFloatN(double4 &a, const short4 &b) { a = make_double4(s2d(b.x), s2d(b.y), s2d(b.z), s2d(b.w)); }
40 
41 __device__ inline void copyFloatN(float2 &a, const double2 &b) { a = make_float2(b.x, b.y); }
42 __device__ inline void copyFloatN(double2 &a, const float2 &b) { a = make_double2(b.x, b.y); }
43 __device__ inline void copyFloatN(float4 &a, const double4 &b) { a = make_float4(b.x, b.y, b.z, b.w); }
44 __device__ inline void copyFloatN(double4 &a, const float4 &b) { a = make_double4(b.x, b.y, b.z, b.w); }
45 
46 // Fast float to integer round
47 __device__ inline int f2i(float f) { f += 12582912.0f; return reinterpret_cast<int&>(f); }
48 
49 // Fast double to integer round
50 __device__ inline int d2i(double d) { d += 6755399441055744.0; return reinterpret_cast<int&>(d); }
51 
52 /* Here we assume that the input data has already been normalized and shifted. */
53 __device__ inline void copyFloatN(short2 &a, const float2 &b) { a = make_short2(f2i(b.x), f2i(b.y)); }
54 __device__ inline void copyFloatN(short4 &a, const float4 &b) { a = make_short4(f2i(b.x), f2i(b.y), f2i(b.z), f2i(b.w)); }
55 __device__ inline void copyFloatN(short2 &a, const double2 &b) { a = make_short2(d2i(b.x), d2i(b.y)); }
56 __device__ inline void copyFloatN(short4 &a, const double4 &b) { a = make_short4(d2i(b.x), d2i(b.y), d2i(b.z), d2i(b.w)); }
57 
58 
70 template<typename OutputType, typename InputType>
71 __device__ inline void convert(OutputType x[], InputType y[], const int N) {
72  // default is one-2-one conversion, e.g., matching vector lengths and precisions
73 #pragma unroll
74  for (int j=0; j<N; j++) copyFloatN(x[j], y[j]);
75 }
76 
77 template<> __device__ inline void convert<float2,short2>(float2 x[], short2 y[], const int N) {
78 #pragma unroll
79  for (int j=0; j<N; j++) x[j] = make_float2(y[j].x, y[j].y);
80 }
81 
82 template<> __device__ inline void convert<float4,short4>(float4 x[], short4 y[], const int N) {
83 #pragma unroll
84  for (int j=0; j<N; j++) x[j] = make_float4(y[j].x, y[j].y, y[j].z, y[j].w);
85 }
86 
87 // 4 <-> 2 vector conversion
88 
89 template<> __device__ inline void convert<double4,double2>(double4 x[], double2 y[], const int N) {
90 #pragma unroll
91  for (int j=0; j<N; j++) x[j] = make_double4(y[2*j].x, y[2*j].y, y[2*j+1].x, y[2*j+1].y);
92 }
93 
94 template<> __device__ inline void convert<double2,double4>(double2 x[], double4 y[], const int N) {
95 #pragma unroll
96  for (int j=0; j<N/2; j++) {
97  x[2*j] = make_double2(y[j].x, y[j].y);
98  x[2*j+1] = make_double2(y[j].z, y[j].w);
99  }
100 }
101 
102 template<> __device__ inline void convert<float4,float2>(float4 x[], float2 y[], const int N) {
103 #pragma unroll
104  for (int j=0; j<N; j++) x[j] = make_float4(y[2*j].x, y[2*j].y, y[2*j+1].x, y[2*j+1].y);
105 }
106 
107 template<> __device__ inline void convert<float2,float4>(float2 x[], float4 y[], const int N) {
108 #pragma unroll
109  for (int j=0; j<N/2; j++) {
110  x[2*j] = make_float2(y[j].x, y[j].y);
111  x[2*j+1] = make_float2(y[j].z, y[j].w);
112  }
113 }
114 
115 template<> __device__ inline void convert<short4,float2>(short4 x[], float2 y[], const int N) {
116 #pragma unroll
117  for (int j=0; j<N; j++) x[j] = make_short4(f2i(y[2*j].x), f2i(y[2*j].y), f2i(y[2*j+1].x), f2i(y[2*j+1].y));
118 }
119 
120 template<> __device__ inline void convert<float2,short4>(float2 x[], short4 y[], const int N) {
121 #pragma unroll
122  for (int j=0; j<N/2; j++) {
123  x[2*j] = make_float2(y[j].x, y[j].y);
124  x[2*j+1] = make_float2(y[j].z, y[j].w);
125  }
126 }
127 
128 template<> __device__ inline void convert<float4,short2>(float4 x[], short2 y[], const int N) {
129 #pragma unroll
130  for (int j=0; j<N; j++) x[j] = make_float4(y[2*j].x, y[2*j].y, y[2*j+1].x, y[2*j+1].y);
131 }
132 
133 template<> __device__ inline void convert<short2,float4>(short2 x[], float4 y[], const int N) {
134 #pragma unroll
135  for (int j=0; j<N/2; j++) {
136  x[2*j] = make_short2(f2i(y[j].x), f2i(y[j].y));
137  x[2*j+1] = make_short2(f2i(y[j].z), f2i(y[j].w));
138  }
139 }
140 
141 template<> __device__ inline void convert<short4,double2>(short4 x[], double2 y[], const int N) {
142 #pragma unroll
143  for (int j=0; j<N; j++) x[j] = make_short4(d2i(y[2*j].x), d2i(y[2*j].y), d2i(y[2*j+1].x), d2i(y[2*j+1].y));
144 }
145 
146 template<> __device__ inline void convert<double2,short4>(double2 x[], short4 y[], const int N) {
147 #pragma unroll
148  for (int j=0; j<N/2; j++) {
149  x[2*j] = make_double2(y[j].x, y[j].y);
150  x[2*j+1] = make_double2(y[j].z, y[j].w);
151  }
152 }
153 
154 template<> __device__ inline void convert<double4,short2>(double4 x[], short2 y[], const int N) {
155 #pragma unroll
156  for (int j=0; j<N; j++) x[j] = make_double4(y[2*j].x, y[2*j].y, y[2*j+1].x, y[2*j+1].y);
157 }
158 
159 template<> __device__ inline void convert<short2,double4>(short2 x[], double4 y[], const int N) {
160 #pragma unroll
161  for (int j=0; j<N/2; j++) {
162  x[2*j] = make_short2(d2i(y[j].x), d2i(y[j].y));
163  x[2*j+1] = make_short2(d2i(y[j].z), d2i(y[j].w));
164  }
165 }
166 
167 template<> __device__ inline void convert<float4,double2>(float4 x[], double2 y[], const int N) {
168 #pragma unroll
169  for (int j=0; j<N; j++) x[j] = make_float4(y[2*j].x, y[2*j].y, y[2*j+1].x, y[2*j+1].y);
170 }
171 
172 template<> __device__ inline void convert<double2,float4>(double2 x[], float4 y[], const int N) {
173 #pragma unroll
174  for (int j=0; j<N/2; j++) {
175  x[2*j] = make_double2(y[j].x, y[j].y);
176  x[2*j+1] = make_double2(y[j].z, y[j].w);
177  }
178 }
179 
180 template<> __device__ inline void convert<double4,float2>(double4 x[], float2 y[], const int N) {
181 #pragma unroll
182  for (int j=0; j<N; j++) x[j] = make_double4(y[2*j].x, y[2*j].y, y[2*j+1].x, y[2*j+1].y);
183 }
184 
185 template<> __device__ inline void convert<float2,double4>(float2 x[], double4 y[], const int N) {
186 #pragma unroll
187  for (int j=0; j<N/2; j++) {
188  x[2*j] = make_float2(y[j].x, y[j].y);
189  x[2*j+1] = make_float2(y[j].z, y[j].w);
190  }
191 }
192 
int vecLength< short >()
Definition: convert.h:15
__device__ int f2i(float f)
Definition: convert.h:47
#define MAX_SHORT_INV
Definition: convert.h:28
int vecLength< float >()
Definition: convert.h:16
int vecLength< float4 >()
Definition: convert.h:24
int vecLength< double4 >()
Definition: convert.h:25
__device__ void convert< float4, short4 >(float4 x[], short4 y[], const int N)
Definition: convert.h:82
static __device__ double s2d(const short &a)
Definition: convert.h:30
int vecLength< double2 >()
Definition: convert.h:21
__device__ void convert< float2, short2 >(float2 x[], short2 y[], const int N)
Definition: convert.h:77
#define b
__device__ int d2i(double d)
Definition: convert.h:50
int vecLength< float2 >()
Definition: convert.h:20
__device__ void copyFloatN(FloatN &a, const FloatN &b)
Definition: convert.h:33
__device__ void convert< double4, short2 >(double4 x[], short2 y[], const int N)
Definition: convert.h:154
__device__ void convert(OutputType x[], InputType y[], const int N)
Definition: convert.h:71
__device__ void convert< short2, float4 >(short2 x[], float4 y[], const int N)
Definition: convert.h:133
__device__ void convert< float4, short2 >(float4 x[], short2 y[], const int N)
Definition: convert.h:128
int int int w
static __device__ float s2f(const short &a)
Definition: convert.h:29
__device__ void convert< float4, float2 >(float4 x[], float2 y[], const int N)
Definition: convert.h:102
__device__ void convert< float2, float4 >(float2 x[], float4 y[], const int N)
Definition: convert.h:107
int int int enum cudaChannelFormatKind f
__device__ void convert< double4, double2 >(double4 x[], double2 y[], const int N)
Definition: convert.h:89
__device__ void convert< float2, short4 >(float2 x[], short4 y[], const int N)
Definition: convert.h:120
int vecLength()
Definition: convert.h:13
__device__ void convert< double2, double4 >(double2 x[], double4 y[], const int N)
Definition: convert.h:94
__device__ void convert< double4, float2 >(double4 x[], float2 y[], const int N)
Definition: convert.h:180
__device__ void convert< double2, float4 >(double2 x[], float4 y[], const int N)
Definition: convert.h:172
__device__ void convert< float4, double2 >(float4 x[], double2 y[], const int N)
Definition: convert.h:167
__device__ void convert< double2, short4 >(double2 x[], short4 y[], const int N)
Definition: convert.h:146
int vecLength< short4 >()
Definition: convert.h:23
__device__ void convert< short4, float2 >(short4 x[], float2 y[], const int N)
Definition: convert.h:115
int vecLength< short2 >()
Definition: convert.h:19
int vecLength< double >()
Definition: convert.h:17
static __inline__ size_t size_t d
#define a
__device__ void convert< short4, double2 >(short4 x[], double2 y[], const int N)
Definition: convert.h:141
__device__ void convert< float2, double4 >(float2 x[], double4 y[], const int N)
Definition: convert.h:185
__device__ void convert< short2, double4 >(short2 x[], double4 y[], const int N)
Definition: convert.h:159