Parameterization and “function template partial specialization is not allowed”

丶灬走出姿态 提交于 2019-12-01 21:52:32

Another way is to turn the templated constant into a constant argument which the compiler can optimise away.

step 1: define the concept of a rotate_distance:

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

step 2: define the rotate functions in terms of overloads of a function which takes an argument of this type:

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

Now, if we wish we can simply call LeftRotate(x, rotate_distance<y>()), which seems to express intent nicely,

or we can now redefine the 2-argument template form in terms of this form:

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

Full Demo:

#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 compilers

Prior to c++11 we didn't have std::integral_constant, so we have to make our own version.

For our purposes, this is sufficient:

template<unsigned int R> struct rotate_distance {};

full proof - note the effect of optimisations:

https://godbolt.org/g/p4tsQ5

Use a template class, rather than a template function:

#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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!