public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/26630]  New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying
@ 2006-03-10 12:50 cyp561 at gmail dot com
  2006-03-10 12:57 ` [Bug middle-end/26630] [4.0 Regression] " pinskia at gcc dot gnu dot org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: cyp561 at gmail dot com @ 2006-03-10 12:50 UTC (permalink / raw)
  To: gcc-bugs

As far as I can tell, c1 should be equal to 80000, not -51072, or at least
equal to c2.

Sorry if I'm mistaken, but I can't see how c1 and c2 can be different. The
result is the same using gcc 3.3.6, gcc 3.4.4 and gcc 4.0.2.

Program (error.c):
---------
#include <stdio.h>

int main()
{
    int a1 = 40000;
#define b1   ((int)(short)(a1-10000))
    int c1 = ( b1 +10000)*2;

    int a2 = 40000;
    int b2 = ((int)(short)(a2-10000));
    int c2 = ( b2 +10000)*2;

    printf("sizeof(int) = %d, sizeof(short) = %d\na1 = %d, b1 = %d, c1 = %d\na2
= %d, b2 = %d, c2 = %d (c1 and c2 should both be 80000)\n",
            sizeof(int),      sizeof(short),      a1,      b1,      c1,     
a2,      b2,      c2);

    if( c1!=80000 || c2!=80000 )
    {
        printf("Error!\n");
        return 1;
    }

    return 0;
}
----------
Output:

j@gentoo ~/tmp $ cat /proc/version
Linux version 2.6.15-gentoo-r1 (root@gentoo) (gcc version 3.3.6 (Gentoo 3.3.6,
ssp-3.3.6-1.0, pie-8.7.8)) #1 PREEMPT Wed Jan 25 11:58:26 CET 2006
j@gentoo ~/tmp $ gcc-3.3.6 -v
Reading specs from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.6/specs
Configured with: /var/tmp/portage/gcc-3.3.6/work/gcc-3.3.6/configure
--prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.3.6
--includedir=/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.6/include
--datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.3.6
--mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.3.6/man
--infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.3.6/info
--with-gxx-include-dir=/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.6/include/g++-v3
--host=i686-pc-linux-gnu --build=i686-pc-linux-gnu --disable-altivec
--enable-nls --without-included-gettext --with-system-zlib --disable-checking
--disable-werror --disable-libunwind-exceptions --disable-multilib
--disable-libgcj --enable-languages=c,c++,f77 --enable-shared
--enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu
Thread model: posix
gcc version 3.3.6 (Gentoo 3.3.6, ssp-3.3.6-1.0, pie-8.7.8)
j@gentoo ~/tmp $ gcc-3.3.6 -o error error.c -Wall
j@gentoo ~/tmp $ ./error
sizeof(int) = 4, sizeof(short) = 2
a1 = 40000, b1 = 30000, c1 = -51072
a2 = 40000, b2 = 30000, c2 = 80000 (c1 and c2 should both be 80000)
Error!
j@gentoo ~/tmp $ gcc-3.4.4 -v
Reading specs from /usr/lib/gcc/i686-pc-linux-gnu/3.4.4/specs
Configured with: /var/tmp/portage/gcc-3.4.4-r1/work/gcc-3.4.4/configure
--prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.4.4
--includedir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.4/include
--datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.4
--mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.4/man
--infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.4/info
--with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.4/include/g++-v3
--host=i686-pc-linux-gnu --build=i686-pc-linux-gnu --disable-altivec
--enable-nls --without-included-gettext --with-system-zlib --disable-checking
--disable-werror --disable-libunwind-exceptions --disable-multilib
--disable-libmudflap --disable-libgcj --enable-languages=c,c++,f77
--enable-shared --enable-threads=posix --enable-__cxa_atexit
--enable-clocale=gnu
Thread model: posix
gcc version 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8)
j@gentoo ~/tmp $ gcc-3.4.4 -o error error.c -Wall
j@gentoo ~/tmp $ ./error
sizeof(int) = 4, sizeof(short) = 2
a1 = 40000, b1 = 30000, c1 = -51072
a2 = 40000, b2 = 30000, c2 = 80000 (c1 and c2 should both be 80000)
Error!
j@gentoo ~/tmp $ gcc-4.0.2 -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: /var/tmp/portage/gcc-4.0.2-r3/work/gcc-4.0.2/configure
--prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/4.0.2
--includedir=/usr/lib/gcc/i686-pc-linux-gnu/4.0.2/include
--datadir=/usr/share/gcc-data/i686-pc-linux-gnu/4.0.2
--mandir=/usr/share/gcc-data/i686-pc-linux-gnu/4.0.2/man
--infodir=/usr/share/gcc-data/i686-pc-linux-gnu/4.0.2/info
--with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/4.0.2/include/g++-v4
--host=i686-pc-linux-gnu --build=i686-pc-linux-gnu --disable-altivec
--enable-nls --without-included-gettext --with-system-zlib --disable-checking
--disable-werror --disable-libunwind-exceptions --disable-multilib
--disable-libmudflap --disable-libgcj --enable-languages=c,c++,f95
--enable-shared --enable-threads=posix --enable-__cxa_atexit
--enable-clocale=gnu
Thread model: posix
gcc version 4.0.2 (Gentoo 4.0.2-r3, pie-8.7.8)
j@gentoo ~/tmp $ gcc-4.0.2 -o error error.c -Wall
j@gentoo ~/tmp $ ./error
sizeof(int) = 4, sizeof(short) = 2
a1 = 40000, b1 = 30000, c1 = -51072
a2 = 40000, b2 = 30000, c2 = 80000 (c1 and c2 should both be 80000)
Error!
j@gentoo ~/tmp $


-- 
           Summary: Incorrect result when subtracting, casting to short and
                    back to int, adding and multiplying
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: cyp561 at gmail dot com
 GCC build triplet: i686-pc-linux-gnu-gcc-3.3.6
  GCC host triplet: i686-pc-linux-gnu-gcc-3.3.6
GCC target triplet: i686-pc-linux-gnu-gcc-3.3.6


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


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

* [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
  2006-03-10 12:50 [Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying cyp561 at gmail dot com
@ 2006-03-10 12:57 ` pinskia at gcc dot gnu dot org
  2006-03-10 13:52 ` rguenth at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-03-10 12:57 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2006-03-10 12:57 -------
Confirmed, just a regression on the 4.0 branch, 3.4.6 was the last release of
the 3.4 branch.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
          Component|c                           |middle-end
     Ever Confirmed|0                           |1
           Keywords|                            |wrong-code
      Known to fail|                            |3.4.0 4.0.0 4.0.3 3.3.3
                   |                            |3.0.4
      Known to work|                            |2.95.3 4.1.0 4.2.0
   Last reconfirmed|0000-00-00 00:00:00         |2006-03-10 12:57:24
               date|                            |
            Summary|Incorrect result when       |[4.0 Regression] Incorrect
                   |subtracting, casting to     |result when subtracting,
                   |short and back to int,      |casting to short and back to
                   |adding and multiplying      |int, adding and multiplying
   Target Milestone|---                         |4.0.4


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


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

* [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
  2006-03-10 12:50 [Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying cyp561 at gmail dot com
  2006-03-10 12:57 ` [Bug middle-end/26630] [4.0 Regression] " pinskia at gcc dot gnu dot org
@ 2006-03-10 13:52 ` rguenth at gcc dot gnu dot org
  2006-03-10 13:53 ` rguenth at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-03-10 13:52 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from rguenth at gcc dot gnu dot org  2006-03-10 13:52 -------
We fold
  ((intD.0) ((short intD.7) a1D.1133 - 10000) + 10000) * 2
via extract_muldiv to
  (intD.0) (short intD.7) a1D.1133 * 2


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu dot
                   |                            |org


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


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

* [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
  2006-03-10 12:50 [Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying cyp561 at gmail dot com
  2006-03-10 12:57 ` [Bug middle-end/26630] [4.0 Regression] " pinskia at gcc dot gnu dot org
  2006-03-10 13:52 ` rguenth at gcc dot gnu dot org
@ 2006-03-10 13:53 ` rguenth at gcc dot gnu dot org
  2006-03-10 14:31 ` rguenth at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-03-10 13:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from rguenth at gcc dot gnu dot org  2006-03-10 13:53 -------
shorter testcase:

extern void abort(void);
int main()
{
  int a1 = 40000;
  int c1 = ( ((int)(short)(a1-10000)) + 10000)*2;
  if (c1 != 80000)
    abort();
  return 0;
}


-- 


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


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

* [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
  2006-03-10 12:50 [Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying cyp561 at gmail dot com
                   ` (2 preceding siblings ...)
  2006-03-10 13:53 ` rguenth at gcc dot gnu dot org
@ 2006-03-10 14:31 ` rguenth at gcc dot gnu dot org
  2006-03-10 16:17 ` rguenth at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-03-10 14:31 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from rguenth at gcc dot gnu dot org  2006-03-10 14:31 -------
with 4.1 we start with

 ((intD.0) (short intD.7) ((short unsigned intD.8) a1D.1280 - 10000) + 10000) *
2

which convert builds from converting a - 10000 to short:

  (short intD.7) ((short unsigned intD.8) a1D.1280 - 10000)

this prevents the (now latent) problem in extract_muldiv to show up.

I believe

    case CONVERT_EXPR:  case NON_LVALUE_EXPR:  case NOP_EXPR:
      /* If op0 is an expression ...  */
      if ((COMPARISON_CLASS_P (op0)
           || UNARY_CLASS_P (op0)
           || BINARY_CLASS_P (op0)
           || EXPRESSION_CLASS_P (op0))
          /* ... and is unsigned, and its type is smaller than ctype,
             then we cannot pass through as widening.  */
          && ((TYPE_UNSIGNED (TREE_TYPE (op0))
               && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
                     && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
               && (GET_MODE_SIZE (TYPE_MODE (ctype))
                   > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
              /* ... or this is a truncation (t is narrower than op0),
                 then we cannot pass through this narrowing.  */
              || (GET_MODE_SIZE (TYPE_MODE (type))
                  < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))

the last check needs to read !=, but this extract_muldiv looks like a mess...


-- 


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


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

* [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
  2006-03-10 12:50 [Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying cyp561 at gmail dot com
                   ` (3 preceding siblings ...)
  2006-03-10 14:31 ` rguenth at gcc dot gnu dot org
@ 2006-03-10 16:17 ` rguenth at gcc dot gnu dot org
  2006-03-10 16:25 ` patchapp at dberlin dot org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-03-10 16:17 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from rguenth at gcc dot gnu dot org  2006-03-10 16:17 -------
The patch from 25125 fixes the problem.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  BugsThisDependsOn|                            |25125
         AssignedTo|unassigned at gcc dot gnu   |rguenth at gcc dot gnu dot
                   |dot org                     |org
             Status|NEW                         |ASSIGNED
  GCC build triplet|i686-pc-linux-gnu-gcc-3.3.6 |
   GCC host triplet|i686-pc-linux-gnu-gcc-3.3.6 |
 GCC target triplet|i686-pc-linux-gnu-gcc-3.3.6 |
   Last reconfirmed|2006-03-10 12:57:24         |2006-03-10 16:17:24
               date|                            |


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


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

* [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
  2006-03-10 12:50 [Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying cyp561 at gmail dot com
                   ` (4 preceding siblings ...)
  2006-03-10 16:17 ` rguenth at gcc dot gnu dot org
@ 2006-03-10 16:25 ` patchapp at dberlin dot org
  2006-03-13  9:02 ` rguenth at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: patchapp at dberlin dot org @ 2006-03-10 16:25 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from patchapp at dberlin dot org  2006-03-10 16:25 -------
Subject: Bug number PR26630

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is
http://gcc.gnu.org/ml/gcc-patches/2006-03/msg00616.html


-- 


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


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

* [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
  2006-03-10 12:50 [Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying cyp561 at gmail dot com
                   ` (5 preceding siblings ...)
  2006-03-10 16:25 ` patchapp at dberlin dot org
@ 2006-03-13  9:02 ` rguenth at gcc dot gnu dot org
  2006-03-13  9:09 ` rguenth at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-03-13  9:02 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from rguenth at gcc dot gnu dot org  2006-03-13 09:02 -------
Subject: Bug 26630

Author: rguenth
Date: Mon Mar 13 09:02:40 2006
New Revision: 111990

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=111990
Log:
2006-03-13  Richard Guenther  <rguenther@suse.de>

        PR middle-end/26630
        * gcc.dg/torture/pr26630.c: New testcase.

Added:
    trunk/gcc/testsuite/gcc.dg/torture/pr26630.c
Modified:
    trunk/gcc/testsuite/ChangeLog


-- 


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


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

* [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
  2006-03-10 12:50 [Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying cyp561 at gmail dot com
                   ` (6 preceding siblings ...)
  2006-03-13  9:02 ` rguenth at gcc dot gnu dot org
@ 2006-03-13  9:09 ` rguenth at gcc dot gnu dot org
  2006-06-09 10:35 ` rguenth at gcc dot gnu dot org
  2007-02-03 16:23 ` gdr at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-03-13  9:09 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from rguenth at gcc dot gnu dot org  2006-03-13 09:09 -------
Subject: Bug 26630

Author: rguenth
Date: Mon Mar 13 09:09:13 2006
New Revision: 111992

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=111992
Log:
2006-03-13  Richard Guenther  <rguenther@suse.de>

        PR middle-end/26630
        * gcc.dg/torture/pr26630.c: New testcase.

Added:
    branches/gcc-4_1-branch/gcc/testsuite/gcc.dg/torture/pr26630.c
Modified:
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog


-- 


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


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

* [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
  2006-03-10 12:50 [Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying cyp561 at gmail dot com
                   ` (7 preceding siblings ...)
  2006-03-13  9:09 ` rguenth at gcc dot gnu dot org
@ 2006-06-09 10:35 ` rguenth at gcc dot gnu dot org
  2007-02-03 16:23 ` gdr at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-06-09 10:35 UTC (permalink / raw)
  To: gcc-bugs



-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|rguenth at gcc dot gnu dot  |unassigned at gcc dot gnu
                   |org                         |dot org
             Status|ASSIGNED                    |NEW


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


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

* [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
  2006-03-10 12:50 [Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying cyp561 at gmail dot com
                   ` (8 preceding siblings ...)
  2006-06-09 10:35 ` rguenth at gcc dot gnu dot org
@ 2007-02-03 16:23 ` gdr at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: gdr at gcc dot gnu dot org @ 2007-02-03 16:23 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from gdr at gcc dot gnu dot org  2007-02-03 16:23 -------
Fixed in GCC-4.1.0.


-- 

gdr at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|4.0.4                       |4.1.0


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


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

end of thread, other threads:[~2007-02-03 16:23 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-10 12:50 [Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying cyp561 at gmail dot com
2006-03-10 12:57 ` [Bug middle-end/26630] [4.0 Regression] " pinskia at gcc dot gnu dot org
2006-03-10 13:52 ` rguenth at gcc dot gnu dot org
2006-03-10 13:53 ` rguenth at gcc dot gnu dot org
2006-03-10 14:31 ` rguenth at gcc dot gnu dot org
2006-03-10 16:17 ` rguenth at gcc dot gnu dot org
2006-03-10 16:25 ` patchapp at dberlin dot org
2006-03-13  9:02 ` rguenth at gcc dot gnu dot org
2006-03-13  9:09 ` rguenth at gcc dot gnu dot org
2006-06-09 10:35 ` rguenth at gcc dot gnu dot org
2007-02-03 16:23 ` gdr 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).