public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/38504]  New: double minus sign when printing integer?
@ 2008-12-12 13:15 kloedej at knmi dot nl
  2008-12-12 13:58 ` [Bug fortran/38504] " dfranke at gcc dot gnu dot org
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: kloedej at knmi dot nl @ 2008-12-12 13:15 UTC (permalink / raw)
  To: gcc-bugs

There seems to be a small bug when writing an 8 byte integer to a character
variable, as shown by this little test program:

program IntAdtest

  integer, parameter :: i8_ = Selected_Int_Kind(18)  ! = integer*8
  character(len=22) :: str_value
  integer(i8_) :: value
  character(len=*), parameter :: format_IntAd  = "(i22)"

  value = -9223372036854775807_i8_
  write(str_value, format_IntAd) value
  print *,"str_value = ["//str_value//"]"

  value = -9223372036854775807_i8_ -1
  write(str_value, format_IntAd) value
  print *,"str_value = ["//str_value//"]"

end program IntAdtest

When compiled with: gfortran -o IntAdtest IntAdtest.F90
I get the following output for the gfortran compiled executable:

>IntAdtest
 str_value = [  -9223372036854775807]
 str_value = [ --9223372036854775808]

For all several other compilers the result is as expected:

>IntAdtest
 str_value = [  -9223372036854775807]
 str_value = [  -9223372036854775808]

I used the following gfortran version for this test:

gfortran --version
GNU Fortran (GCC) 4.4.0 20081021 (experimental) [trunk revision 141258]
Copyright (C) 2008 Free Software Foundation, Inc.

best regards,

Jos de Kloe


-- 
           Summary: double minus sign when printing integer?
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: kloedej at knmi dot nl


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
@ 2008-12-12 13:58 ` dfranke at gcc dot gnu dot org
  2008-12-12 14:48 ` kloedej at knmi dot nl
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: dfranke at gcc dot gnu dot org @ 2008-12-12 13:58 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from dfranke at gcc dot gnu dot org  2008-12-12 13:55 -------
Sort of confirmed. You are aware that 'value-1' corresponds to
'-HUGE(value)-1', which is out of range for integer numbers of that kind?

Reduced testcase:

IntAdtest
  integer, parameter :: i8_ = Selected_Int_Kind(18)  ! = integer*8
  integer(i8_) :: value

  value = -9223372036854775807_i8_
  write(*, FMT="(i22)") value, -HUGE(value)

  value = -9223372036854775807_i8_ -1
  write(*, FMT="(i22)") value, -HUGE(value)-1
end program IntAdtest

Adding Jerry as CC.


-- 

dfranke at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dfranke at gcc dot gnu dot
                   |                            |org, jvdelisle at gcc dot
                   |                            |gnu dot org
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2008-12-12 13:55:55
               date|                            |


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
  2008-12-12 13:58 ` [Bug fortran/38504] " dfranke at gcc dot gnu dot org
@ 2008-12-12 14:48 ` kloedej at knmi dot nl
  2008-12-12 15:17 ` kargl at gcc dot gnu dot org
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: kloedej at knmi dot nl @ 2008-12-12 14:48 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from kloedej at knmi dot nl  2008-12-12 14:47 -------
(In reply to comment #1)
> Sort of confirmed. You are aware that 'value-1' corresponds to
> '-HUGE(value)-1', which is out of range for integer numbers of that kind?

Thanks for your reply.
Yes I am aware that defining an integer constant with value '-HUGE(value)-1' is
not allowed, but as far as I know the variable type is allowed to contain that
value.
So if: 
   value = -9223372036854775807_i8_ -1
is illegal, then something like:
   value = -9223372036854775807_i8_
   value = value-1
should be legal, and still be within the range of an 8 byte integer.
See also this discussion:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29580

Anyway, the point is that gfortran does accept this code, but if you write to a
character variable the result is strange.

Best regards,

Jos de Kloe


-- 


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
  2008-12-12 13:58 ` [Bug fortran/38504] " dfranke at gcc dot gnu dot org
  2008-12-12 14:48 ` kloedej at knmi dot nl
@ 2008-12-12 15:17 ` kargl at gcc dot gnu dot org
  2008-12-12 16:00 ` dfranke at gcc dot gnu dot org
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: kargl at gcc dot gnu dot org @ 2008-12-12 15:17 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from kargl at gcc dot gnu dot org  2008-12-12 15:15 -------
(In reply to comment #1)
> Sort of confirmed. You are aware that 'value-1' corresponds to
> '-HUGE(value)-1', which is out of range for integer numbers of that kind?
> 

( Ihaven't had my morning coffe, but ...)

No. It is in range.  It is the only way (other than TRANSFER) to 
get the most negative integer value in a simple assignment.  Now, 
if you write

  value =  9223372036854775807_i8_ + 1
  write(*, FMT="(i22)") value, HUGE(value)+1

This results in out of range values, and wrap around semantics 
still gives

 --9223372036854775808
 --9223372036854775808

Also, note that if one uses integer(4), you get the right output.

           -2147483647
           -2147483647
           -2147483648
           -2147483648


-- 


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
                   ` (2 preceding siblings ...)
  2008-12-12 15:17 ` kargl at gcc dot gnu dot org
@ 2008-12-12 16:00 ` dfranke at gcc dot gnu dot org
  2008-12-12 16:04 ` kargl at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: dfranke at gcc dot gnu dot org @ 2008-12-12 16:00 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from dfranke at gcc dot gnu dot org  2008-12-12 15:59 -------
> > Sort of confirmed. You are aware that 'value-1' corresponds to
> > '-HUGE(value)-1', which is out of range for integer numbers of that kind?
>
> No. It is in range.

Loose wording on my side, s/range/model/ then :)
*hands over a cup of morning coffee*


-- 


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
                   ` (3 preceding siblings ...)
  2008-12-12 16:00 ` dfranke at gcc dot gnu dot org
@ 2008-12-12 16:04 ` kargl at gcc dot gnu dot org
  2008-12-13 11:20 ` burnus at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: kargl at gcc dot gnu dot org @ 2008-12-12 16:04 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from kargl at gcc dot gnu dot org  2008-12-12 16:03 -------
Looking at the -fdump-tree-original, I suspect that the minus sign in
the hex formatted number is redundant.  To keep things short, I've removed
the dt_parm setup code. _gfortran_transfer_integer probably outputs -,
then converts 0x80000000000000 to -9223....

  value = -0x8000000000000000;
  {
    struct __st_parameter_dt dt_parm.2;

    _gfortran_transfer_integer (&dt_parm.2, &value, 8);
    {
      static integer(kind=8) C.1495 = -0x8000000000000000;

      _gfortran_transfer_integer (&dt_parm.2, &C.1495, 8);
    }
    _gfortran_st_write_done (&dt_parm.2);
  }

  value4 = -2147483648;
  {
    struct __st_parameter_dt dt_parm.4;

    _gfortran_st_write (&dt_parm.4);
    _gfortran_transfer_integer (&dt_parm.4, &value4, 4);
    {
      static integer(kind=4) C.1499 = -2147483648;

      _gfortran_transfer_integer (&dt_parm.4, &C.1499, 4);
    }
    _gfortran_st_write_done (&dt_parm.4);
  }


-- 


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
                   ` (4 preceding siblings ...)
  2008-12-12 16:04 ` kargl at gcc dot gnu dot org
@ 2008-12-13 11:20 ` burnus at gcc dot gnu dot org
  2008-12-13 15:33 ` jvdelisle at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: burnus at gcc dot gnu dot org @ 2008-12-13 11:20 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from burnus at gcc dot gnu dot org  2008-12-13 11:19 -------
For completeness, on my x86-64-linux system I get with 4.4.0, 4.3.0 and with
openSUSE binaries (4.1, 4.2, 4.3, 4.4) always a single "-".

Hmm, OK found something: With -m32 I get "--" but with -m64 (default) I get
"-", maybe it helps.


-- 


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
                   ` (5 preceding siblings ...)
  2008-12-13 11:20 ` burnus at gcc dot gnu dot org
@ 2008-12-13 15:33 ` jvdelisle at gcc dot gnu dot org
  2008-12-13 15:43 ` jvdelisle at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: jvdelisle at gcc dot gnu dot org @ 2008-12-13 15:33 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from jvdelisle at gcc dot gnu dot org  2008-12-13 15:32 -------
I have also found on pr38472 some odd behaviour at -m32. That we see this with
an integer output has my concern level up.


-- 


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
                   ` (6 preceding siblings ...)
  2008-12-13 15:33 ` jvdelisle at gcc dot gnu dot org
@ 2008-12-13 15:43 ` jvdelisle at gcc dot gnu dot org
  2008-12-13 16:29 ` jvdelisle at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: jvdelisle at gcc dot gnu dot org @ 2008-12-13 15:43 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from jvdelisle at gcc dot gnu dot org  2008-12-13 15:42 -------
Reduced test case. Fails at -m32

program IntAdtest

  integer, parameter :: i8_ = Selected_Int_Kind(18)  ! = integer*8
  integer(i8_) :: value

  value = -9223372036854775807_i8_ -1
  write(*, '(i22)') value

end program IntAdtest

-fdump-tree-original looks clean


-- 


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
                   ` (7 preceding siblings ...)
  2008-12-13 15:43 ` jvdelisle at gcc dot gnu dot org
@ 2008-12-13 16:29 ` jvdelisle at gcc dot gnu dot org
  2008-12-13 19:36 ` jvdelisle at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: jvdelisle at gcc dot gnu dot org @ 2008-12-13 16:29 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from jvdelisle at gcc dot gnu dot org  2008-12-13 16:28 -------
I have isolated the problem to gfc_itoa.  gfc_itoa returns a string. The number
of digits is determined from the strlen of that string.  At -m32, that string
length (digits, see below) is off by one. I instrumented write_decimal in
write.c to see this.  I now will dig into gfc_itoa.

$ gfc -m64 pr38504.f90
$ ./a.out 
sign=1
w=22, m=-1, digits=19, nsign=1
  -9223372036854775808
$ gfc -m32 pr38504.f90 
$ ./a.out 
sign=1
w=22, m=-1, digits=20, nsign=1
 --9223372036854775808


-- 

jvdelisle at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |jvdelisle at gcc dot gnu dot
                   |dot org                     |org
             Status|NEW                         |ASSIGNED
   Last reconfirmed|2008-12-12 13:55:55         |2008-12-13 16:28:33
               date|                            |


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
                   ` (8 preceding siblings ...)
  2008-12-13 16:29 ` jvdelisle at gcc dot gnu dot org
@ 2008-12-13 19:36 ` jvdelisle at gcc dot gnu dot org
  2008-12-14  6:53 ` jvdelisle at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: jvdelisle at gcc dot gnu dot org @ 2008-12-13 19:36 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from jvdelisle at gcc dot gnu dot org  2008-12-13 19:34 -------
Testing this.

Index: write.c
===================================================================
--- write.c     (revision 142574)
+++ write.c     (working copy)
@@ -600,9 +600,16 @@ write_decimal (st_parameter_dt *dtp, con
   sign = calculate_sign (dtp, n < 0);
   if (n < 0)
     n = -n;
-
   nsign = sign == S_NONE ? 0 : 1;
+  
+  /* conv calls gfc_itoa which sets the negative sign needed
+     by write_integer. The sign '+' or '-' is set below based on sign
+     calculated above, so we just point past the sign in the string
+     before proceeding to avoid double signs in corner cases.
+     (see PR38504)  */
   q = conv (n, itoa_buf, sizeof (itoa_buf));
+  if (*q == '-')
+    q++;

   digits = strlen (q);



-- 


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
                   ` (9 preceding siblings ...)
  2008-12-13 19:36 ` jvdelisle at gcc dot gnu dot org
@ 2008-12-14  6:53 ` jvdelisle at gcc dot gnu dot org
  2008-12-14  7:01 ` jvdelisle at gcc dot gnu dot org
  2008-12-14  7:03 ` jvdelisle at gcc dot gnu dot org
  12 siblings, 0 replies; 14+ messages in thread
From: jvdelisle at gcc dot gnu dot org @ 2008-12-14  6:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #11 from jvdelisle at gcc dot gnu dot org  2008-12-14 06:52 -------
Subject: Bug 38504

Author: jvdelisle
Date: Sun Dec 14 06:50:53 2008
New Revision: 142747

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=142747
Log:
2008-12-13  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

        PR libfortran/38504
        io/write.c (write_decimal): Skip extra sign '-' at beginning of string
        returned by gfc_itoa.

Modified:
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/io/write.c


-- 


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
                   ` (10 preceding siblings ...)
  2008-12-14  6:53 ` jvdelisle at gcc dot gnu dot org
@ 2008-12-14  7:01 ` jvdelisle at gcc dot gnu dot org
  2008-12-14  7:03 ` jvdelisle at gcc dot gnu dot org
  12 siblings, 0 replies; 14+ messages in thread
From: jvdelisle at gcc dot gnu dot org @ 2008-12-14  7:01 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #12 from jvdelisle at gcc dot gnu dot org  2008-12-14 07:00 -------
Subject: Bug 38504

Author: jvdelisle
Date: Sun Dec 14 06:58:38 2008
New Revision: 142748

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=142748
Log:
2008-12-13  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

        PR libfortran/38504
        * gfortran.dg/fmt_int_sign.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/fmt_int_sign.f90
Modified:
    trunk/gcc/testsuite/ChangeLog


-- 


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug fortran/38504] double minus sign when printing integer?
  2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
                   ` (11 preceding siblings ...)
  2008-12-14  7:01 ` jvdelisle at gcc dot gnu dot org
@ 2008-12-14  7:03 ` jvdelisle at gcc dot gnu dot org
  12 siblings, 0 replies; 14+ messages in thread
From: jvdelisle at gcc dot gnu dot org @ 2008-12-14  7:03 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #13 from jvdelisle at gcc dot gnu dot org  2008-12-14 07:01 -------
Fixed on trunk.


-- 

jvdelisle at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


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


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2008-12-14  7:03 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-12 13:15 [Bug fortran/38504] New: double minus sign when printing integer? kloedej at knmi dot nl
2008-12-12 13:58 ` [Bug fortran/38504] " dfranke at gcc dot gnu dot org
2008-12-12 14:48 ` kloedej at knmi dot nl
2008-12-12 15:17 ` kargl at gcc dot gnu dot org
2008-12-12 16:00 ` dfranke at gcc dot gnu dot org
2008-12-12 16:04 ` kargl at gcc dot gnu dot org
2008-12-13 11:20 ` burnus at gcc dot gnu dot org
2008-12-13 15:33 ` jvdelisle at gcc dot gnu dot org
2008-12-13 15:43 ` jvdelisle at gcc dot gnu dot org
2008-12-13 16:29 ` jvdelisle at gcc dot gnu dot org
2008-12-13 19:36 ` jvdelisle at gcc dot gnu dot org
2008-12-14  6:53 ` jvdelisle at gcc dot gnu dot org
2008-12-14  7:01 ` jvdelisle at gcc dot gnu dot org
2008-12-14  7:03 ` jvdelisle at gcc dot gnu dot org

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).