This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] -ffinite-math-only A-A


On 8/22/07, Christian BRUEL <christian.bruel@st.com> wrote:>
> Hello,
>
> I believe that ieee floating point A-A can be optimized to 0 under
> -ffinite-math-only.
>
> so on i686 the code
>
> float
> foo(float x)
> {
>    return (x-x);
> }
>
> now compiles with -O2 -ffinite-math-only into
>
> foo:
>          pushl   %ebp
>          popl    %ebp
>          fldz
>          ret
>
> instead of
>
> foo:
>          pushl   %ebp
>          movl    %esp, %ebp
>          flds    8(%ebp)
>          popl    %ebp
>          fsub    %st(0), %st
>
>
> Additional note :
> The sequence (x-x) is common and is also used in the newlib to generate
> NaN with (x-x)/(x-x). So do_divide in real.c is now able to recognize
> 0/0 and emit a NaN constant instead of 1.0 (fixing a bug in sh4 newlib
> build where -ffinite-math-only is set by default).

The patch is wrong:

> 2007-07-16  Christian Bruel  <christian.bruel@st.com>
>
>         * fold-const.c (fold_binary): Optimize A-A->0 if -ffinite-math-only.
>         * simplify_rtx (simplify_binary_operation_1): Likewise.
>
> boostraped and regtested on i686-pc-linux-gnu
> regtested on sh-superh-elf (defaults -ffinite-math-only)
>
> Best Regards,
>
> Christian
>
>
> Index: fold-const.c
> ===================================================================
> --- fold-const.c        (revision 127666)
> +++ fold-const.c        (working copy)
> @@ -10152,7 +10152,8 @@
>        if ((! FLOAT_TYPE_P (type)
>            || (flag_unsafe_math_optimizations

  ^^^^
this test is the restricting one

>                && !HONOR_NANS (TYPE_MODE (type))
> -              && !HONOR_INFINITIES (TYPE_MODE (type))))
> +              && !HONOR_INFINITIES (TYPE_MODE (type)))
> +          || flag_finite_math_only)

!HONOR_NANS && !HONOR_INFINITIES is exactly flag_finite_math_only.

See also http://gcc.gnu.org/wiki/GeertBosch for some good overview on this
matters.  So the correct fix would be to remove the
flag_unsafe_math_optimizations
check instead.  (And check what IEEE says to -Inf - -Inf and Inf -
Inf, just in case
Geert is right with that only !HONOR_NANS is required).

Richard.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]