我创建了一个使用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’属性需要一个整数常量.我完全清楚这是一个众所周知的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
};
但我无法相信没有更好的方法来做到这一点.
解决方法:
我也不喜欢宏,但它有效.
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
};
对于每个128位SSE向量布局,我冒昧地为每个特征类型和每个实际向量类型编写一个typedef.
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矢量类型.
标签:c,templates,clang,sse 来源: https://codeday.me/bug/20190829/1758674.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。