I create an application which make use of a SSE4.1 vector instructions. To better manage the vector types I've created templated helper struct vector_type as follows:
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
};
This compiles perfectly with g++. Unfortunately the clang++ (I use clang++ in version 3.6) complains that 'vector_size' attribute requires an integer constant
. I'm perfectly aware that this is a well known issue of clang, which is submitted as a Bug 16986. My question is rather is there a way to workaround the problem. I came up with the code:
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
};
But I can't believe there is no better way to do it.