public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/34070] New: Wrong code for (int)x%4
@ 2007-11-12 10:23 simonmar at microsoft dot com
2007-11-12 12:42 ` [Bug rtl-optimization/34070] [4.1/4.2/4.3 Regression] " rguenth at gcc dot gnu dot org
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: simonmar at microsoft dot com @ 2007-11-12 10:23 UTC (permalink / raw)
To: gcc-bugs
The following code generates the wrong result:
--------------------
#include <stdio.h>
int f(unsigned int x)
{
printf("%x %d\n", x, (int)x);
return ((int)x) % 4;
}
int main(int argc, char *argv[])
{
printf("%d\n", f((unsigned int)(-1)));
return 0;
}
--------------------
I expect this:
$ gcc-3.4.3 ctest33.c -Wall && ./a.out
ffffffff -1
-1
and with gcc-4 and greater I get this:
$ gcc-4.2.1 ctest33.c -Wall && ./a.out
ffffffff -1
3
Why do I think this is a bug? Well, initially I thought I'd run into undefined
behaviour, but on closer reading of the C spec it seems the behaviour should be
implementation-defined, and gcc is not implementing the documented behaviour.
Furthermore, gcc's behaviour is not consistent, as implementation-defined
behaviour should be.
The bug appears to be centered around conversion from unsigned to signed
integers. We convert from unsigned to signed in f(), and the value passed is
0xffffffff. The result is therefore implementation-defined (C99 6.3.1.3), and
gcc defines it (section 4.5 of the gcc docs) as: "For conversion to a type of
width N, the value is reduced modulo 2^N to be within range of the type". I
presume this means that the value is truncated to N bits and the result
interpreted as twos-complement, which in this case should mean that (int)x is
-1, and the expression is (-1 % 4), which has value -1.
We can see from the printf output that (int)x has value -1. Since this is its
implementation-defined value, it should have the same value in the expression
(int)x % 4.
Indeed, several minor variations of this code give the expected output.
Substituting 0xffffffffU for x in the definition of f(), for example.
Optimisation level has no effect. Bug also observed on i686-unknown-linux.
--
Summary: Wrong code for (int)x%4
Product: gcc
Version: 4.2.1
Status: UNCONFIRMED
Severity: major
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: simonmar at microsoft dot com
GCC build triplet: x86_64-unknown-linux-gnu
GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34070
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug rtl-optimization/34070] [4.1/4.2/4.3 Regression] Wrong code for (int)x%4
2007-11-12 10:23 [Bug c/34070] New: Wrong code for (int)x%4 simonmar at microsoft dot com
@ 2007-11-12 12:42 ` rguenth at gcc dot gnu dot org
2007-11-12 12:46 ` rguenth at gcc dot gnu dot org
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2007-11-12 12:42 UTC (permalink / raw)
To: gcc-bugs
------- Comment #1 from rguenth at gcc dot gnu dot org 2007-11-12 12:41 -------
The difference is that we now generate
f:
.LFB2:
andl $3, %edi
movl %edi, %eax
ret
while previously we'd get the correct
f:
.LFB3:
leal 3(%rdi), %eax
cmpl $-1, %edi
cmovg %edi, %eax
andl $-4, %eax
subl %eax, %edi
movl %edi, %eax
ret
for
int f(unsigned int x)
{
return ((int)x) % 4;
}
This is a problem in fold, as we get initially:
;; Function f (f)
;; enabled by -tree-original
{
return (int) x & 3;
}
which is wrong.
--
rguenth at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |rguenth at gcc dot gnu dot
| |org
Status|UNCONFIRMED |NEW
Component|c |rtl-optimization
Ever Confirmed|0 |1
Keywords| |wrong-code
Last reconfirmed|0000-00-00 00:00:00 |2007-11-12 12:41:59
date| |
Summary|Wrong code for (int)x%4 |[4.1/4.2/4.3 Regression]
| |Wrong code for (int)x%4
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34070
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug rtl-optimization/34070] [4.1/4.2/4.3 Regression] Wrong code for (int)x%4
2007-11-12 10:23 [Bug c/34070] New: Wrong code for (int)x%4 simonmar at microsoft dot com
2007-11-12 12:42 ` [Bug rtl-optimization/34070] [4.1/4.2/4.3 Regression] " rguenth at gcc dot gnu dot org
@ 2007-11-12 12:46 ` rguenth at gcc dot gnu dot org
2007-11-12 13:03 ` [Bug middle-end/34070] " rguenth at gcc dot gnu dot org
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2007-11-12 12:46 UTC (permalink / raw)
To: gcc-bugs
------- Comment #2 from rguenth at gcc dot gnu dot org 2007-11-12 12:45 -------
Mine.
--
rguenth at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
AssignedTo|unassigned at gcc dot gnu |rguenth at gcc dot gnu dot
|dot org |org
Status|NEW |ASSIGNED
Last reconfirmed|2007-11-12 12:41:59 |2007-11-12 12:45:59
date| |
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34070
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug middle-end/34070] [4.1/4.2/4.3 Regression] Wrong code for (int)x%4
2007-11-12 10:23 [Bug c/34070] New: Wrong code for (int)x%4 simonmar at microsoft dot com
2007-11-12 12:42 ` [Bug rtl-optimization/34070] [4.1/4.2/4.3 Regression] " rguenth at gcc dot gnu dot org
2007-11-12 12:46 ` rguenth at gcc dot gnu dot org
@ 2007-11-12 13:03 ` rguenth at gcc dot gnu dot org
2007-11-12 14:16 ` rguenth at gcc dot gnu dot org
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2007-11-12 13:03 UTC (permalink / raw)
To: gcc-bugs
--
rguenth at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Component|rtl-optimization |middle-end
Known to fail| |4.1.0
Known to work| |4.0.4
Target Milestone|--- |4.2.3
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34070
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug middle-end/34070] [4.1/4.2 Regression] Wrong code for (int)x%4
2007-11-12 10:23 [Bug c/34070] New: Wrong code for (int)x%4 simonmar at microsoft dot com
` (3 preceding siblings ...)
2007-11-12 14:16 ` rguenth at gcc dot gnu dot org
@ 2007-11-12 14:16 ` rguenth at gcc dot gnu dot org
2008-01-22 14:49 ` rguenth at gcc dot gnu dot org
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2007-11-12 14:16 UTC (permalink / raw)
To: gcc-bugs
------- Comment #4 from rguenth at gcc dot gnu dot org 2007-11-12 14:16 -------
Fixed for 4.3.0.
--
rguenth at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Known to work|4.0.4 |4.0.4 4.3.0
Summary|[4.1/4.2/4.3 Regression] |[4.1/4.2 Regression] Wrong
|Wrong code for (int)x%4 |code for (int)x%4
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34070
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug middle-end/34070] [4.1/4.2/4.3 Regression] Wrong code for (int)x%4
2007-11-12 10:23 [Bug c/34070] New: Wrong code for (int)x%4 simonmar at microsoft dot com
` (2 preceding siblings ...)
2007-11-12 13:03 ` [Bug middle-end/34070] " rguenth at gcc dot gnu dot org
@ 2007-11-12 14:16 ` rguenth at gcc dot gnu dot org
2007-11-12 14:16 ` [Bug middle-end/34070] [4.1/4.2 " rguenth at gcc dot gnu dot org
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2007-11-12 14:16 UTC (permalink / raw)
To: gcc-bugs
------- Comment #3 from rguenth at gcc dot gnu dot org 2007-11-12 14:16 -------
Subject: Bug 34070
Author: rguenth
Date: Mon Nov 12 14:16:05 2007
New Revision: 130098
URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130098
Log:
2007-11-12 Richard Guenther <rguenther@suse.de>
PR middle-end/34070
* fold-const.c (fold_binary): If testing for non-negative
operands with tree_expr_nonnegative_warnv_p make sure to
use op0 which has all (sign) conversions retained.
* gcc.c-torture/execute/pr34070-1.c: New testcase.
* gcc.c-torture/execute/pr34070-2.c: Likewise.
Added:
trunk/gcc/testsuite/gcc.c-torture/execute/pr34070-1.c
trunk/gcc/testsuite/gcc.c-torture/execute/pr34070-2.c
Modified:
trunk/gcc/ChangeLog
trunk/gcc/fold-const.c
trunk/gcc/testsuite/ChangeLog
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34070
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug middle-end/34070] [4.1/4.2 Regression] Wrong code for (int)x%4
2007-11-12 10:23 [Bug c/34070] New: Wrong code for (int)x%4 simonmar at microsoft dot com
` (4 preceding siblings ...)
2007-11-12 14:16 ` [Bug middle-end/34070] [4.1/4.2 " rguenth at gcc dot gnu dot org
@ 2008-01-22 14:49 ` rguenth at gcc dot gnu dot org
2008-01-22 14:57 ` [Bug middle-end/34070] [4.1 " rguenth at gcc dot gnu dot org
2008-07-04 16:16 ` jsm28 at gcc dot gnu dot org
7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-01-22 14:49 UTC (permalink / raw)
To: gcc-bugs
------- Comment #5 from rguenth at gcc dot gnu dot org 2008-01-22 14:46 -------
Subject: Bug 34070
Author: rguenth
Date: Tue Jan 22 14:45:56 2008
New Revision: 131723
URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=131723
Log:
2008-01-22 Richard Guenther <rguenther@suse.de>
PR middle-end/34739
Backport from mainline
2008-01-16 Richard Guenther <rguenther@suse.de>
PR c/34768
* c-typeck.c (common_pointer_type): Do not merge inconsistent
type qualifiers for function types.
2007-11-12 Richard Guenther <rguenther@suse.de>
PR middle-end/34070
* fold-const.c (fold_binary): If testing for non-negative
operands with tree_expr_nonnegative_warnv_p make sure to
use op0 which has all (sign) conversions retained.
2006-10-24 Richard Guenther <rguenther@suse.de>
PR middle-end/28796
* builtins.c (fold_builtin_classify): Use HONOR_INFINITIES
and HONOR_NANS instead of MODE_HAS_INFINITIES and MODE_HAS_NANS
for deciding optimizations in consistency with fold-const.c
(fold_builtin_unordered_cmp): Likewise.
Added:
branches/gcc-4_2-branch/gcc/testsuite/gcc.c-torture/execute/pr34070-1.c
- copied unchanged from r130098,
trunk/gcc/testsuite/gcc.c-torture/execute/pr34070-1.c
branches/gcc-4_2-branch/gcc/testsuite/gcc.c-torture/execute/pr34070-2.c
- copied unchanged from r130098,
trunk/gcc/testsuite/gcc.c-torture/execute/pr34070-2.c
branches/gcc-4_2-branch/gcc/testsuite/gcc.c-torture/execute/pr34768-1.c
- copied unchanged from r131568,
trunk/gcc/testsuite/gcc.c-torture/execute/pr34768-1.c
branches/gcc-4_2-branch/gcc/testsuite/gcc.c-torture/execute/pr34768-2.c
- copied unchanged from r131568,
trunk/gcc/testsuite/gcc.c-torture/execute/pr34768-2.c
branches/gcc-4_2-branch/gcc/testsuite/gcc.dg/pr28796-1.c
- copied unchanged from r118001, trunk/gcc/testsuite/gcc.dg/pr28796-1.c
branches/gcc-4_2-branch/gcc/testsuite/gcc.dg/pr28796-2.c
- copied unchanged from r118001, trunk/gcc/testsuite/gcc.dg/pr28796-2.c
Modified:
branches/gcc-4_2-branch/gcc/ChangeLog
branches/gcc-4_2-branch/gcc/builtins.c
branches/gcc-4_2-branch/gcc/c-typeck.c
branches/gcc-4_2-branch/gcc/fold-const.c
branches/gcc-4_2-branch/gcc/testsuite/ChangeLog
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34070
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug middle-end/34070] [4.1 Regression] Wrong code for (int)x%4
2007-11-12 10:23 [Bug c/34070] New: Wrong code for (int)x%4 simonmar at microsoft dot com
` (5 preceding siblings ...)
2008-01-22 14:49 ` rguenth at gcc dot gnu dot org
@ 2008-01-22 14:57 ` rguenth at gcc dot gnu dot org
2008-07-04 16:16 ` jsm28 at gcc dot gnu dot org
7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-01-22 14:57 UTC (permalink / raw)
To: gcc-bugs
------- Comment #6 from rguenth at gcc dot gnu dot org 2008-01-22 14:52 -------
Fixed on the 4.2 branch.
--
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
Known to work|4.0.4 4.3.0 |4.0.4 4.2.3 4.3.0
Summary|[4.1/4.2 Regression] Wrong |[4.1 Regression] Wrong code
|code for (int)x%4 |for (int)x%4
Target Milestone|4.2.3 |4.1.3
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34070
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug middle-end/34070] [4.1 Regression] Wrong code for (int)x%4
2007-11-12 10:23 [Bug c/34070] New: Wrong code for (int)x%4 simonmar at microsoft dot com
` (6 preceding siblings ...)
2008-01-22 14:57 ` [Bug middle-end/34070] [4.1 " rguenth at gcc dot gnu dot org
@ 2008-07-04 16:16 ` jsm28 at gcc dot gnu dot org
7 siblings, 0 replies; 9+ messages in thread
From: jsm28 at gcc dot gnu dot org @ 2008-07-04 16:16 UTC (permalink / raw)
To: gcc-bugs
------- Comment #7 from jsm28 at gcc dot gnu dot org 2008-07-04 16:16 -------
Closing 4.1 branch.
--
jsm28 at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Known to fail|4.1.0 |4.1.0 4.1.3
Resolution| |FIXED
Target Milestone|4.1.3 |4.2.3
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34070
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2008-07-04 16:16 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-12 10:23 [Bug c/34070] New: Wrong code for (int)x%4 simonmar at microsoft dot com
2007-11-12 12:42 ` [Bug rtl-optimization/34070] [4.1/4.2/4.3 Regression] " rguenth at gcc dot gnu dot org
2007-11-12 12:46 ` rguenth at gcc dot gnu dot org
2007-11-12 13:03 ` [Bug middle-end/34070] " rguenth at gcc dot gnu dot org
2007-11-12 14:16 ` rguenth at gcc dot gnu dot org
2007-11-12 14:16 ` [Bug middle-end/34070] [4.1/4.2 " rguenth at gcc dot gnu dot org
2008-01-22 14:49 ` rguenth at gcc dot gnu dot org
2008-01-22 14:57 ` [Bug middle-end/34070] [4.1 " rguenth at gcc dot gnu dot org
2008-07-04 16:16 ` jsm28 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).