typedef float2 cuFloatComplex;
__host__ __device__ static __inline__ float cuCrealf (cuFloatComplex x)
{
return x.x;
}
__host__ __device__ static __inline__ float cuCimagf (cuFloatComplex x)
{
return x.y;
}
__host__ __device__ static __inline__ cuFloatComplex make_cuFloatComplex (float r, float i)
{
cuFloatComplex res;
res.x = r;
res.y = i;
return res;
}
__host__ __device__ static __inline__ cuFloatComplex cuConjf (cuFloatComplex x)
{
return make_cuFloatComplex (cuCrealf(x), -cuCimagf(x));
}
__host__ __device__ static __inline__ cuFloatComplex cuCaddf (cuFloatComplex x, cuFloatComplex y)
{
return make_cuFloatComplex (cuCrealf(x) + cuCrealf(y),
cuCimagf(x) + cuCimagf(y));
}
__host__ __device__ static __inline__ cuFloatComplex cuCsubf (cuFloatComplex x, cuFloatComplex y)
{
return make_cuFloatComplex (cuCrealf(x) - cuCrealf(y),
cuCimagf(x) - cuCimagf(y));
}
__host__ __device__ static __inline__ cuFloatComplex cuCmulf (cuFloatComplex x, cuFloatComplex y)
{
cuFloatComplex prod;
prod = make_cuFloatComplex ((cuCrealf(x) * cuCrealf(y)) - (cuCimagf(x) * cuCimagf(y)),
(cuCrealf(x) * cuCimagf(y)) + (cuCimagf(x) * cuCrealf(y)));
return prod;
}
__host__ __device__ static __inline__ cuFloatComplex cuCdivf (cuFloatComplex x, cuFloatComplex y)
{
cuFloatComplex quot;
float s = fabsf(cuCrealf(y)) + fabsf(cuCimagf(y));
float oos = 1.0f / s;
float ars = cuCrealf(x) * oos;
float ais = cuCimagf(x) * oos;
float brs = cuCrealf(y) * oos;
float bis = cuCimagf(y) * oos;
s = (brs * brs) + (bis * bis);
oos = 1.0f / s;
quot = make_cuFloatComplex (((ars * brs) + (ais * bis)) * oos, ((ais * brs) - (ars * bis)) * oos);
return quot;
}
__host__ __device__ static __inline__ float cuCabsf (cuFloatComplex x)
{
float a = cuCrealf(x);
float b = cuCimagf(x);
float v, w, t;
a = fabsf(a);
b = fabsf(b);
if (a > b)
{
v = a;
w = b;
}
else
{
v = b;
w = a;
}
t = w / v;//保证分母比分子大
t = 1.0f + t * t;
t = v * sqrtf(t);
if ((v == 0.0f) || (v > 3.402823466e38f) || (w > 3.402823466e38f))
{
t = v + w;
}
return t;
}
typedef double2 cuDoubleComplex;
__host__ __device__ static __inline__ double cuCreal (cuDoubleComplex x)
{
return x.x;
}
__host__ __device__ static __inline__ double cuCimag (cuDoubleComplex x)
{
return x.y;
}
__host__ __device__ static __inline__ cuDoubleComplex make_cuDoubleComplex (double r, double i)
{
cuDoubleComplex res;
res.x = r;
res.y = i;
return res;
}
__host__ __device__ static __inline__ cuDoubleComplex cuConj(cuDoubleComplex x)
{
return make_cuDoubleComplex (cuCreal(x), -cuCimag(x));
}
__host__ __device__ static __inline__ cuDoubleComplex cuCaddf (cuDoubleComplex x, cuDoubleComplex y)
{
return make_cuDoubleComplex (cuCrealf(x) + cuCrealf(y), cuCimagf(x) + cuCimagf(y));
}
__host__ __device__ static __inline__ cuDoubleComplex cuCsubf (cuDoubleComplex x, cuDoubleComplex y)
{
return make_cuDoubleComplex (cuCrealf(x) - cuCrealf(y),
cuCimagf(x) - cuCimagf(y));
}
__host__ __device__ static __inline__ cuDoubleComplex cuCmulf (cuDoubleComplex x, cuDoubleComplex y)
{
cuDoubleComplex prod;
prod = make_cuDoubleComplex ((cuCrealf(x) * cuCrealf(y)) - (cuCimagf(x) * cuCimagf(y)),
(cuCrealf(x) * cuCimagf(y)) + (cuCimagf(x) * cuCrealf(y)));
return prod;
}
__host__ __device__ static __inline__ cuDoubleComplex cuCdivf (cuDoubleComplex x, cuDoubleComplex y)
{
cuDoubleComplex quot;
double s = fabsf(cuCrealf(y)) + fabsf(cuCimagf(y));
double oos = 1.0 / s;
double ars = cuCrealf(x) * oos;
double ais = cuCimagf(x) * oos;
double brs = cuCrealf(y) * oos;
double bis = cuCimagf(y) * oos;
s = (brs * brs) + (bis * bis);
oos = 1.0 / s;
quot = make_cuDoubleComplex (((ars * brs) + (ais * bis)) * oos, ((ais * brs) - (ars * bis)) * oos);
return quot;
}
__host__ __device__ static __inline__ double cuCabsf (cuDoubleComplex x)
{
double a = cuCrealf(x);
double b = cuCimagf(x);
double v, w, t;
a = fabsf(a);
b = fabsf(b);
if (a > b)
{
v = a;
w = b;
} else
{
v = b;
w = a;
}
t = w / v;//保证分母比分子大
t = 1.0 + t * t;
t = v * sqrtf(t);
if ((v == 0.0) || (v > 1.7e308) || (w > 1.7e308))
{
t = v + w;
}
return t;
}
CUDA复数类的定义
最新推荐文章于 2024-06-24 10:45:46 发布