public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Daney <ddaney@avtrex.com>
To: Morten Welinder <terra@gnome.org>
Cc: gcc@gcc.gnu.org
Subject: Re: INT_MIN % -1
Date: Tue, 30 Nov 2004 00:24:00 -0000	[thread overview]
Message-ID: <41ABB6B2.7020502@avtrex.com> (raw)
In-Reply-To: <20041129215914.A03FC1422D5B@darter.rentec.com>

Morten Welinder wrote:
> What is the value of  INT_MIN % -1  supposed to be, assuming 32-bit
> ints and two-complement representation.
> 
> C99 seems to be telling us two things about '%':
> 
> (1) It computes remainder for integer division.
> (2) "If the quotient  a/b  is representable, the expression
>     (a/b)*b + a%b  shall equal  a."
> 
> (2) is not useful for the case since the quotient is not representable.  I'd
> say that (1) means that the result should be zero, but that would be rather
> expensive to implement as you would have to test for -1 at runtime on i386,
> for example.
> 
> I would claim that there is no overflow in the calculation: both arguments
> and the result are perfectly representable as 32-bit ints.  The fact that
> the quotient of the same values, if performed, overflows is no more relevant
> that the fact that you can't take the square root of the two sides.
> 
> gcc 3.4 on solaris/sparc seems to get zero; gcc 3.3.1 on linux gives me a
> crash at runtime.  (Because the signed integer division instruction traps
> as documented.)
> 
> Comments?
> 
> Morten
> 
> 
> 
> -----------------------------------------------------------------------------
> 
> 
>>uname -a
> 
> SunOS troll 5.8 Generic_117350-11 sun4u sparc
> 
>>./a.out 
> 
> -2147483648 % -1 (const) = 0
> -2147483648 % -1 (non-const) = 0
> 
> -----------------------------------------------------------------------------
> 
> 
>>uname -a
> 
> Linux darter 2.4.21-243-default #1 Thu Aug 12 15:22:14 UTC 2004 i686 i686 i386 GNU/Linux
> 
>>./a.out 
> 
> -2147483648 % -1 (const) = 0
> Floating point exception
> 
> -----------------------------------------------------------------------------

/junk # uname -a
Linux (none) 2.4.25-Avtrex #152 Mon Nov 15 10:51:36 PST 2004 mips unknown
/junk # ./bla
-2147483648 % -1 (const) = 0
-2147483648 % -1 (non-const) = 0

Intel chose to trap.  Sun and MIPS chose not to.

I don't have the spec. in front of me, but I can imagine that it allows for
this.

If you want well defined behavior use something like java.  The JLS
specifies exactly what should happen in this case.

David Daney.

  reply	other threads:[~2004-11-29 23:54 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-29 23:38 Morten Welinder
2004-11-30  0:24 ` David Daney [this message]
2004-11-30  0:45 ` Andreas Schwab
2004-11-30  4:25 Paul Schlie
2004-11-30  7:02 ` Robert Dewar
2004-11-30 22:47   ` Paul Schlie
2004-12-01  0:39     ` Robert Dewar
2004-12-01  3:22       ` Paul Schlie
2004-12-01 11:40         ` Robert Dewar

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=41ABB6B2.7020502@avtrex.com \
    --to=ddaney@avtrex.com \
    --cc=gcc@gcc.gnu.org \
    --cc=terra@gnome.org \
    /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).