public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Where does the C standard describe overflow of signed integers?
@ 2005-07-11 14:58 Nicholas Nethercote
  2005-07-11 15:07 ` Dave Korn
                   ` (3 more replies)
  0 siblings, 4 replies; 28+ messages in thread
From: Nicholas Nethercote @ 2005-07-11 14:58 UTC (permalink / raw)
  To: gcc

Hi,

There was recently a very long thread about the overflow behaviour of 
signed integers in C.  Apparently this is undefined according to the C 
standard.  I searched the standard on this matter, and while I did find 
some paragraphs that described how unsigned integers must wrap around upon 
overflow, I couldn't find anything explicit about signed integers.  Can 
someone point me to the relevant part(s) of the standard?

Also, does anyone know what the required behaviour for Fortran integers is 
on overflow?

(I realise this isn't exactly on-topic for this list, but I thought it 
reasonable to ask since this topic was discussed so enthusiastically 
recently :)

Thanks very much.

Nick

^ permalink raw reply	[flat|nested] 28+ messages in thread
* RE: Where does the C standard describe overflow of signed integers?
@ 2005-07-14  1:10 Paul Schlie
  2005-07-14  1:59 ` Robert Dewar
  2005-07-14 17:57 ` Matthew Woodcraft
  0 siblings, 2 replies; 28+ messages in thread
From: Paul Schlie @ 2005-07-14  1:10 UTC (permalink / raw)
  To: GCC Development

> " ... A computation involving unsigned operands can never overflow, because
> a  result that cannot be represented by the resulting unsigned integer type
> is reduced modulo the number that is one greater than the largest value that
> can be represented by the resulting type."

Although I don't intend to extend the debate; doesn't anyone find it curious
that given this hard requirement, combined with the fact that all current
machine architectures rely on 2's complement signed integer representation
to eliminate the otherwise necessity for distinct signed integer arithmetic
operations; that by extension unsigned and signed integer arithmetic
operations are behaviorally equivalent all current machine implementations
(as well as likely future implementations for the same reasons); therefore
seemingly irrational and counter productive to presume otherwise, regardless
of the standard's presently relatively ambiguous position on the subject for
apparently largely historical reasons.

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. i.e.:

  signed: 0 .. INT_MAX  INT_MIN ..    -1    0 ...
unsigned: 0 ..    UINT_MAX/2    .. UINT_MAX 0 ...


^ permalink raw reply	[flat|nested] 28+ messages in thread
* Re: Where does the C standard describe overflow of signed integers?
@ 2005-07-14 19:09 Paul Schlie
  2005-07-14 19:13 ` Robert Dewar
  0 siblings, 1 reply; 28+ messages in thread
From: Paul Schlie @ 2005-07-14 19:09 UTC (permalink / raw)
  To: Matthew Woodcraft; +Cc: gcc

> 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).


^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2005-07-16 14:26 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-11 14:58 Where does the C standard describe overflow of signed integers? Nicholas Nethercote
2005-07-11 15:07 ` Dave Korn
2005-07-11 16:07   ` Nicholas Nethercote
2005-07-11 17:04     ` Dave Korn
2005-07-11 15:15 ` Nathan Sidwell
2005-07-11 15:23   ` Dave Korn
2005-07-11 15:18 ` Overflow in Fortran (was: Where does the C standard describe overflow of signed integers?) Paul Brook
2005-07-12 23:13 ` Where does the C standard describe overflow of signed integers? Michael Meissner
2005-07-14  1:10 Paul Schlie
2005-07-14  1:59 ` Robert Dewar
2005-07-14  5:28   ` Paul Schlie
2005-07-14 17:57 ` Matthew Woodcraft
2005-07-14 18:36   ` Paul Koning
2005-07-14 19:09 Paul Schlie
2005-07-14 19:13 ` Robert Dewar
2005-07-14 19:28   ` Paul Schlie
2005-07-14 19:33     ` Robert Dewar
2005-07-14 20:13       ` Paul Schlie
2005-07-15 13:20         ` Georg Bauhaus
2005-07-15 13:33           ` Georg Bauhaus
2005-07-15 14:31           ` Dave Korn
2005-07-16 12:04             ` Georg Bauhaus
2005-07-16 14:26               ` Paul Schlie
2005-07-15 15:03           ` Paul Schlie
2005-07-16 12:12             ` Georg Bauhaus
2005-07-14 20:35     ` Paul Koning
2005-07-14 21:58       ` Paul Schlie
2005-07-15  7:04         ` Avi Kivity

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).