简体   繁体   English

使用非类型模板参数的C ++优化

[英]C++ optimization with a Non-Type Template Parameter

I am not sure is there any sense in this case bits to be a non-type template: 我不确定在这种情况下bit是否会成为非类型模板:

template< int bits > 
inline bool FitsInBits(int x )
{
#if bits >= 32
    #error 'bits' should be less than 32
#endif
    return x >= -((int)1<<(bits-1)) && x < ((int)1<<(bits-1));
}

Rather then: 而不是:

inline bool FitsInBits(int x, int bits )
{
    return x >= -((int)1<<(bits-1)) && x < ((int)1<<(bits-1));
}

As I understand the compiler will create many variants of FitsInBits in the first case at compile time? 据我了解,编译器会在编译时的第一种情况下创建FitsInBits的许多变体吗? But I don't see how this will optimize the calculation. 但是我不知道这将如何优化计算。

The template isn't necessarily more efficient, but it assures you that the values of: -((int)1<<(bits-1)) and ((int)1<<(bits-1)) can be computed at compile time, so all that happens at run-time is basically: 该模板不一定会有效,但可以向您保证: -((int)1<<(bits-1))((int)1<<(bits-1))可以按编译时,因此在运行时发生的所有事情基本上是:

return x >= constant_a && x < constant_b;

This, in turn, is sufficiently trivial that there's a pretty good chance the compiler can/will generate inline-code for it. 反过来,这是微不足道的,以至于编译器很有可能会/将为其生成内联代码。

Assuming that bits is a value known at compile time, the non-template version can (and probably will) do the same--but since bits is a normal parameter to a normal function, you could (perhaps accidentally) pass some value for bits that wasn't known until run time (eg, based on data entered by the user). 假设bits是在编译时已知的值,则非模板版本可以(并且可能会)执行相同的操作,但是由于bits是常规函数的常规参数,因此您可能 (偶然地)传递了一些bits值直到运行时才知道(例如,基于用户输入的数据)。

If you do so, the compiler can't/won't give you a warning about having done so--and in this case, the expressions probably won't reduce to constants as above, so you'd very likely end up with code to load 1 into one register, load bits into another, decrement the second, shift the first left by the number of places specified in the second, compare to x , conditionally jump to a label, negate the first, compare again, do another conditional jump, etc. This is still only around a dozen instructions (or so), but undoubtedly slower than when the values are constants. 如果这样做,编译器将不会/不会警告您这样做-在这种情况下,表达式可能不会像上面那样简化为常量,因此您很可能最终得到代码将1装入一个寄存器,将bits装入另一个寄存器,递减第二个寄存器,将第一个寄存器左移第二个寄存器中指定的位数,与x进行比较,有条件地跳转到标签,取反第一个,再次比较,再执行另一个条件跳转等。这仍然只有十几条指令(或大约),但无疑比值是常数时要慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM