【发布时间】:2016-04-26 20:50:40
【问题描述】:
我创建了一个使用 SSE4.1 矢量指令的应用程序。为了更好地管理向量类型,我创建了模板化帮助器 struct vector_type,如下所示:
template <class T, int N>
struct vector_type {
typedef T type __attribute__((vector_size(sizeof(T)*N)));
using single_element_type = T;
static constexpr int size = N;
typedef union {
type v;
T s[N];
} access_type;
// some other implementation
};
这与 g++ 完美编译。不幸的是,clang++(我在 3.6 版中使用 clang++)抱怨 'vector_size' attribute requires an integer constant。我完全清楚这是一个众所周知的 clang 问题,它作为 Bug 16986 提交。我的问题是有没有办法解决这个问题。我想出了代码:
template <class T, int ASize>
struct vector_type_impl;
template <>
struct vector_type_impl<int,16> {
typedef int type __attribute__((vector_size(16)));
};
template <class T, int N>
struct vector_type: vector_type_impl<T, N*sizeof(T)> {
using type = typename vector_type_impl<T, N*sizeof(T)>::type;
using single_element_type = T;
static constexpr int size = N;
typedef union {
type v;
T s[N];
} access_type;
// some other implementation
};
但我不敢相信没有更好的方法。
【问题讨论】:
-
向量类型
typedefed 和__attribute((vector_size(N)))或[[gnu::vector_size(N)]]有一个内置的operator [],所以使用union是相当浪费的。 -
似乎 Bug 16986 已在 Clang 7 中修复