Branch mispredictions

2019-06-10 19:36发布

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

Lets say i have this piece of C++ code

while(memoryAddress = getNextAddress()){

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

}

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?

1条回答
霸刀☆藐视天下
2楼-- · 2019-06-10 20:20

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.

查看更多
登录 后发表回答