[PATCH] Optimize abs(x) < 0.0

Roger Sayle roger@eyesopen.com
Sun Jul 21 10:04:00 GMT 2002


> Now computer hardware is forced to project that three valued logic onto a
> two valued logical "true, false" dichotomy.  The solution chosen is to have
> both comparisons above return "false".  That means that to check for a NaN
> and use a > or <= comparison, you have to use *both*, else the NaN will
> just escape your check.

When applying a code transformation it is sufficient to guarantee that
the behaviour of the transformed code is the same as the original.
In this case, I don't have to explicitly "check for a NaN" with two
comparisons iff the optimized code produces the same results in the
presence of NaNs and finite values.

Your suggestion of requiring two tests to check for a NaN amounts to

	(x<0.0) || (x>=0.0)? finite_case : NaN_case

where finite_case of "abs(x) < 0.0" always returns false, and the
NaN_case "abs(NaN) < 0.0" is "NaN < 0.0" which is also prescribed
false in the mapping to a two valued logic.  So the entire expression
(foo? false : false) can be optimized to false.

So yes, NaN escapes my checks, but the it escapes the correct way.

[You might also appreciate that "x == x" is a better explicit check
for finite vs. NaN that would require only a single comparison]

Roger
--



More information about the Gcc-patches mailing list