public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/98801] New: Request for a conditional move built-in function
@ 2021-01-23  4:47 jeffhurchalla at gmail dot com
  2021-01-23  6:53 ` [Bug c++/98801] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: jeffhurchalla at gmail dot com @ 2021-01-23  4:47 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-05-27 17:56 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-23  4:47 [Bug c++/98801] New: Request for a conditional move built-in function jeffhurchalla at gmail dot com
2021-01-23  6:53 ` [Bug c++/98801] " pinskia at gcc dot gnu.org
2021-01-23 19:37 ` jeffhurchalla at gmail dot com
2021-01-23 20:28 ` jeffhurchalla at gmail dot com
2021-01-25  9:36 ` [Bug middle-end/98801] " rguenth at gcc dot gnu.org
2021-01-25 19:19 ` peter at cordes dot ca
2021-01-26  0:17 ` jeffhurchalla at gmail dot com
2021-01-27  5:18 ` jeffhurchalla at gmail dot com
2023-05-27 17:56 ` richard.yao at alumni dot stonybrook.edu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).