字符串化内联汇编程序的模板类型

时间:2017-11-01 13:07:41

标签: c++ c++11 templates inline-assembly

我正在寻找一种通过模板函数自动化gcc内联汇编调用的方法 例如,我有以下虚函数将值存储到指针中。现在我专门针对不同类型的模板功能。每当代码中的某些内容发生变化时,我都需要针对每个专业化进行更改。

template <typename T>
void store_ptr(T *location, T value);

template <>
void store_ptr<char>(char *location, char value) {
  __asm__ __volatile__(
      "strb %1, [%0]\n\t"
      : "+r" (location)
      : "r" (value)
      : "memory"
  );
}


template <>
void store_ptr<short>(short *location, short value) {
  __asm__ __volatile__(
      "strh %1, [%0]\n\t"
      : "+r" (location)
      : "r" (value)
      : "memory"
  );
}

如果模板可以根据模板类型对指令附录(&#34; b&#34;,&#34; h&#34; ...)进行字符串化,那将是很好的。

template <typename T>
void store_ptr<T>(T *location, T value) {
  __asm__ __volatile__(
     "str" stringify_template_type(T) " %1, [%0]\n\t"
     : "+r" (location)
     : "r" (value)
     : "memory"
     );
}

是否有(简单)方法实现这一目标?

0 个答案:

没有答案