分支预测错误 [英] Branch mispredictions

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

问题描述

这个问题可能很愚蠢,但我还是会问。

我从 Mysticial的答案

,我想知道以下情况是否可能发生

This question may be silly but i will ask it anyway.
I've heard about branch prediction from this Mysticial's answer
and i want to know if it is possible for the following to happen

说我有这段C ++代码

Lets say i have this piece of C++ code

while(memoryAddress = getNextAddress()){

  if(haveAccess(memoryAddress))
    // change the value of *memoryAdrress
  else 
    // do something else

}

因此,如果分支预测变量在某些情况下错误地预测了如果语句为true,则程序更改* memoryAddress的值会不好吗?
是否会发生分段错误之类的事情?

So if the branch predictor predicts wrongly in some case that the if statement is true and then the program change the value of *memoryAddress can bad happen out of that? Can things like segmentation fault happen?

推荐答案

处理器内部的分支预测变量设计为没有功能上可观察到的效果

The branch predictor inside a processor is designed to have no functionally observable effects.

分支预测器不够完善,无法每次都正确使用,无论尝试如何欺骗您。如果每次都是正确的,那只会是分支始终被执行的方式,而不会成为预测变量。

The branch predictor is not sophisticated enough to get it right every time, regardless of attempts to trick it such as yours. If it was right everytime, it would just be how branches are always executed, it wouldn't be a "predictor".

分支的条件仍在计算时执行继续,并且如果条件证明没有预期值,则不会将任何内容提交给内存。执行将返回到正确的分支。

The condition of the branch is still computed while execution continues, and if the condition turns out not have the predicted value, nothing is committed to memory. Execution goes back to the correct branch.

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

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