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

# Re: [PATCH] Perform constant folding of math builtins

*From*: Brad Lucier <lucier at math dot purdue dot edu>
*To*: roger at eyesopen dot com (Roger Sayle)
*Cc*: lucier at math dot purdue dot edu (Brad Lucier), gcc-patches at gcc dot gnu dot org
*Date*: Mon, 26 Aug 2002 13:14:42 -0500 (EST)
*Subject*: Re: [PATCH] Perform constant folding of math builtins

> Hi Brad,
> > > sqrt(x)*sqrt(y) = sqrt(x*y)
> > > exp(log(x)) = x
> >
> > This is false for negative x, for obvious reasons. This could mask
> > a real error; I don't think it should be used.
>
> I was relying on the semantics of "-funsafe-math-optimizations", that
> to quote the documentation, "allows optimizations for floating-point
> arithmetic that assume the arguments and results are valid".
OK, I'm convinced that exp(log(x)) = x should be OK in -ffast-math.
Sorry for the noise.
> I don't suppose I could convince you this is a reasonable transformation
> at GCC's most aggressive level of numerical optimization, namely
> "-funsafe-math-optimizations"? I've had the misfortune to recently
> come across sqrt(x)*sqrt(y) in a "real world" example:
>
> > #define len(v) sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2])
> >
> > double dotprod(double *a, double *b)
> > {
> > return (a[0]*b[0] + a[1]*b[1] + a[2]*b[2])/(len(a)*len(b));
> > }
The transformation sqrt(x)*sqrt(y) -> sqrt(x*y) can take code that computes
a perfectly good value (everything in range, etc.), and transforms it to code
that computes nonsense, if x*y overflows or underflows to zero and
sqrt(x)*sqrt(y) does not.
Should -funsafe-math-optimizations do this to code that the author may
have carefully written to avoid this? Perhaps not.
Brad