←back to thread

170 points judicious | 2 comments | | HN request time: 0s | source
Show context
mwkaufma ◴[] No.45408552[source]
Is cmov branchless, or just branching by another name?
replies(3): >>45409160 #>>45409795 #>>45410660 #
stevefan1999 ◴[] No.45409795[source]
That depends on how you define branch.

Say in Rust:

let foo = if bar { 1 } else { 2 };

And

let mut foo; if bar { foo = 1; } else { foo = 2; }

Despite they looked the same, functions the same and effectively the same, but the first one is the conditional move, and the second one would be a jump initially (until further compiler optimization kick in)

You will notice that for conditional move, you "get" a predictable expression for the result, but with branched jump, it's like you "get" a bunch of arbitrary statements, that writes to the expression. It may end up folding so both will essentially be compiled to cmov, but the way to representation of the assignment is different. You can be certain with conditional instructions, but you can't be certain with branched jump, otherwise we don't need branch prediction.

In fact, the way conditional instructions work is due to Church encoding, that you created a lambda function that calls the left or right function depending on the input evaluation, which can be seen as implicitly embedding the branch.

replies(1): >>45410126 #
1. mwkaufma ◴[] No.45410126[source]
Lotsa detail, but not to the point: does cmov depend on branch prediction or not?
replies(1): >>45410630 #
2. stevefan1999 ◴[] No.45410630[source]
No, predicated instructions like CMOV do not depend on branch prediction.