Portable branch prediction hints(便携式分支预测提示)
问题描述
是否有任何可移植的方式进行分支预测提示?考虑以下示例:
Is there any portable way of doing branch prediction hints? Consider the following example:
if (unlikely_condition) {
/* ..A.. */
} else {
/* ..B.. */
}
这与做有什么不同吗:
if (!unlikely_condition) {
/* ..B.. */
} else {
/* ..A.. */
}
或者是使用编译器特定提示的唯一方法?(例如 GCC 上的 __builtin_expect)
Or is the only way to use compiler specific hints? (e.g. __builtin_expect on GCC)
编译器是否会根据条件的顺序对 if
条件进行任何不同的处理?
Will compilers treat the if
conditions any differently based on the ordering of the conditions?
推荐答案
进行静态分支预测的规范方法是 if
被预测为非分支(即每个 if
> 子句被执行,而不是 else
),并采用循环和向后goto
.因此,如果您希望静态预测很重要,请不要将常见情况放在 else
中.绕过一个未采取的循环并不容易.我从来没有尝试过,但我想把它放在一个 else
子句中应该可以很好地移植.
The canonical way to do static branch prediction is that if
is predicted not-branched (i.e. every if
clause is executed, not else
), and loops and backward-goto
s are taken. So, don't put the common case in else
if you expect static prediction to be significant. Getting around an untaken loop isn't as easy; I've never tried but I suppose putting it an an else
clause should work pretty portably.
许多编译器支持某种形式的#pragma unroll
,但仍然需要用某种#if
来保护它以保护其他编译器.
Many compilers support some form of #pragma unroll
, but it will still be necessary to guard it with some kind of #if
to protect other compilers.
分支预测提示理论上可以表达一个完整的描述如何转换程序的流程控制图和如何排列可执行内存中的基本块……所以有各种各样的东西要表达,而且大多数不会很便携.
Branch prediction hints can theoretically express a complete description of how to transform a program's flow-control graph and arrange the basic blocks in executable memory… so there are a variety of things to express, and most won't be very portable.
正如 GNU 在 __builtin_expect
的文档中所推荐的那样,配置文件引导的优化优于提示,而且工作量更少.
As GNU recommends in the documentation for __builtin_expect
, profile-guided optimization is superior to hints, and with less effort.
这篇关于便携式分支预测提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:便携式分支预测提示
基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01