On Wed, 3 Oct 2018, Jeff Law wrote:
+/* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
+ (for coss (COS)
+ atans (ATAN)
+ sqrts (SQRT)
+ (simplify
+ (coss (atans:s @0))
+ (rdiv {build_one_cst (type);}
+ (sqrts (plus (mult @0 @0) {build_one_cst (type);})))))
Don't we have the same kinds of issues with the x*x in here? As X gets
large it will overflow, but the result is going to be approaching zero.
So we might be able to use a similar trick here.
(I have not read the patch)
The similar trick would say that for X large, this is the same as 1/abs(X)
I guess. Note that it may be simpler to generate a call to hypot (C99).
A related remark would be: with the precision of double, for x>=cst (about
2^53), atan(x) is constant, within .5 ulp of pi/2 if the math library is
super precise (which it probably isn't). Returning 0 for its cos (what
happens if x*x gives +Inf) is thus completely fine unless you are using
crlibm, but then you wouldn't use flag_unsafe_math_optimizations. The main
issue is that if we have -ffast-math, we have -ffinite-math-only, and we
are possibly introducing an infinity as intermediate result...
--
Marc Glisse