1

A usual way to target different floating point precisions (float / double) is either by typedefs

typedef float Real;
//typedef double Real;

or by using templates

template<typename Real>
...

This is convenient, but anyone has ideas how to use the CUDA types float2/float3/... and make_float2/make_float3/... ? Sure, I could make #defines or typedefs for all of them but that seems not very elegant.

4

1 回答 1

6

您可以实现连接类型和通道编号的辅助类:

template <typename T, int cn> struct MakeVec;
template <> struct MakeVec<float, 3>
{
    typedef float3 type;
};
template <> struct MakeVec<double, 3>
{
    typedef double3 type;
};
// and so on for all combination of T and cn

用法:

template <typename T>
void func()
{
    typedef typename MakeVec<T, 4>::type vec4_type;
    vec4_type vec4; // for T=float it will be float4, for T=double it will be double4
}

你可以在这里找到实现

于 2013-07-24T12:47:59.730 回答