public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "fxcoudert at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/31262] -fno-range-check with large integer values triggers ICE
Date: Tue, 20 Mar 2007 09:27:00 -0000	[thread overview]
Message-ID: <20070320092711.24353.qmail@sourceware.org> (raw)
In-Reply-To: <bug-31262-10259@http.gcc.gnu.org/bugzilla/>



------- Comment #2 from fxcoudert at gcc dot gnu dot org  2007-03-20 09:27 -------
Here's a patch:

Index: trans-const.c
===================================================================
--- trans-const.c       (revision 123017)
+++ trans-const.c       (working copy)
@@ -165,23 +165,31 @@
     }
   else
     {
-      unsigned HOST_WIDE_INT words[2];
-      size_t count;
+      unsigned HOST_WIDE_INT *words;
+      size_t count, numb;

+      /* Determine the number of unsigned HOST_WIDE_INT that are required
+         for represent the value.  */
+      numb = 8*sizeof(HOST_WIDE_INT);
+      count = (mpz_sizeinbase (i, 2) + numb-1) / numb;
+      if (count < 2)
+       count = 2;
+      words = (unsigned HOST_WIDE_INT *) alloca (count *
sizeof(HOST_WIDE_INT));
+
       /* Since we know that the value is not zero (mpz_fits_slong_p),
         we know that at least one word will be written, but we don't know
         about the second.  It's quicker to zero the second word before
         than conditionally clear it later.  */
       words[1] = 0;
-
+      
       /* Extract the absolute value into words.  */
-      mpz_export (words, &count, -1, sizeof (HOST_WIDE_INT), 0, 0, i);
+      mpz_export (words, &count, -1, sizeof(HOST_WIDE_INT), 0, 0, i);

-      /* We assume that all numbers are in range for its type, and that
-        we never create a type larger than 2*HWI, which is the largest
-        that the middle-end can handle.  */
-      gcc_assert (count == 1 || count == 2);
-
+      /* We don't assume that all numbers are in range for its type.
+         However, we never create a type larger than 2*HWI, which is the
+        largest that the middle-end can handle. So, we only take the
+        first two elements of words, which is equivalent to wrapping the
+        value if it's larger than the type range.  */
       low = words[0];
       high = words[1];


And there's a small testcase, to see that the constant trees created for
overflowing values have the same value than the naturally overflowing
calculation done at run-time:

$ cat uu.f90 
  integer :: a
  integer(kind=8) :: b
  a = -3
  b = -huge(b) / 7
  a = a ** 73
  b = 7894_8 * b - 78941_8
  print *, (-3)**73, a
  print *, 7894_8 * (-huge(b) / 7) - 78941_8, b
  end
$ gfortran -fno-range-check uu.f90 && ./a.out
 -1534976995 -1534976995
  2635249153387000989  2635249153387000989


-- 

fxcoudert at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
      Known to fail|                            |4.1.3 4.2.0 4.3.0
            Summary|-fno-range-check can trigger|-fno-range-check with large
                   |ICE                         |integer values triggers ICE


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31262


  parent reply	other threads:[~2007-03-20  9:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-19 12:24 [Bug fortran/31262] New: -fno-range-check can trigger ICE fxcoudert at gcc dot gnu dot org
2007-03-19 12:25 ` [Bug fortran/31262] " fxcoudert at gcc dot gnu dot org
2007-03-19 12:25 ` fxcoudert at gcc dot gnu dot org
2007-03-20  9:27 ` fxcoudert at gcc dot gnu dot org [this message]
2007-03-22 22:52 ` [Bug fortran/31262] -fno-range-check with large integer values triggers ICE fxcoudert at gcc dot gnu dot org
2007-03-22 22:53 ` fxcoudert at gcc dot gnu dot org

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=20070320092711.24353.qmail@sourceware.org \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.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).