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

            Bug ID: 95914
           Summary: Failure to optimize saturated add properly
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

uint16_t add16(uint16_t a, uint16_t b)
{
    uint16_t c = a + b;
    return c < a ? -1 : c;
}

With -O3, LLVM outputs this on x86 :

add16(unsigned short, unsigned short):
  add di, si
  mov eax, 65535
  cmovae eax, edi
  ret

GCC outputs this :

add16(unsigned short, unsigned short):
  xor eax, eax
  add si, di
  jc .L8
.L2:
  test ax, ax
  mov eax, -1
  cmove eax, esi
  ret
.L8:
  mov eax, 1
  jmp .L2

I don't know if this doesn't also occur on also architectures but the output
seems rather crippled on x86, at least.

Reply via email to