【问题标题】:clang templated use of __attribute__((vector_size(N)))clang 模板化使用 __attribute__((vector_size(N)))
【发布时间】: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 中修复

标签: c++ templates clang sse


【解决方案1】:

我也不喜欢宏,但它可以工作。

Live Demo

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 向量类型。

【讨论】:

  • 哦等等,clang++ 不喜欢它。如果找不到 clang++ 解决方案,我将删除答案。
  • 是的,这可能是在 clang 中实现它的唯一方法,希望他们能在未来的版本中继续努力......
【解决方案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 向量扩展等所有工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 2013-07-15
    • 2013-11-17
    • 1970-01-01
    相关资源
    最近更新 更多