c++ - 如何重载其参数仅因 gcc vector 扩展 vector_size 属性不同而不同的函数?

标签 c++ gcc simd

我正在尝试编写一些常见的实用函数,例如将 gcc vector 中的所有元素相加。

inline float add_all(float const in __attribute__((vector_size(8))))
{
  return in[0] + in[1];
}

inline float add_all(float const in __attribute__((vector_size(16))))
{
  return in[0] + in[1] + in[2] + in[3];
}

inline double add_all(double const in __attribute__((vector_size(16))))
{
  return in[0] + in[1];
}

inline double add_all(double const in __attribute__((vector_size(32))))
{
  return in[0] + in[1] + in[2] + in[3];
}

但是,编译时,gcc对象:

In file included from matrix.hpp:5:0,
                 from matrix.cpp:3:
vector.hpp:22:1: error: 'float vxl::add_all(__vector(4) float)' conflicts with a previous declaration
 }
 ^
vector.hpp:14:14: note: previous declaration 'float vxl::add_all(__vector(2) float)'
 inline float add_all(float const in __attribute__((vector_size(8))))
              ^
vector.hpp:19:14: note: -fabi-version=6 (or =0) avoids this error with a change in mangling
 inline float add_all(float const in __attribute__((vector_size(16))))
              ^
vector.hpp:32:1: error: 'double vxl::add_all(__vector(4) double)' conflicts with a previous declaration
 }
 ^
vector.hpp:24:15: note: previous declaration 'double vxl::add_all(__vector(2) double)'
 inline double add_all(double const in __attribute__((vector_size(16))))
               ^
vector.hpp:29:15: note: -fabi-version=6 (or =0) avoids this error with a change in mangling
 inline double add_all(double const in __attribute__((vector_size(32))))

除了 gcc 建议的方法之外,是否存在解决方法?

最佳答案

提供一个额外的默认参数,为函数提供不同的损坏名称:

typedef float __attribute__((vector_size(8))) vector_f8;
typedef float __attribute__((vector_size(16))) vector_f16;
typedef double __attribute__((vector_size(16))) vector_d16;
typedef double __attribute__((vector_size(32))) vector_d32;

template <int _len>
struct vector_len {
    static int const len = _len;
};

float
add_all(vector_f8 in, vector_len<8> *_p = NULL) {
    return in[0] + in[1];
}

float
add_all(vector_f16 in, vector_len<16> *_p = NULL) {
    return in[0] + in[1] + in[2] + in[3];
}

float
add_all(vector_d16 in, vector_len<16> *_p = NULL) {
    return in[0] + in[1];
}

float
add_all(vector_d32 in, vector_len<32> *_p = NULL) {
    return in[0] + in[1] + in[2] + in[3];
}

关于c++ - 如何重载其参数仅因 gcc vector 扩展 vector_size 属性不同而不同的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25339890/

相关文章:

c++ - 双链表中的智能指针

linux - 在 Debian 64 位上编译和链接 32 位应用程序

embedded - 对于 NEON 优化,哪个更好,gcc 或 armcc?

c - Windows gcc 上 srand() 输出的差异与 C 编程的预期输出?

c - 严格别名仅适用于第一个元素吗?

c++ - 256 位 AVX vector 中 32 位 float 的水平和

c++ - 如何跨文件分配 header

c++ - 使用 boost::shared_ptr 进行多态转换

c++ - 在 C++ 中检查打开的 UDP 端口

C++:使用 longjmp 和 setjmp 安全吗?