public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/112600] New: Failed to optimize saturating addition using __builtin_add_overflow
@ 2023-11-17 21:40 redi at gcc dot gnu.org
  2023-11-17 21:41 ` [Bug middle-end/112600] " redi at gcc dot gnu.org
                   ` (14 more replies)
  0 siblings, 15 replies; 16+ messages in thread
From: redi at gcc dot gnu.org @ 2023-11-17 21:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112600

            Bug ID: 112600
           Summary: Failed to optimize saturating addition using
                    __builtin_add_overflow
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

These two implementations of C++26 saturating addition (std::add_sat<unsigned>)
have equivalent behaviour:

unsigned
add_sat(unsigned x, unsigned y) noexcept
{
    unsigned z;
    if (!__builtin_add_overflow(x, y, &z))
            return z;
    return -1u;
}

unsigned
add_sat2(unsigned x, unsigned y) noexcept
{
    unsigned res;
    res = x + y;
    res |= -(res < x);
    return res;
}


For -O3 on x86_64 GCC uses a branch for the first one:

add_sat(unsigned int, unsigned int):
        add     edi, esi
        jc      .L3
        mov     eax, edi
        ret
.L3:
        or      eax, -1
        ret

For the second one we get better code:

add_sat2(unsigned int, unsigned int):
        add     edi, esi
        sbb     eax, eax
        or      eax, edi
        ret



Clang compiles them both to the same code:

add_sat(unsigned int, unsigned int):
        add     edi, esi
        mov     eax, -1
        cmovae  eax, edi
        ret

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

end of thread, other threads:[~2024-06-11 16:59 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-17 21:40 [Bug middle-end/112600] New: Failed to optimize saturating addition using __builtin_add_overflow redi at gcc dot gnu.org
2023-11-17 21:41 ` [Bug middle-end/112600] " redi at gcc dot gnu.org
2023-11-17 21:45 ` redi at gcc dot gnu.org
2023-11-19 21:43 ` pinskia at gcc dot gnu.org
2023-11-20  9:23 ` rguenth at gcc dot gnu.org
2024-01-22 12:46 ` tnfchris at gcc dot gnu.org
2024-05-16 12:09 ` cvs-commit at gcc dot gnu.org
2024-05-16 12:09 ` cvs-commit at gcc dot gnu.org
2024-05-17 14:57 ` cvs-commit at gcc dot gnu.org
2024-05-18  2:17 ` cvs-commit at gcc dot gnu.org
2024-06-05  8:38 ` cvs-commit at gcc dot gnu.org
2024-06-05 20:42 ` ubizjak at gmail dot com
2024-06-06 17:45 ` cvs-commit at gcc dot gnu.org
2024-06-08 10:19 ` cvs-commit at gcc dot gnu.org
2024-06-09 10:10 ` cvs-commit at gcc dot gnu.org
2024-06-11 16:59 ` cvs-commit at gcc dot gnu.org

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).