我目前正在试验 GCC vector extensions .但是,我想知道如何让 sqrt(vec)
按预期工作。
如:
typedef double v4d __attribute__ ((vector_size (16)));
v4d myfunc(v4d in)
{
return some_sqrt(in);
}
至少在最近的 x86 系统上,它发出对相关内部 sqrtpd 的调用。是否有适用于矢量类型的 sqrt 内置 GCC,或者是否需要降低到固有级别才能完成此操作?
最佳答案
看起来这是一个错误:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54408除了按组件进行之外,我不知道有任何解决方法。无论如何,矢量扩展从来都不是为了取代特定于平台的内在函数。
一些时髦的代码:
#include <cmath>
#include <utility>
template <::std::size_t...> struct indices { };
template <::std::size_t M, ::std::size_t... Is>
struct make_indices : make_indices<M - 1, M - 1, Is...> {};
template <::std::size_t... Is>
struct make_indices<0, Is...> : indices<Is...> {};
typedef float vec_type __attribute__ ((vector_size(4 * sizeof(float))));
template <::std::size_t ...Is>
vec_type sqrt_(vec_type const& v, indices<Is...> const)
{
vec_type r;
::std::initializer_list<int>{(r[Is] = ::std::sqrt(v[Is]), 0)...};
return r;
}
vec_type sqrt(vec_type const& v)
{
return sqrt_(v, make_indices<4>());
}
int main()
{
vec_type v;
return sqrt(v)[0];
}
您也可以试试自动矢量化的运气,它与矢量扩展是分开的。
关于GCC 向量扩展区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18921049/