如何向 C++ 传达范围信息编译器?

发布于 2024-12-21 00:21:37 字数 435 浏览 2 评论 0原文

有没有什么方法可以向编译器表明您知道特定变量的值在代码中的某个点必须在特定范围内,以帮助编译器进行优化?我正在编写一个库,可以在编译时知道某些变量的范围,如果它能以某种方式将此信息传递给编译器,以便编译器可以使用它进行优化,那就太好了。我想添加对任何编译器的支持,即使它不能适用于所有编译器(这听起来像是某些编译器可以作为扩展的东西,但我还没有找到任何)。我知道我可以写这样的东西:

if(x < COMPILE_TIME_MIN or x > COMPILE_TIME_MAX)
    return;
// compiler will assume for code below that x is in range COMPILE_TIME_MIN..COMPILE_TIME_MAX

但这是运行时检查。也许有一些技巧可以让编译器在没有这样的检查的情况下对范围做出假设?

Is there any way to indicate to the compiler that you know the value of a particular variable must be within a particular range at a certain point in the code, to assist the compiler with optimizing? I'm writing a library that makes it possible to know the range of some variables at compile time, and it would be nifty if it could somehow communicate this information to the compiler so that the compiler could use it for optimization. I'd like to add support for any compilers where it would work even if it couldn't be made to work for all of them (it sounds like the sort of thing that some compilers could have as an extension, but I haven't found any). I know I could write something like this:

if(x < COMPILE_TIME_MIN or x > COMPILE_TIME_MAX)
    return;
// compiler will assume for code below that x is in range COMPILE_TIME_MIN..COMPILE_TIME_MAX

But that's a runtime check. Maybe there's some trick to get the compiler to make an assumption about the range without such a check?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女七分熟 2024-12-28 00:21:37

任何这样的“提示”都是特定于编译器的。

例如,Visual C++ 允许您使用 __assume 内在函数提供此类提示

(其他编译器也可能提供此类内在函数,但我对其他编译器不够熟悉,无法提供任何进一步的信息。如果您有兴趣,请参阅编译器的文档。)

Any such "hint" would be compiler-specific.

As an example, Visual C++ allows you to provide such a hint using the __assume intrinsic.

(Other compilers may also provide such intrinsics, but I am not familiar enough with other compilers to provide any further information. Consult your compiler's documentation if you are interested.)

猫腻 2024-12-28 00:21:37

它不是标准的,但对于 gcc,有一个名为 __builtin_expect 的命令,其中的宏定义为 likelyunlikely 来满足您的目的。请参阅此处,了解如何在内核中使用它们-space,但是 __builtin_expect 是一个 gcc 扩展,也可以在用户空间中使用(参见 这个问题),即使未定义可能不太可能

It's not standard, but with gcc, there comes a command called __builtin_expect, with macros defined as likely and unlikely that serve your purpose. See here for example which talks about using them in kernel-space, but __builtin_expect is a gcc extension and could be used in user-space also (see this question), even if likely and unlikely are not defined.

故人爱我别走 2024-12-28 00:21:37

我不知道有任何 C++ 编译技术可以利用这些信息,但我确实知道各种静态分析技术可以利用这些信息;向这些工具“告诉”某些内容的常见方法是通过 assert,例如:

assert(x > COMPILE_TIME_MIN);
assert(x < COMPILE_TIME_MAX);

但是通常这些工具也能够分析诸如“if 条件”之类的内容” 本身,所以没有特别需要这样做。

此外,如果范围确实很小,您还可以用较小的变量来表示它 - 例如使用短整型或字符 - 并添加 COMPILE_TIME_MIN。尽管我不知道编译本身,但这可以帮助此类工具。

最后,与所有优化方法一样,我建议首先对代码进行分析,看看这是否真的是一个瓶颈。此外,请记住,编译器是为优化“正常”代码而设计的 - 手动优化当然会有帮助,只需小心操作即可。

I don't know of any C++ compilation techniques that take advantage of this information, but I do know of various static analysis techniques that do; the common way to "tell" something to those tools would be via asserts, for instance:

assert(x > COMPILE_TIME_MIN);
assert(x < COMPILE_TIME_MAX);

But usually those tools would also be able to analyze things such as "if conditions" by themselves, so there's no special need to do so.

Additionally, if the range is really small, you can also represent it in a smaller-sized variable - e.g. using a short or char - and adding COMPILE_TIME_MIN. That can help such tools, though I don't know about the compilation itself.

And finally, as in all optimization approaches, I'd recommend first profiling your code to see if this could really be a bottleneck. Additionally, keep in mind that compilers are designed for optimizing "normal" code - hand-optimization could certainly help, just do it carefully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文