日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Purpose of cmove instruction in x86 assembly? | cmove 指令如何避免错误的分支预测带来的开销?

發布時間:2024/2/28 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Purpose of cmove instruction in x86 assembly? | cmove 指令如何避免错误的分支预测带来的开销? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

cmove 指令是用來做什么的?(Purpose of cmove instruction in x86 assembly?)

The purpose of cmov is to allow software (in some cases) to avoid a branch.

For example, if you have this code:

cmp eax,ebxjne .l1mov eax,edx .l1:

…then when a modern CPU sees the jne branch it will take a guess about whether the branch will be taken or not taken, and then start speculatively executing instructions based on the guess. If the guess is wrong there’s a performance penalty, because the CPU has to discard any speculatively executed work and then start fetching and executing the correct path.

For a conditional move (e.g. cmove eax,edx) the CPU doesn’t need to guess which code will be executed and the cost of a mispredicted branch is avoided. However, the CPU can’t know if the value in eax will change or not, which means that later instructions that depend on the results of the conditional move have to wait until the conditional move completes (instead of being speculatively executed with an assumed value and not stalling). 我的理解是,它只是通過不去預測這種方式,來避免預測錯了重來。就像你只要不寫代碼,就不會有bug。

This means that if the branch can be easily predicted a branch can be faster; and if the branch can’t be easily predicted the condition move can be faster.

Note that a conditional move is never strictly needed (it can always be done with a branch instead) - it’s more like an optional optimization.

參考:條件數據傳送(Conditional Data Transfer)

Reference: CSAPP Section 5.12 - Understanding Memory Performance

需要指出的是,不是所有的條件行為都能用條件數據傳送來實現,所以無可避免地在某些情況中,程序員會寫出導致條件分支的代碼,而對于這些條件分支,處理器用分支預測可能會處理得很糟糕。

總結

以上是生活随笔為你收集整理的Purpose of cmove instruction in x86 assembly? | cmove 指令如何避免错误的分支预测带来的开销?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。