不允许参数化和"功能模板部分特化"

时间:2016-09-04 07:46:53

标签: c++ function-templates

这是What is the function parameter equivalent of constexpr?的延续。在最初的问题中,我们试图加速一些在Clang和VC ++下执行移位和旋转的代码。 Clang和VC ++没有很好地优化代码,因为它将移位/旋转量视为变量(即,不是constexpr)。

当我尝试参数化移位量和字大小时,会产生:

$ g++ -std=c++11 -march=native test.cxx -o test.exe
test.cxx:13:10: error: function template partial specialization is not allowed
uint32_t LeftRotate<uint32_t, unsigned int>(uint32_t v)
         ^         ~~~~~~~~~~~~~~~~~~~~~~~~
test.cxx:21:10: error: function template partial specialization is not allowed
uint64_t LeftRotate<uint64_t, unsigned int>(uint64_t v)
         ^         ~~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.

这是测试程序。它比需要的要大一些,所以人们可以看到我们需要处理uint32_tuint64_t(更不用说uint8_tuint16_t和其他类型了。

$ cat test.cxx
#include <iostream>
#include <stdint.h>

template<typename T, unsigned int R>
inline T LeftRotate(unsigned int v)
{
  static const unsigned int THIS_SIZE = sizeof(T)*8;
  static const unsigned int MASK = THIS_SIZE-1;
  return T((v<<R)|(v>>(-R&MASK)));
};

template<uint32_t, unsigned int R>
uint32_t LeftRotate<uint32_t, unsigned int>(uint32_t v)
{
  __asm__ ("roll %1, %0" : "+mq" (v) : "I" ((unsigned char)R));
  return v;
}

#if __x86_64__
template<uint64_t, unsigned int R>
uint64_t LeftRotate<uint64_t, unsigned int>(uint64_t v)
{
  __asm__ ("rolq %1, %0" : "+mq" (v) : "J" ((unsigned char)R));
  return v;
}
#endif

int main(int argc, char* argv[])
{
  std::cout << "Rotated: " << LeftRotate<uint32_t, 2>((uint32_t)argc) << std::endl;
  return 0;
}

我已经经历了多次错误消息迭代,具体取决于我尝试实现旋转的方式。 Othr错误消息包括 no function template matches function template specialization... 。使用template <>似乎产生了最难以理解的一个。

我如何参数化移位量,希望Clang和VC ++能按预期优化函数调用?

2 个答案:

答案 0 :(得分:2)

另一种方法是将模板化常量转换为编译器可以优化的常量参数。

第1步:定义rotate_distance的概念:

template<unsigned int R> using rotate_distance = std::integral_constant<unsigned int, R>;

第2步:根据函数的重载定义旋转函数,该函数采用此类型的参数:

template<unsigned int R>
uint32_t LeftRotate(uint32_t v, rotate_distance<R>)

现在,如果我们希望我们可以简单地调用LeftRotate(x, rotate_distance<y>()),这似乎很好地表达了意图,

或者我们现在可以根据以下形式重新定义2参数模板表单:

template<unsigned int Dist, class T>
T LeftRotate(T t)
{
  return LeftRotate(t, rotate_distance<Dist>());
}

完整演示:

#include <iostream>
#include <stdint.h>
#include <utility>

template<unsigned int R> using rotate_distance = std::integral_constant<unsigned int, R>;

template<typename T, unsigned int R>
inline T LeftRotate(unsigned int v, rotate_distance<R>)
{
  static const unsigned int THIS_SIZE = sizeof(T)*8;
  static const unsigned int MASK = THIS_SIZE-1;
  return T((v<<R)|(v>>(-R&MASK)));
}

template<unsigned int R>
uint32_t LeftRotate(uint32_t v, rotate_distance<R>)
{
  __asm__ ("roll %1, %0" : "+mq" (v) : "I" ((unsigned char)R));
  return v;
}

#if __x86_64__
template<unsigned int R>
uint64_t LeftRotate(uint64_t v, rotate_distance<R>)
{
  __asm__ ("rolq %1, %0" : "+mq" (v) : "J" ((unsigned char)R));
  return v;
}
#endif


template<unsigned int Dist, class T>
T LeftRotate(T t)
{
  return LeftRotate(t, rotate_distance<Dist>());
}

int main(int argc, char* argv[])
{
  std::cout << "Rotated: " << LeftRotate((uint32_t)argc, rotate_distance<2>()) << std::endl;
  std::cout << "Rotated: " << LeftRotate((uint64_t)argc, rotate_distance<2>()) << std::endl;
  std::cout << "Rotated: " << LeftRotate<2>((uint64_t)argc) << std::endl;
  return 0;
}

pre-c ++ 11编译器

在c ++ 11之前,我们没有std :: integral_constant,所以我们必须制作自己的版本。

就我们的目的而言,这已足够:

template<unsigned int R> struct rotate_distance {};

完整证明 - 请注意优化的效果:

https://godbolt.org/g/p4tsQ5

答案 1 :(得分:1)

使用模板类,而不是模板函数:

#include <iostream>
#include <stdint.h>


template<typename T, unsigned int R>
struct LeftRotate {
    static inline T compute(T v)
    {
        static const unsigned int THIS_SIZE = sizeof(T)*8;
        static const unsigned int MASK = THIS_SIZE-1;
        return T((v<<R)|(v>>(-R&MASK)));
    }
};


template<unsigned int R>
struct LeftRotate<uint32_t, R> {
    static inline uint32_t compute(uint32_t v)
    {
        __asm__ ("roll %1, %0" : "+mq" (v) : "I" ((unsigned char)R));
        return v;
    }
};

#if __x86_64__
template<unsigned int R>
struct LeftRotate<uint64_t, R> {
    static inline uint64_t compute(uint64_t v)
    {
        __asm__ ("rolq %1, %0" : "+mq" (v) : "J" ((unsigned char)R));
        return v;
    }
};
#endif

int main(int argc, char* argv[])
{
  std::cout << "Rotated: " << LeftRotate<uint32_t, 2>::compute((uint32_t)argc) << std::endl;
  return 0;
}