https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98801
Bug ID: 98801 Summary: Request for a conditional move built-in function Product: gcc Version: 10.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: jeffhurchalla at gmail dot com Target Milestone: --- There are a number of idiomatic ways to hint to gcc to generate a conditional move, but there is no way that is documented/guaranteed. In practice, they do not always result in a conditional move. I would like to request a builtin function that generates a conditional move. The current situation: Perhaps the most common hint is to write a = (cond) ? x : y Another way is to bit-hack, and hope the compiler transforms it to cmov. E.g. a = ((-cond) & x) | ((cond-1) & y) There are more ways still, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93165 . None can be relied on for the purpose. Certainly a conditional move can be accomplished via inline asm, but this is non-portable across ISAs, it's bug-prone, and it seems to hinder the optimizer. The existing function __builtin_expect_with_probability is interesting: in theory it seems like it might allow a user to get conditional moves via a probability of 0.5. I'm unsure if this works as desired in practice; its documentation does not mention anything about conditional moves or unpredictable branches. (Also note that a probability of 0.5 does not necessarily mean a condition is unpredictable - e.g. a condition could alternate between true and false each time it's executed.) Perhaps also interesting is clang's __builtin_unpredictable, which is documented as a way to indicate to the compiler that a branch condition is unpredictable. I'm unsure here too if this affects conditional moves; their docs don't mention it. Personally, I'd like conditional moves for performance in situations where I know a branch is completely unpredictable. However, cmovs appear to also be useful for security, to avoid timing attacks.