运算符重载与整数模板参数专门化

时间:2017-12-10 18:50:56

标签: c++ templates integer operator-overloading

我有一个矢量类,定义如下:

template <unsigned int SIZE>
class Vector : public Stringable
{
    private:

        float mData [SIZE];

    public:
        Vector ();

        ...
};

我还在这个(数学)向量上声明了一些运算符(在Vector类之外):

template <unsigned int SIZE>
Vector<SIZE> operator+ (const Vector<SIZE>& vec, float c);

template<>
Vector<3> operator+ (const Vector<3>& vec, float c);

template<>
Vector<4> operator+ (const Vector<4>& vec, float c);

template <unsigned int SIZE>
Vector<SIZE> operator+ (const Vector<SIZE>& vec1, const Vector<SIZE>& vec2);

template <>
Vector<3> operator+ (const Vector<3>& vec1, const Vector<3>& vec2);

template <>
Vector<4> operator+ (const Vector<4>& vec1, const Vector<4>& vec2);

这是它们的实施方式:

template <unsigned int SIZE>
Vector<SIZE> operator+(const Vector<SIZE>& vec1, const Vector<SIZE>& vec2)
{
    Vector<SIZE> result = Vector<SIZE>(vec1);

    int i = 0;
    for (i = 0; (i + 4) < SIZE; i += 4)
    {
        __m128 lhs = _mm_set_ps(vec1[i], vec1[i+1], vec1[i+2], vec1[i+3]);
        __m128 rhs = _mm_set_ps(vec2[i], vec2[i+1], vec2[i+2], vec2[i+3]);
        _mm_add_ps(lhs, rhs);
        _mm_store_ps(result.getRawData(), lhs);
    }

    for (; i < SIZE; i++)
    {
        result[i] = vec1[i] - vec2[i];
    }

    return result;
}

template<>
Vector<3> operator+<3>(const Vector<3>& vec1, const Vector<3>& vec2)
{
    Vector<3> result = Vector<3>(vec1);
    float tmpBuffer [4];

    __m128 lhs = _mm_set_ps(vec1[0], vec1[1], vec1[2], 0);
    __m128 rhs = _mm_set_ps(vec2[0], vec2[1], vec2[2], 0);
    _mm_add_ps(lhs, rhs);
    _mm_store_ps(tmpBuffer, lhs);
    mempcpy(result.getRawData(), tmpBuffer, 3 * sizeof(float));

    return result;
}

template<>
Vector<4> operator+<4>(const Vector<4>& vec1, const Vector<4>& vec2)
{
    Vector<4> result = Vector<4>(vec1);

    __m128 lhs = _mm_set_ps(vec1[0], vec1[1], vec1[2], vec1[3]);
    __m128 rhs = _mm_set_ps(vec2[0], vec2[1], vec2[2], vec1[3]);
    _mm_add_ps(lhs, rhs);
    _mm_store_ps(result.getRawData(), lhs);

    return result;
}

template <unsigned int SIZE>
Vector<SIZE> operator+(const Vector<SIZE>& vec, float c)
{
    Vector<SIZE> result = Vector<SIZE>(vec);

    int i = 0;
    for (i = 0; (i + 4) < SIZE; i += 4)
    {
        __m128 lhs = _mm_set_ps(vec[i], vec[i+1], vec[i+2], vec[i+3]);
        __m128 rhs = _mm_set1_ps(c);
        _mm_add_ps(lhs, rhs);
        _mm_store_ps(result.getRawData(), lhs);
    }

    for (; i < SIZE; i++)
    {
        result[i] = vec[i] + c;
    }

    return result;
}

template <>
Vector<3> operator+<3>(const Vector<3>& vec, float c)
{
    Vector<3> result = Vector<3>(vec);
    float tmpBuffer [4];

    __m128 lhs = _mm_set_ps(vec[0], vec[1], vec[2], 0);
    __m128 rhs = _mm_set1_ps(c);
    _mm_add_ps(lhs, rhs);
    _mm_store_ps(tmpBuffer, lhs);
    mempcpy(result.getRawData(), tmpBuffer, 3 * sizeof(float));

    return result;
}

template <>
Vector<4> operator+<4>(const Vector<4>& vec, float c)
{
    Vector<4> result = Vector<4>(vec);

    __m128 lhs = _mm_set_ps(vec[0], vec[1], vec[2], 0);
    __m128 rhs = _mm_set1_ps(c);
    _mm_add_ps(lhs, rhs);
    _mm_store_ps(result.getRawData(), lhs);

    return result;
}

以上所有代码都在一个文件中:“Vector.cpp”

我目前正在使用英特尔的SSE内在函数从“正常”矢量操作转向SIMD操作。

因为大小为3&amp;的载体4在我的应用程序中很常见,我想专门为我的模板操作符,以便更好地使用XMM寄存器。 (如您所见,SIMD循环不会用于任何大小<4的矢量。)

不幸的是,我对如何实现这一点感到茫然,我真的不知道我需要什么声明/定义,模板参数需要什么,......

编辑:现在,我收到了'多个定义'链接器错误,我正在使用clang ++ 5.0

这些错误中有大约50个(很多,50个只是猜测),所有专业化似乎都存在......

CMakeFiles/Space.elf.dir/src/Entities/Entity.cpp.o: In function `Maths::Vector<3u> Maths::operator+<3u>(Maths::Vector<3u> const&, float)':
/home/.../CLionProjects/Space/src/Maths/Vector.cpp:427: multiple definition of `Maths::Vector<3u> Maths::operator+<3u>(Maths::Vector<3u> const&, float)'
CMakeFiles/Space.elf.dir/src/main.cpp.o:/home/.../CLionProjects/Space/src/Maths/Vector.cpp:427: first defined here
CMakeFiles/Space.elf.dir/src/Entities/Entity.cpp.o: In function `Maths::Vector<4u> Maths::operator+<4u>(Maths::Vector<4u> const&, Maths::Vector<4u> const&)':
/home/.../CLionProjects/Space/src/Maths/Vector.cpp:392: multiple definition of `Maths::Vector<4u> Maths::operator+<4u>(Maths::Vector<4u> const&, Maths::Vector<4u> const&)'
CMakeFiles/Space.elf.dir/src/main.cpp.o:/home/.../CLionProjects/Space/src/Maths/Vector.cpp:392: first defined here
/home/.../CLionProjects/Space/src/Maths/Vector.cpp:377: multiple definition of `Maths::Vector<3u> Maths::operator+<3u>(Maths::Vector<3u> const&, Maths::Vector<3u> const&)'
CMakeFiles/Space.elf.dir/src/main.cpp.o:/home/.../CLionProjects/Space/src/Maths/Vector.cpp:377: first defined here
CMakeFiles/Space.elf.dir/src/Entities/EntityManager.cpp.o: In function `Maths::Vector<3u> Maths::operator+<3u>(Maths::Vector<3u> const&, float)':

我已经审查了部分路径,但你得到了想法

1 个答案:

答案 0 :(得分:0)

好的,我解决了我的问题,这是怎么回事。 我像往常一样对任何其他类分隔定义和声明。 然后在我的实现文件(.cpp)的底部,我明确地实例化了我的类,还有我的所有操作符。

(不幸的是,加载/卸载的数量使SIMD指令过时了,因为它创造了比它摆脱的更多的开销。专门的完整类可能是一个解决方案,但这是另一个时间)