From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1255 invoked by alias); 29 Nov 2004 23:54:30 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 969 invoked from network); 29 Nov 2004 23:54:15 -0000 Received: from unknown (HELO avtrex.com) (67.116.42.149) by sourceware.org with SMTP; 29 Nov 2004 23:54:15 -0000 Received: from avtrex.com ([192.168.0.111] RDNS failed) by avtrex.com with Microsoft SMTPSVC(5.0.2195.6713); Mon, 29 Nov 2004 15:54:14 -0800 Message-ID: <41ABB6B2.7020502@avtrex.com> Date: Tue, 30 Nov 2004 00:24:00 -0000 From: David Daney User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4.1) Gecko/20031030 MIME-Version: 1.0 To: Morten Welinder CC: gcc@gcc.gnu.org Subject: Re: INT_MIN % -1 References: <20041129215914.A03FC1422D5B@darter.rentec.com> In-Reply-To: <20041129215914.A03FC1422D5B@darter.rentec.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 29 Nov 2004 23:54:14.0391 (UTC) FILETIME=[BAEAC070:01C4D66E] X-SW-Source: 2004-11/txt/msg01179.txt.bz2 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.