This is the mail archive of the 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] 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.


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