public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Gabriel Dos Reis <gdr@integrable-solutions.net>
To: Nathan Sidwell <nathan@codesourcery.com>
Cc: Michael Veksler <VEKSLER@il.ibm.com>, gcc@gcc.gnu.org
Subject: Re: Do C++ signed types have modulo semantics?
Date: Mon, 27 Jun 2005 15:17:00 -0000	[thread overview]
Message-ID: <m38y0vzv6q.fsf@uniton.integrable-solutions.net> (raw)
In-Reply-To: <42C00EBF.6040507@codesourcery.com>

Nathan Sidwell <nathan@codesourcery.com> writes:

| Gabriel Dos Reis wrote:
| 
| > But a compiler could define them to be modulo -- that is the whole
| > point.  The paragraph does not say they don't "modulo".
| 
| of course it could do so, but then to be useful it should document it,
| and it would be an extension.

We're in violent agreement there.

| > | 18.2.1.2/57 claims is_modulo is true 'for signed types on most
| > | machines'.  Such an assertion is false when optimizations rely the
| > | undefinedness of signed overflow.  A DR should probably be filed
| > | (maybe one is, I'm not at all familiar with library DRs).
| > Well, undefined behaviour does not mean unconditional hell or  evil.
| > It is just behaviour left up to the compiler to whatever it wants.
| 
| correct.  However the std *itself* says in one place 'this is
| undefined' and in another place 'this is usually modulo'. I find that
| confusing at best.

well, you could send a message to -lib.  The standard does not
prescribe "this is always true", so saying "it is usually true" does
not contradict previous statement that it is undefined -- it is just
as vague.

| > And all useful programs we write rely on undefined behaviour of one
| > sort or the other, starting with GCC.  In the case of
| 
| They do?

Well, just check out GCC for starters :-)

| I thought they usually relied on implementation defined,
| documented extensions or were part of the implementation.  Now I'm
| sure you'll prove me wrong in some way or other, but please stick to
| the point -- do real important programs that must not break and cannot
| be changed rely on signed modulo behaviour?

I don't think that is a useful question, because first we would need
to agree on what is considered "real important programs" when and
where they could or should be changed.

I think what we should address is 

  (1) Can we make it useful, not just left to random blue moon?
  (2) can it be done in a reasonable way?

Just saying, "ah but the standard sys it is undefined behaviour" does
nto sound to me as a satisfying answer.  It one takes the standards
literally, then GCC is not required to be useful; just conformant.
But then you just need "cp /bin/sh gcc" and appropriate documentation.

| > When RTH helped cleanup the numeric_limits implementation in September
| > 2002, he made a very good point (which I held to be true, since
| > obviously he is The Middle-end and Back-end Guy)
| >    http://gcc.gnu.org/ml/libstdc++/2002-09/msg00207.html
| 
| thanks for that.  I was under the impression that some of the loop
| optimizers relied on the fact that iterating over a signed type did
| not do odd modulo related things. 

yes, but in the case of the *concrete* targets supported by GCC, do
you know of any that "do odd modulo related things"?  
GCC's documentation GCC/gcc/doc/implement-c.texi says:

   GCC supports only two's complement integer types, and all bit patterns
   are ordinary values.

And, in the case of 2's complement, the relation between the min, max of
signed integer type is well-defined.  It is not left up to the will of
the Go'aulds.

|  Indeed this comment in loop.c
| concerning BIVs leads me to believe we already fail to honor the
| is_modulo value
| 
|     Note that treating the entire pseudo as a BIV will result in making
|     simple increments to any GIVs based on it.  However, if the variable
|     overflows in its declared mode but not its promoted mode, the result will
|     be incorrect.  This is acceptable if the variable is signed, since
|     overflows in such cases are undefined, but not if it is unsigned, since
|     those overflows are defined.  So we only check for SIGN_EXTEND and
|     not ZERO_EXTEND.

As I said earlier, we do have a consistency problem in GCC and this is
an area where we would be nore useful in improving things.  I do not
consider lengthy discussions exemplified bt late VRP thing very
useful.  It does not really help much arguing "but it is undefined
behaviour".  Yes, we can define it to something useful when we can
(and we should).

| Anyway, this doesn't answer Michael's question.  He asked whether C
| and C++ differ in this regard.  The answer is the standards are the
| same, and the implementation is the same (because it is the same
| backend). 

With the difference that C++ gives users a hook to ask for more information.
And that makes a real difference -- as opposed to a mere "undefined
behaviour".

| So, if whatever optimizations he is turning on change the
| behaviour rth cited, then limits should change too.

As I said, I'm for consistent semantics.  The question remains as
whether we should reflect reality -- the arithmetic is modulo -- or
just leave in the abstract tower of "standard says it is undefined
behaviour". 

| I don't particularly care what behaviour is chosen, except that
| 
| 1) C and C++ implementations should behave the same way
| 
| 2) we should pick the behaviour that leads to better code generation in real life.

We can generate better code if we don't care about semantics and
usefulness.  Just compile the program as if it was

   int main() { } 

| 3) if modulo behaviour is chosen, it should be well documented in a
| place more prominant than type_traits<int>::is_modulo.

I'm all for more documentation and if you could help there, it would
be appreciated.

-- Gaby

  parent reply	other threads:[~2005-06-27 15:17 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-27  5:09 Michael Veksler
2005-06-27 13:02 ` Nathan Sidwell
2005-06-27 14:10   ` Gabriel Dos Reis
2005-06-27 14:36     ` Nathan Sidwell
2005-06-27 14:47       ` Paul Koning
2005-06-27 15:21         ` Michael Veksler
2005-06-28  3:06           ` Mark Mitchell
2005-06-28  3:49             ` Gabriel Dos Reis
2005-06-28 10:08             ` Robert Dewar
2005-06-28 11:19               ` Steven Bosscher
2005-06-28 12:03                 ` Ulrich Weigand
2005-06-28 12:09                   ` Steven Bosscher
2005-06-29 16:46                     ` Steven Bosscher
2005-06-29 18:01                       ` Daniel Berlin
2005-06-29 18:32                         ` Steven Bosscher
2005-06-29 18:45                           ` Robert Dewar
2005-06-29 19:13                         ` Nicholas Nethercote
2005-06-28 12:16                 ` Paul Koning
2005-06-28 12:30                   ` Steven Bosscher
2005-06-28 12:35                     ` Michael Veksler
2005-06-28 12:38                       ` Steven Bosscher
2005-06-28 16:54             ` Joe Buck
2005-06-28 17:02               ` Mark Mitchell
2005-06-28 17:08                 ` Daniel Berlin
2005-06-28 18:12                   ` Mark Mitchell
2005-06-28 17:46                 ` Theodore Papadopoulo
2005-06-27 15:17       ` Gabriel Dos Reis [this message]
2005-06-30 20:53         ` Kai Henningsen
2005-06-27 14:39 Morten Welinder
2005-06-27 15:21 ` Gabriel Dos Reis
     [not found] <OF0E4366CA.5D54868B-ON4325702E.0036D351-4325702E.003973C5@il.ibm.com>
     [not found] ` <m3k6keeibo.fsf@uniton.integrable-solutions.net>
2005-06-28 13:29   ` Nathan Sidwell
2005-06-28 13:59     ` Gabriel Dos Reis
2005-06-28 14:05       ` Andrew Pinski
2005-06-28 14:13         ` Gabriel Dos Reis
2005-06-28 15:30           ` Michael Veksler
2005-06-28 15:49             ` Nathan Sidwell
2005-06-28 15:58               ` Michael Veksler
2005-06-28 16:07                 ` Andrew Pinski
2005-06-28 16:39                 ` Gabriel Dos Reis
2005-06-28 15:55             ` Gabriel Dos Reis
2005-06-28 16:47               ` Joseph S. Myers
2005-06-28 17:58                 ` Gabriel Dos Reis
2005-06-28 14:05       ` Nathan Sidwell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m38y0vzv6q.fsf@uniton.integrable-solutions.net \
    --to=gdr@integrable-solutions.net \
    --cc=VEKSLER@il.ibm.com \
    --cc=gcc@gcc.gnu.org \
    --cc=nathan@codesourcery.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).