This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying
- From: "cyp561 at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 10 Mar 2006 12:50:03 -0000
- Subject: [Bug c/26630] New: Incorrect result when subtracting, casting to short and back to int, adding and multiplying
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
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
- Follow-Ups:
- [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
- From: pinskia at gcc dot gnu dot org
- [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
- From: rguenth at gcc dot gnu dot org
- [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
- From: rguenth at gcc dot gnu dot org
- [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
- From: rguenth at gcc dot gnu dot org
- [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
- From: rguenth at gcc dot gnu dot org
- [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
- From: patchapp at dberlin dot org
- [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
- From: rguenth at gcc dot gnu dot org
- [Bug middle-end/26630] [4.0 Regression] Incorrect result when subtracting, casting to short and back to int, adding and multiplying
- From: rguenth at gcc dot gnu dot org