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

Re: optimization request: (-a)*(-b) => a*b


Sylvain Pion wrote:
> 
> While I am at it, I also think the following kind of tests can be optimized:
> (-a)>0  => a<0
> 
> Am I right ?  Is it doable ?
Depends on what the type and representation of a is.

If it is a floating point type with ieee representation, there is a +0 and a
-0. ieee semantics are a bit strange here and there is some interaction with
rounding modes IIR. It depends on how closely your trying to get ieee
semantics.

If a is an unsigned integral type, -a >0 -> a != 0.

If a is a signed integral type, it depends on the semantics defined for signed
integral overflow. The problem is we're dealing with bounded integers, and
we've defined what happens when the mathematically correct result lies outside
the representable range. Although the standard makes this implementation
defined for signed integral types, most (all?) gcc implementationd define
signed integral arithmetic as wrapping (just as unsigned arithmetic is mandated
to). The problem with transforming (-a)>0 into a<0 is that mostneg int is
tranformed incorrectly. For a 32bit 2's complement system, this has a as value
-2^31, the negation operation gives the same value result (by truncating the
true result, and then reinterpreting the bit pattern), unfortunately. So for
this value (-(-2^31))>0 is false, yet your proposed transform (-2^31)<0 is
true.

If you're happy changing the semantics of overflowing signed arithmetic from
implentation defined to undefined, then the suggested optimization is ok.

nathan
-- 
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
      You can up the bandwidth, but you can't up the speed of light      
nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk


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