CUDA复数类的定义

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;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值