函数指针的分支预测 [英] branch prediction on a function pointer

查看:88
本文介绍了函数指针的分支预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环反复运行.该循环内的逻辑取决于程序所处的模式.为了提高性能,我一直在考虑可以初始化函数指针数组functionPtr [],这样就可以调用运行正确逻辑的functionPtrmode.循环将以相同的模式保持多个周期(该数目在前期未知,但有数千个).该程序仅在intel x64计算机上运行,​​并且不需要可移植性.

I have a loop that is running over and over again. The logic inside that loop is dependent on the mode that the program is in. To improve performance I was thinking that an array of function pointers can be initialized, functionPtr[], so that would just call functionPtrmode that runs the right logic. The loop will stay in the same mode for many cycles (the number is unknown upfront but many thousands). The program runs on an intel x64 machine only and needs no portability.

我希望CPU使用分支预测,但是由于我的分支不是有条件的(在汇编级别上),但是分支的位置确实取决于变量(functionPtr + mode).CPU是否会尝试计算functionPtr + mode并开始在管道中提取这些指令?

I was hoping that the CPU would utilize branch prediction but since my branch isn't conditional (on the assembly level) but the location of the branch does depend on a variable, (functionPtr+mode). Will the CPU attempt to calculate functionPtr+mode and start pulling those instructions in while in the pipeline?

推荐答案

是的,合理的最新处理器可以为间接跳转做(至少类似的)分支预测.

Yes, reasonably recent processors can do (at least something like) branch prediction for indirect jumps.

从奔腾(英特尔第一个进行分支预测的人)到第一个奔腾IV,所有用于间接分支的都是分支目标缓冲区(BTB).这意味着当(且仅当)目标与先前的目标完全相同时,他们才能正确预测"这样的分支-听起来很适合您的情况.

From the Pentium (Intel's first to do branch prediction) through the first Pentium IV's, all that was used for indirect branches was the Branch Target Buffer (BTB). This meant that they "predicted" such branches correctly when (and only when) the target was exactly identical to the previous target--which sounds like it's adequate for your case.

从Pentium M/Prescott(最后一个Pentium IV)开始,Intel改进了间接跳转的分支预测,以使用两级自适应预测器.如果我正确地理解了您的问题(即您的循环将在多个连续的迭代中使用相同的目标执行,而这正是您所关心的),那么即使BTB也足以满足您的目的.如果(例如)您在连续数字的最低有效位上进行分支,则二级预测器将变得更加有用,因此您具有一种可预测的模式,可以在一个迭代中跳到一个目标,而在下一个迭代中跳到另一个目标.使用这样的模式,仅BTB总是会错误地预测分支,但是当前处理器中的二级预测器将正确地预测(在前几次迭代之后,因此可以检测到该模式).

Starting with the Pentium M/Prescott (the last Pentium IV) Intel improved branch prediction for indirect jumps to use a two-level adaptive predictor. If I'm understanding your question correctly (i.e., your loop will execute with the same target for many consecutive iterations, and those are what you care about) even just the BTB would be sufficient for your purposes. The two level predictor would become more useful if (for example) you were branching on the least significant bit of consecutive numbers, so you had a predictable pattern of jumping to one target in one iteration, and the other in the next iteration. With a pattern like this, the BTB alone would always predict the branch incorrectly, but the two-level predictor in a current processor would predict correctly (after the first couple of iterations, so the pattern could be detected).

这篇关于函数指针的分支预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆