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

--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <[email protected]>:

https://gcc.gnu.org/g:de3ad7ed6d0244e87d4154f53bbd0aa64ca9175d

commit r17-7157-gde3ad7ed6d0244e87d4154f53bbd0aa64ca9175d
Author: Jakub Jelinek <[email protected]>
Date:   Fri Dec 4 09:23:26 2026 +0100

    match.pd: Fix up (FTYPE) N CMP CST optimization for -frounding-math
[PR129024]

    This optimization among other things queries the range of the
    integer (casted to floating point), converts those bounds to
    floating point and if CST is above/below those bounds it folds it
    to false or true:
            /* Optimize cases when CST is outside of ITYPE's range.  */
            (if (real_compare (LT_EXPR, cst, &imin))
             { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp
== NE_EXPR,
                                      type); })
            (if (real_compare (GT_EXPR, cst, &imax))
             { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp
== NE_EXPR,
                                      type); })
    The problem with that is that real_from_integer (&imin, fmt, imin_val,
isign);
    and real_from_integer (&imax, fmt, imax_val, isign); round to nearest.
    Which is fine for -fno-rounding-math, but wrong with -frounding-math.
    In that case, we should assume the worst (note, imin and imax aren't used
    anywhere but in the above two ifs after being computed), and so if
    the integer to real conversion isn't exact, we need to lower imin by 1ulp
    and/or incrase imax by 1ulp.

    The following patch adds an extra argument to real_from_integer, defaulting
to
    rnd_even where the caller can ask how to round it if it is not exact, and
    for flag_rounding_math asks for rounding to -inf for imin and +inf for
imax.

    2026-12-04  Jakub Jelinek  <[email protected]>

            PR tree-optimization/129024
            * real.h (enum real_round_kind): New type.
            (real_from_integer): Add another argument defaulted to rnd_even.
            * real.cc (real_from_integer): Add a new argument defaulted to
            rnd_even.  If it is not rnd_even, round the result if it is not
            exact.
            * match.pd ((FTYPE) N CMP CST): Take -frounding-math into account
            when computing imin and imax.

            * c-c++-common/pr129024.c: New test.

    Reviewed-by: Richard Biener <[email protected]>

Reply via email to