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

            Bug ID: 94893
           Summary: Sign function not getting optimized to simple compare
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

inline int sign(int x)
{
    return (x >> 31) | ((unsigned)-x >> 31);
}

bool f(int x)
{
    return sign(x) < 1;
}

With -O3, LLVM outputs :

f(int):
  test edi, edi
  setle al
  ret

GCC outputs :

f(int):
  mov eax, edi
  sar edi, 31
  neg eax
  shr eax, 31
  or eax, edi
  setle al
  ret

Reply via email to