public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libfortran/19032] New: modulo generates wrong result
@ 2004-12-16 11:28 Thomas dot Koenig at online dot de
  2004-12-16 11:29 ` [Bug libfortran/19032] " Thomas dot Koenig at online dot de
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: Thomas dot Koenig at online dot de @ 2004-12-16 11:28 UTC (permalink / raw)
  To: gcc-bugs

$ cat mod-neg.f90
  integer :: a,b
  a = 2
  b = -1
  print *,modulo(2,-1)
  print *,modulo(a,b)
end
$ gfortran mod-neg.f90 && ./a.out
           0
          -1
$ gfortran -v
Reading specs from /home/zfkts/lib/gcc/ia64-unknown-linux-gnu/4.0.0/specs
Configured with: ../gcc-4.0-20041212/configure --prefix=/home/zfkts
--enable-languages=c,c++,f95 : (reconfigured) ../gcc-4.0-20041212/configure
--prefix=/home/zfkts --enable-languages=c,c++,f95 --disable-shared
Thread model: posix
gcc version 4.0.0 20041212 (experimental)

It's a bit strange that the first result is correct, and the
second one isn't.

-- 
           Summary: modulo generates wrong result
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libfortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Thomas dot Koenig at online dot de
                CC: gcc-bugs at gcc dot gnu dot org


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


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

* [Bug libfortran/19032] modulo generates wrong result
  2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
@ 2004-12-16 11:29 ` Thomas dot Koenig at online dot de
  2004-12-16 14:30 ` tobi at gcc dot gnu dot org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Thomas dot Koenig at online dot de @ 2004-12-16 11:29 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code


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


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

* [Bug libfortran/19032] modulo generates wrong result
  2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
  2004-12-16 11:29 ` [Bug libfortran/19032] " Thomas dot Koenig at online dot de
@ 2004-12-16 14:30 ` tobi at gcc dot gnu dot org
  2004-12-16 14:41 ` [Bug libfortran/19032] modulo generates wrong result for divisor -1 tobi at gcc dot gnu dot org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: tobi at gcc dot gnu dot org @ 2004-12-16 14:30 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From tobi at gcc dot gnu dot org  2004-12-16 14:30 -------
The second result is correct, the first wrong.

The difference results from the fact that the first statement is evaluated by
gfortran's constant folding pass, whereas the second is evaluated in generated code.

In other words, the code generation for modulo is wrong.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|                            |1
   Last reconfirmed|0000-00-00 00:00:00         |2004-12-16 14:30:16
               date|                            |


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


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

* [Bug libfortran/19032] modulo generates wrong result for divisor -1
  2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
  2004-12-16 11:29 ` [Bug libfortran/19032] " Thomas dot Koenig at online dot de
  2004-12-16 14:30 ` tobi at gcc dot gnu dot org
@ 2004-12-16 14:41 ` tobi at gcc dot gnu dot org
  2004-12-16 14:42 ` tobi at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: tobi at gcc dot gnu dot org @ 2004-12-16 14:41 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From tobi at gcc dot gnu dot org  2004-12-16 14:41 -------
I checked gfortran against the examples in the standard, and we seem to only get
the case modulo (..., -1) wrong (result should be 0, we print -1)

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|modulo generates wrong      |modulo generates wrong
                   |result                      |result for divisor -1


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


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

* [Bug libfortran/19032] modulo generates wrong result for divisor -1
  2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
                   ` (2 preceding siblings ...)
  2004-12-16 14:41 ` [Bug libfortran/19032] modulo generates wrong result for divisor -1 tobi at gcc dot gnu dot org
@ 2004-12-16 14:42 ` tobi at gcc dot gnu dot org
  2004-12-16 22:40 ` Thomas dot Koenig at online dot de
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: tobi at gcc dot gnu dot org @ 2004-12-16 14:42 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From tobi at gcc dot gnu dot org  2004-12-16 14:42 -------
(In reply to comment #1)
> The second result is correct, the first wrong.

It's the other way round, as might be obvious from comment #2
Sorry.


-- 


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


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

* [Bug libfortran/19032] modulo generates wrong result for divisor -1
  2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
                   ` (3 preceding siblings ...)
  2004-12-16 14:42 ` tobi at gcc dot gnu dot org
@ 2004-12-16 22:40 ` Thomas dot Koenig at online dot de
  2004-12-17  1:18 ` tobi at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Thomas dot Koenig at online dot de @ 2004-12-16 22:40 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From Thomas dot Koenig at online dot de  2004-12-16 22:40 -------
Reals are also broken:

$ cat mod-real.f90
program main
  real :: a,b
  a = 2.0
  b = -1.0
  print *,modulo(a,b)
end program main
$ gfortran mod-real.f90
$ ./a.out
  -1.000000

-- 


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


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

* [Bug libfortran/19032] modulo generates wrong result for divisor -1
  2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
                   ` (4 preceding siblings ...)
  2004-12-16 22:40 ` Thomas dot Koenig at online dot de
@ 2004-12-17  1:18 ` tobi at gcc dot gnu dot org
  2004-12-17 14:29 ` [Bug libfortran/19032] modulo generates wrong result for divisor 1 and -1 Thomas dot Koenig at online dot de
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: tobi at gcc dot gnu dot org @ 2004-12-17  1:18 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From tobi at gcc dot gnu dot org  2004-12-17 01:18 -------
A patch for integers is here: http://gcc.gnu.org/ml/fortran/2004-12/msg00171.html

I had guessed so Thomas, but I hoped that someone else would complete the fix :-)

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


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


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

* [Bug libfortran/19032] modulo generates wrong result for divisor 1 and -1
  2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
                   ` (5 preceding siblings ...)
  2004-12-17  1:18 ` tobi at gcc dot gnu dot org
@ 2004-12-17 14:29 ` Thomas dot Koenig at online dot de
  2004-12-20 19:08 ` tobi at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Thomas dot Koenig at online dot de @ 2004-12-17 14:29 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From Thomas dot Koenig at online dot de  2004-12-17 14:27 -------
I've adjusted the subject.

I've had a look at the real modulo and mod case,
but didn't quite understand it - it appeared to
be overly complicated, compared to the straightforward
formula a - floor(a/b)*b.  I'm probably missing something.

Also, here's a new test case.

$ cat mod-array.f90
program main
  integer, parameter :: n=16
  real, dimension(n) :: ar, br, modulo_result, floor_result
  integer, dimension(n) :: ai, bi , imodulo_result, ifloor_result

  ai(1:4) = 5
  ai(5:8) = -5
  ai(9:12) = 1
  ai(13:16) = -1
  bi(1:4) = (/ 3,-3, 1, -1/)
  bi(5:8) = bi(1:4)
  bi(9:12) = bi(1:4)
  bi(13:16) = bi(1:4)
  ar = ai
  br = bi
  modulo_result = modulo(ar,br)
  imodulo_result = modulo(ai,bi)
  floor_result = ar-floor(ar/br)*br
  ifloor_result = nint(real(ai-floor(real(ai)/real(bi))*bi))

  do i=1,n
     if (modulo_result(i) /= floor_result(i) ) then
        print "(A,4F5.0)" ,"real case failed: ", &
             ar(i),br(i), modulo_result(i), floor_result(i)
    end if
    if (imodulo_result(i) /= ifloor_result(i)) then
       print "(A,4I5)", "int case failed: ", &
            ai(i), bi(i), imodulo_result(i), ifloor_result(i)

     end if
  end do
end program main
$ gfortran mod-array.f90 && ./a.out
real case failed:    5.  -1.  -1.   0.
int case failed:     5   -1   -1    0
real case failed:   -5.   1.   1.   0.
int case failed:    -5    1    1    0
real case failed:    1.  -1.  -1.   0.
int case failed:     1   -1   -1    0
real case failed:   -1.   1.   1.   0.
int case failed:    -1    1    1    0


-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|modulo generates wrong      |modulo generates wrong
                   |result for divisor -1       |result for divisor 1 and -1


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


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

* [Bug libfortran/19032] modulo generates wrong result for divisor 1 and -1
  2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
                   ` (6 preceding siblings ...)
  2004-12-17 14:29 ` [Bug libfortran/19032] modulo generates wrong result for divisor 1 and -1 Thomas dot Koenig at online dot de
@ 2004-12-20 19:08 ` tobi at gcc dot gnu dot org
  2004-12-20 19:56 ` tobi at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: tobi at gcc dot gnu dot org @ 2004-12-20 19:08 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |tobi at gcc dot gnu dot org
                   |dot org                     |
             Status|NEW                         |ASSIGNED
   Last reconfirmed|2004-12-16 14:30:16         |2004-12-20 19:08:48
               date|                            |


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


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

* [Bug libfortran/19032] modulo generates wrong result for divisor 1 and -1
  2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
                   ` (7 preceding siblings ...)
  2004-12-20 19:08 ` tobi at gcc dot gnu dot org
@ 2004-12-20 19:56 ` tobi at gcc dot gnu dot org
  2004-12-27 17:13 ` cvs-commit at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: tobi at gcc dot gnu dot org @ 2004-12-20 19:56 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From tobi at gcc dot gnu dot org  2004-12-20 19:56 -------
Updated patch here: http://gcc.gnu.org/ml/gcc-patches/2004-12/msg01563.html

-- 


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


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

* [Bug libfortran/19032] modulo generates wrong result for divisor 1 and -1
  2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
                   ` (8 preceding siblings ...)
  2004-12-20 19:56 ` tobi at gcc dot gnu dot org
@ 2004-12-27 17:13 ` cvs-commit at gcc dot gnu dot org
  2004-12-27 17:14 ` tobi at gcc dot gnu dot org
  2004-12-27 17:16 ` pinskia at gcc dot gnu dot org
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu dot org @ 2004-12-27 17:13 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From cvs-commit at gcc dot gnu dot org  2004-12-27 17:13 -------
Subject: Bug 19032

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	tobi@gcc.gnu.org	2004-12-27 17:13:07

Modified files:
	gcc/fortran    : ChangeLog trans-intrinsic.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/gfortran.fortran-torture/execute: 
	                                                intrinsic_mod_ulo.f90 
Added files:
	gcc/testsuite/gfortran.dg: intrinsic_modulo_1.f90 

Log message:
	fortran/
	PR fortran/19032
	* trans-intrinsic.c (gfc_conv_intrinsic_mod): Update comment
	in front of function to match the standard.  Correct handling
	of MODULO.
	
	testsuite/
	PR fortran/19032
	* gfortran.dg/intrinsic_modulo_1.f90: New.
	* gfortran.fortran-torture/execute/intrinsic_mod_ulo.f90: Add
	tests with divisor -1.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/ChangeLog.diff?cvsroot=gcc&r1=1.279&r2=1.280
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/trans-intrinsic.c.diff?cvsroot=gcc&r1=1.35&r2=1.36
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.4822&r2=1.4823
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/intrinsic_modulo_1.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.fortran-torture/execute/intrinsic_mod_ulo.f90.diff?cvsroot=gcc&r1=1.2&r2=1.3



-- 


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


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

* [Bug libfortran/19032] modulo generates wrong result for divisor 1 and -1
  2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
                   ` (9 preceding siblings ...)
  2004-12-27 17:13 ` cvs-commit at gcc dot gnu dot org
@ 2004-12-27 17:14 ` tobi at gcc dot gnu dot org
  2004-12-27 17:16 ` pinskia at gcc dot gnu dot org
  11 siblings, 0 replies; 13+ messages in thread
From: tobi at gcc dot gnu dot org @ 2004-12-27 17:14 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From tobi at gcc dot gnu dot org  2004-12-27 17:13 -------
Fixed.

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


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


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

* [Bug libfortran/19032] modulo generates wrong result for divisor 1 and -1
  2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
                   ` (10 preceding siblings ...)
  2004-12-27 17:14 ` tobi at gcc dot gnu dot org
@ 2004-12-27 17:16 ` pinskia at gcc dot gnu dot org
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-12-27 17:16 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |4.0.0


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


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

end of thread, other threads:[~2004-12-27 17:16 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-16 11:28 [Bug libfortran/19032] New: modulo generates wrong result Thomas dot Koenig at online dot de
2004-12-16 11:29 ` [Bug libfortran/19032] " Thomas dot Koenig at online dot de
2004-12-16 14:30 ` tobi at gcc dot gnu dot org
2004-12-16 14:41 ` [Bug libfortran/19032] modulo generates wrong result for divisor -1 tobi at gcc dot gnu dot org
2004-12-16 14:42 ` tobi at gcc dot gnu dot org
2004-12-16 22:40 ` Thomas dot Koenig at online dot de
2004-12-17  1:18 ` tobi at gcc dot gnu dot org
2004-12-17 14:29 ` [Bug libfortran/19032] modulo generates wrong result for divisor 1 and -1 Thomas dot Koenig at online dot de
2004-12-20 19:08 ` tobi at gcc dot gnu dot org
2004-12-20 19:56 ` tobi at gcc dot gnu dot org
2004-12-27 17:13 ` cvs-commit at gcc dot gnu dot org
2004-12-27 17:14 ` tobi at gcc dot gnu dot org
2004-12-27 17:16 ` pinskia 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).