From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14027 invoked by alias); 30 Nov 2004 04:36:26 -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 14018 invoked from network); 30 Nov 2004 04:36:22 -0000 Received: from unknown (HELO nile.gnat.com) (205.232.38.5) by sourceware.org with SMTP; 30 Nov 2004 04:36:22 -0000 Received: from localhost (localhost [127.0.0.1]) by filtered-nile.gnat.com (Postfix) with ESMTP id A23E69626; Mon, 29 Nov 2004 23:36:22 -0500 (EST) Received: from nile.gnat.com ([127.0.0.1]) by localhost (nile.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 14911-01-7; Mon, 29 Nov 2004 23:36:22 -0500 (EST) Received: from [127.0.0.1] (taconic.gnat.com [205.232.38.103]) by nile.gnat.com (Postfix) with ESMTP id 68F36961F; Mon, 29 Nov 2004 23:36:22 -0500 (EST) Message-ID: <41ABF8C8.7040108@gnat.com> Date: Tue, 30 Nov 2004 07:02:00 -0000 From: Robert Dewar User-Agent: Mozilla Thunderbird 0.8 (Windows/20040913) MIME-Version: 1.0 To: Paul Schlie Cc: gcc@gcc.gnu.org Subject: Re: INT_MIN % -1 References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at nile.gnat.com X-SW-Source: 2004-11/txt/msg01193.txt.bz2 Paul Schlie wrote: > (where I'm assuming: (-INT_MIN / -1) => -1 isn't reasonably correct or > necessary, nor is a run-time exception/trap) > > Although possibly naive, I'd like to think that the C standard won't be used > as a crutch, but that GCC may rise above many of the unspecified behaviors, > and establish instead, well defined logically consistent useful ones, which > others may aspire to emulate. One possibility would be to have an optional divide by zero trap that sets the right result and continues. Then there is a special compiler switch that sets up this trap routine if you really really really want this not very useful marginal behavior (a real division by zero is undefined, so it is fine to do anything you like). That being said, GNAT (the Ada front end) does go to the trouble of doing this right. Given the source program: > procedure K is > X, Y : Integer; > function Id (X : Integer) return Integer is begin return X; end Id; > begin > X := Id (Integer'First); > Y := Id ( - 1); > X := X mod y; > end; (the Id function is to stop the compiler from doing optimizing value tracing :-) The generated code (-gnatG output) (with checks off to simplifty) looks like: procedure k is x : integer; y : integer; function k__id (x : integer) return integer is begin return x; end k__id; begin x := k__id (-16#8000_0000#); y := k__id (-1); x := (if y = -1 then 0 else x mod y); return; end k; The test for y = -1 is precisely to handle this case, and it is only one test. Note that the cost in Ada is generally smaller than in C, because we know more about the range of variables. For example, the Ada program: procedure K is X, Y : Integer range -1000 .. +1000; function Id (X : Integer) return Integer is begin return X; end Id; begin X := Id (1); Y := Id ( - 1); X := X mod y; end; Does not generate the check for -1, because it knows that X cannot be Integer'First.