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

            Bug ID: 126692
           Summary: Fold hypot compared with zero
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: easyhack, missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ktkachov at gcc dot gnu.org
  Target Milestone: ---

hypot never returns less than the larger magnitude of its arguments, and
it returns +Inf whenever either is infinite, so it is zero exactly when
both arguments are zero.  The comparison therefore does not need the
call:

  hypot (x, y) == 0  ->  x == 0 & y == 0
  hypot (x, y) != 0  ->  x != 0 | y != 0

A nonzero argument cannot underflow the result to zero, since the true
value is at least the smallest subnormal and C requires hypot to compute
it without undue underflow.  Dropping the call loses the ERANGE an
overflowing hypot may report, so it needs the right errno flags

int f (double x, double y) { return __builtin_hypot (x, y) == 0.0; }
Generates at -Ofast:
f:
        stp     x29, x30, [sp, -16]!
        mov     x29, sp
        bl      hypot
        fcmp    d0, #0.0
        ldp     x29, x30, [sp], 16
        cset    w0, eq
        ret

but could avoid the hypot call altogether

Reply via email to