[英]Is it possible to aid branch prediction?
Can you intentionally write code in a specific way so that the branch predictor will choose the option that will be the case most of the times. 您是否可以有意识地以特定方式编写代码,以便分支预测器将选择大多数情况下的选项。 For example error checks whether a resource was loaded.
例如,错误检查资源是否已加载。 If this is possible how can you use this to your advantage?
如果可能的话,你如何利用这个优势?
If you are using GCC you can use the macros `likely()/unlikely()': 如果你正在使用GCC,你可以使用宏`possible()/ impossible()':
likely()/unlikely() macros in the Linux kernel - how do they work? Linux内核中的可能()/不可能()宏 - 它们如何工作? What's their benefit?
他们的好处是什么?
Theoretically, yes. 从理论上讲,是的。 Effectively speaking NO.
有效地说NO。 You won't really get any benefit, try it out yourself.
你不会真正得到任何好处,自己尝试一下。
With the way modern hardware works your CPU will still grind out all of the branches no matter what you do. 随着现代硬件的运行方式,无论您做什么,您的CPU仍然会磨掉所有分支。 But it doesn't really matter because they will do it concurrently.
但这并不重要,因为他们会同时做到这一点。
To attempt to do it yourself you would need to use assembly language. 要尝试自己动手,您需要使用汇编语言。 Compiler hints like shown above will not do much.
上面显示的编译器提示不会做太多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.