public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Robert Dewar <dewar@gnat.com>
To: Paul Schlie <schlie@comcast.net>
Cc: gcc@gcc.gnu.org
Subject: Re: INT_MIN % -1
Date: Tue, 30 Nov 2004 07:02:00 -0000	[thread overview]
Message-ID: <41ABF8C8.7040108@gnat.com> (raw)
In-Reply-To: <BDD14F36.8169%schlie@comcast.net>

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.

  reply	other threads:[~2004-11-30  4:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-30  4:25 Paul Schlie
2004-11-30  7:02 ` Robert Dewar [this message]
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
  -- strict thread matches above, loose matches on Subject: below --
2004-11-29 23:38 Morten Welinder
2004-11-30  0:24 ` David Daney
2004-11-30  0:45 ` Andreas Schwab

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=41ABF8C8.7040108@gnat.com \
    --to=dewar@gnat.com \
    --cc=gcc@gcc.gnu.org \
    --cc=schlie@comcast.net \
    /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).