使用模板切换SSE指令集内置函数

3

我使用模板特化的方式来切换浮点/双精度SSE指令:

template<typename Precision>
struct simd
{
    typedef Precision simd_vector;
};

template<>
struct simd<float>
{
    typedef __m128 simd_vector;
};

template<>
struct simd<double>
{
    typedef __m128d simd_vector;
};

int main()
{
    simd<float>::simd_vector float_vector;//__m128
    simd<double>::simd_vector double_vector;//__m128d
}

它的表现良好,但我不知道如何以相同的方式使用SSE内嵌函数? 假设我们想要切换添加操作:对于float,使用_mm_add_ps内部函数,对于double,使用_mm_add_pd。 我怎样才能使用模板专业化技巧呢?


这是“Precision”,不是“Presision”! :) - Mats Petersson
2个回答

4

由于您可能需要为想象中的每个操作生成一行,因此最好实现适当的运算符:

template<>
struct simd<double>
{
    typedef __m128d simd_vector;
};

simd<float>::simd_vector operator+(simd<float>::simd_vector a, simd<float>::simd_vector b)
{
    return _mm_add_ps(a, b);
}

etc.


1
你可以通过为每个类专门设计适当的函数来调用它们各自的函数。
template<>
struct simd<float>
{
    typedef __m128 simd_vector;
    simd_vector v;

    simd operator+(const simd& _s){
       //call my simd function for floats to operator on v
    }
};

template<>
struct simd<double>
{
    typedef __m128d simd_vector;
    simd_vector v;

    simd operator+(const simd& _s){
        //call my simd function for doubles to operate on v
    }
};

"而使用方法如下:"
simd<float> sf1, sf2;
simd<float> sf3 = sf1+sf2;
//get vector throw sd3.v;
simd<double> sd1, sd2;
simd<double> sd3 = sd1 + sd2;
//get vector through sd3.v;

当然,您需要适当地初始化sf1、sf2、sd1和sd2。

1
我添加了一个使用示例。 - pippin1289

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接