This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[gfortran,patch] Integer constants overflowing type with -fno-range-check
- From: "François-Xavier Coudert" <fxcoudert at gmail dot com>
- To: "fortran at gcc dot gnu dot org" <fortran at gcc dot gnu dot org>, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Tue, 20 Mar 2007 18:24:58 +0100
- Subject: [gfortran,patch] Integer constants overflowing type with -fno-range-check
Hi all,
Attached patch fixes PR31262: when an integer constant overflows its
type, it can still be accepted if -fno-range-check is used. However,
the function gfc_conv_mpz_to_tree() in trans-const.c had an assumption
that constants were always inside their type range.
This removes the assumption by allowing the integer constant
arbitrarily large. The patch was regtested on i686-linux, and I will
commit it with a testcase along the lines of:
integer :: a
integer(kind=8) :: b
a = -3
b = -huge(b) / 7
a = a ** 73
b = 7894_8 * b - 78941_8
if ((-3)**73 /= a) call abort()
if (7894_8 * (-huge(b) / 7) - 78941_8 /= b) call abort()
end
OK for mainline? It's not a regression, so I won't backport it to 4.2
unless someone feels strongly about it.
FX
:ADDPATCH fortran:
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];