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]
Other format: [Raw text]

Re: Where does the C standard describe overflow of signedintegers?


> Matthew Woodcraft writes:
>> Paul Schlie wrote:
>>As optimization seems to be a non-argument, as by analogy all
>>optimizations which are available for unsigned arithmetic are
>>correspondingly available for signed integer operations; as any signed
>>value may then be thought of as being unsigned for the purposes of
>>computation and/or comparison.
>
> What about optimising x*2/2 to x?

Given that "C" requires the above be evaluated as (x*2)/2, as the language
specifies that the syntax defines the precedence of the operations, and that
no optimization should alter the behavior as specified by the program; I'd
say that unless it was known that the value range of x was between 0
and INT_MAX, the optimization is simply invalid.

As programmers should know and often rely on the finite range of integers to
intentionally specify algebraically inconsistent transforms; which is why
various precision integers, and in the infinite precision libraries are
available for use when such overflow ambiguities are not acceptable or
desired; the complier should simply do what is asked, not pretend it knows
better, because it doesn't.

However it seems quite reasonable and desirable for the compiler to provide
feedback to the programmer, indicating that the expression specified may be
portably algebraically simplified to "x", if the negative value overflow
behavior was not intentionally desired; thereby enabling the programmer to
improve both the portability and performance of their specified program,
rather than assuming that a value altering optimization is desirable (which
in general it never is, and typically only leads to difficult to diagnose
problems, as the program isn't actually doing an equivalent of what was
specified).



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