gpt4 book ai didi

c++ - 参数化和 "function template partial specialization is not allowed"

转载 作者:太空狗 更新时间:2023-10-29 21:14:55 25 4
gpt4 key购买 nike

这是 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;
}

根据我尝试实现轮换的方式,我经历了多次错误消息迭代。其他错误消息包括 no function template matches function template specialization... 。使用 template <>似乎产生了最难以理解的一个。

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

最佳答案

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

第 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;
}

C++11 之前的编译器

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

为了我们的目的,这就足够了:

template<unsigned int R> struct rotate_distance {};

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

https://godbolt.org/g/p4tsQ5

关于c++ - 参数化和 "function template partial specialization is not allowed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39314690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com