Branch mispredictions

烂漫一生 提交于 2019-12-08 10:10:17

问题


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:


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.



来源:https://stackoverflow.com/questions/19141414/branch-mispredictions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!