Bug 8224 - Incorrect joining of signed and unsigned division
Summary: Incorrect joining of signed and unsigned division
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2002-10-14 07:36 UTC by mikulas
Modified: 2003-07-25 17:33 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
bug-div.c (130 bytes, application/octet-stream)
2003-05-21 15:16 UTC, mikulas
Details

Note You need to log in before you can comment on or make changes to this bug.
Description mikulas 2002-10-14 07:36:02 UTC
When evaluating expressions like (unsigned)((int)x/2)/2,
gcc incorrectly merges these two divisions into one signed
(or unsigned in 2.7.2.3) division by 4, producing incorrect
result.

Release:
gcc-2.7.2.3, gcc-2.95.4, gcc-3.0.4, gcc-3.2

Environment:
Bug present in almost all gcc releases, tested on Linux and OS/2.

How-To-Repeat:
Compile and run the file. The bug can be seen with or
without optimizations.
Comment 1 Wolfgang Bangerth 2003-02-02 23:15:06 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Same behavior still present with 3.4. Indeed strange.
Comment 2 glen 2003-03-15 21:50:10 UTC
From: Glen Nakamura <glen@imodulo.com>
To: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org,
	mikulas@artax.karlin.mff.cuni.cz
Cc:  
Subject: Re: c/8224: Incorrect joining of signed and unsigned division
Date: Sat, 15 Mar 2003 21:50:10 +0000

 I'm testing the following patch as a fix to PR c/8224:
 
 	* fold-const.c (extract_muldiv_1): Don't pass through type conversions
 	when signedness changes for division or modulus.
 
 diff -Nru3p gcc-3.3.orig/gcc/fold-const.c gcc-3.3/gcc/fold-const.c
 --- gcc-3.3.orig/gcc/fold-const.c	2003-02-16 08:25:20.000000000 +0000
 +++ gcc-3.3/gcc/fold-const.c	2003-02-16 08:25:20.000000000 +0000
 @@ -4117,7 +4117,12 @@ extract_muldiv_1 (t, c, code, wide_type)
  	      /* ... or its type is larger than ctype,
  		 then we cannot pass through this truncation.  */
  	      || (GET_MODE_SIZE (TYPE_MODE (ctype))
 -		  < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))))
 +		  < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
 +	      /* ... or signedness changes for division or modulus,
 +		 then we cannot pass through this conversion.  */
 +	      || (code != MULT_EXPR
 +		  && (TREE_UNSIGNED (ctype)
 +		      != TREE_UNSIGNED (TREE_TYPE (op0))))))
  	break;
  
        /* Pass the constant down and see if we can make a simplification.  If

Comment 3 glen 2003-03-21 22:48:01 UTC
From: Glen Nakamura <glen@imodulo.com>
To: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org
Cc:  
Subject: Re: c/8224: Incorrect joining of signed and unsigned division
Date: Fri, 21 Mar 2003 22:48:01 -1000

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8224
 
 I'll need someone with CVS write access to commit the following patch:
 http://gcc.gnu.org/ml/gcc-bugs/2003-03/msg00949.html
 Approval is here:
 http://gcc.gnu.org/ml/gcc-patches/2003-03/msg01966.html
 
 FYI, the patch I submitted to gcc-patches is equivalent to the one above
 except I mistakenly forgot the "or modulus" part of the comment.
 Hence, I think the patch sent to gcc-bugs should be committed.
 
 Thanks!
 
 -glen

Comment 4 glen 2003-03-23 18:33:20 UTC
From: Glen Nakamura <glen@imodulo.com>
To: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org
Cc:  
Subject: Re: c/8224: Incorrect joining of signed and unsigned division
Date: Sun, 23 Mar 2003 18:33:20 +0000

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8224
 
 Adding link to test case:
 http://gcc.gnu.org/ml/gcc-patches/2003-03/txt00029.txt

Comment 5 Mark Mitchell 2003-03-23 22:55:51 UTC
From: mmitchel@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c/8224
Date: 23 Mar 2003 22:55:51 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_3-branch
 Changes by:	mmitchel@gcc.gnu.org	2003-03-23 22:55:51
 
 Modified files:
 	gcc            : ChangeLog fold-const.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/gcc.dg: 20030323-1.c 
 
 Log message:
 	PR c/8224
 	* fold-const.c (extract_muldiv_1): Don't pass through type conversions
 	when signedness changes for division or modulus.
 	
 	PR c/8224
 	* gcc.dg/20030323-1.c: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.16114.2.346&r2=1.16114.2.347
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fold-const.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.227.2.2&r2=1.227.2.3
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.2261.2.109&r2=1.2261.2.110
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20030323-1.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
 

Comment 6 Mark Mitchell 2003-03-23 22:55:51 UTC
From: mmitchel@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c/8224
Date: 23 Mar 2003 22:55:51 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_3-branch
 Changes by:	mmitchel@gcc.gnu.org	2003-03-23 22:55:51
 
 Modified files:
 	gcc            : ChangeLog fold-const.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/gcc.dg: 20030323-1.c 
 
 Log message:
 	PR c/8224
 	* fold-const.c (extract_muldiv_1): Don't pass through type conversions
 	when signedness changes for division or modulus.
 	
 	PR c/8224
 	* gcc.dg/20030323-1.c: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.16114.2.346&r2=1.16114.2.347
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fold-const.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.227.2.2&r2=1.227.2.3
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.2261.2.109&r2=1.2261.2.110
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20030323-1.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
 
Comment 7 Mark Mitchell 2003-03-23 22:57:26 UTC
From: mmitchel@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c/8224
Date: 23 Mar 2003 22:57:26 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	mmitchel@gcc.gnu.org	2003-03-23 22:57:26
 
 Modified files:
 	gcc            : ChangeLog fold-const.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/gcc.dg: 20030323-1.c 
 
 Log message:
 	PR c/8224
 	* fold-const.c (extract_muldiv_1): Don't pass through type conversions
 	when signedness changes for division or modulus.
 	
 	PR c/8224
 	* gcc.dg/20030323-1.c: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=1.17195&r2=1.17196
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fold-const.c.diff?cvsroot=gcc&r1=1.241&r2=1.242
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2527&r2=1.2528
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20030323-1.c.diff?cvsroot=gcc&r1=1.1&r2=1.2
 

Comment 8 Mark Mitchell 2003-03-23 22:57:26 UTC
From: mmitchel@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c/8224
Date: 23 Mar 2003 22:57:26 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	mmitchel@gcc.gnu.org	2003-03-23 22:57:26
 
 Modified files:
 	gcc            : ChangeLog fold-const.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/gcc.dg: 20030323-1.c 
 
 Log message:
 	PR c/8224
 	* fold-const.c (extract_muldiv_1): Don't pass through type conversions
 	when signedness changes for division or modulus.
 	
 	PR c/8224
 	* gcc.dg/20030323-1.c: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=1.17195&r2=1.17196
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fold-const.c.diff?cvsroot=gcc&r1=1.241&r2=1.242
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2527&r2=1.2528
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20030323-1.c.diff?cvsroot=gcc&r1=1.1&r2=1.2
 

Comment 9 Mark Mitchell 2003-03-23 22:57:43 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: Fixed in GCC 3.3, GCC 3.4.
Comment 10 Eric Botcazou 2003-03-29 10:29:33 UTC
From: ebotcazou@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c/8224
Date: 29 Mar 2003 10:29:33 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_2-branch
 Changes by:	ebotcazou@gcc.gnu.org	2003-03-29 10:29:33
 
 Modified files:
 	gcc            : ChangeLog fold-const.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/gcc.dg: 20030323-1.c 
 
 Log message:
 	PR c/8224
 	* fold-const.c (extract_muldiv_1): Don't pass through type conversions
 	when signedness changes for division or modulus.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.13152.2.657.2.289&r2=1.13152.2.657.2.290
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fold-const.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.185.2.4.2.6&r2=1.185.2.4.2.7
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.1672.2.166.2.121&r2=1.1672.2.166.2.122
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20030323-1.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=NONE&r2=1.2.4.1