我创建了一个使用SSE4.1向量指令的应用程序。为了更好地管理矢量类型,我创建了模板化的辅助结构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
};
但我无法相信没有更好的方法可以做到。
答案 0 :(得分:3)
我也不喜欢宏,但它有效。
template <class T, int ASize>
struct vector_type_impl;
// Clang bug workaround
#ifdef CLANG_BUG_VECTORTYPE
#error CLANG_BUG_VECTORTYPE should not be defined
#endif
#define CLANG_BUG_VECTORTYPE(T) \
template <> struct vector_type_impl<T,16> { \
typedef T type __attribute__((vector_size(16))); \
};
CLANG_BUG_VECTORTYPE(signed char);
CLANG_BUG_VECTORTYPE(unsigned char);
CLANG_BUG_VECTORTYPE(signed short);
CLANG_BUG_VECTORTYPE(unsigned short);
CLANG_BUG_VECTORTYPE(signed int);
CLANG_BUG_VECTORTYPE(unsigned int);
CLANG_BUG_VECTORTYPE(std::int64_t);
CLANG_BUG_VECTORTYPE(std::uint64_t);
CLANG_BUG_VECTORTYPE(float);
CLANG_BUG_VECTORTYPE(double);
#undef CLANG_BUG_VECTORTYPE
template <class T, int N>
struct vector_type {
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
};
我冒昧地为每个特征类型和每个实际矢量类型写一个typedef
,用于每个128位SSE矢量布局。
typedef vector_type<signed char, 16> sbyte16_vec;
typedef vector_type<signed char, 16> ubyte16_vec;
typedef vector_type<signed short, 8> short8_vec;
typedef vector_type<unsigned short, 8> ushort8_vec;
typedef vector_type<signed int, 4> int4_vec;
typedef vector_type<unsigned int, 4> uint4_vec;
typedef vector_type<std::int64_t, 2> long2_vec;
typedef vector_type<std::uint64_t, 2> ulong2_vec;
typedef vector_type<float, 4> float4_vec;
typedef vector_type<double, 2> double2_vec;
typedef sbyte16_vec::type sbyte16;
typedef ubyte16_vec::type ubyte16;
typedef short8_vec::type short8;
typedef ushort8_vec::type ushort8;
typedef int4_vec::type int4;
typedef uint4_vec::type uint4;
typedef long2_vec::type long2;
typedef ulong2_vec::type ulong2;
typedef float4_vec::type float4;
typedef double2_vec::type double2;
上面的现场演示测试了所有SSE矢量类型。
答案 1 :(得分:2)
您可以使用Clang's OpenCL vector extensions。
这是一个适用于GCC,Clang和ICC的解决方案。
template <class T, int N>
struct vector_type {
#if defined(__clang__)
typedef T type __attribute__((ext_vector_type(N)));
#else
typedef T type __attribute__((vector_size(sizeof(T)*N)));
#endif
using single_element_type = T;
static constexpr int size = N;
typedef union {
type v;
T s[N];
} access_type;
// some other implementation
};
像ICC一样的Clang只支持GCC向量扩展的一小部分。但Clang的OpenCL向量扩展涵盖了GCC向量扩展可以做的大部分内容。我做了一张桌子here。除vector = scalar
操作外,GCC的向量扩展执行Clang的OpenCL向量扩展等所有操作。