public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/49543] New: Cast move causes incorrect code and numerical results
@ 2011-06-27 16:16 acarmeli at mathworks dot com
  2011-06-27 16:20 ` [Bug middle-end/49543] " acarmeli at mathworks dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: acarmeli at mathworks dot com @ 2011-06-27 16:16 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: Cast move causes incorrect code and numerical results
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: middle-end
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: acarmeli@mathworks.com


Created attachment 24606
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24606
Folded and unfolded expressions calculate different results

Overview
--------

In the following example, "Data" is a 32-bit int, and "tmp" is a 64-bit int.

with gcc -ansi -pedantic the following line:

   tmp = (int64_T)(Data - 1) * 1805715409L; /* 32-bit subtraction */

is compiled as

   tmp = ((int64_T)Data - 1) * 1805715409L; /* 64-bit subtraction */

This changes the numerical result if Data-1 overflowed.

If the code was not folded, as in this example:

    int32_T hoist;
    hoist = Data - 1;
    tmp = (int64_T)hoist * 1805715409L;

the result in tmp is computed correctly.

The attached file cast_move_bug.c demonstrates this and has copiling
instructions.

The attached file cast_move_bug.s shows that the location of the up-cast to 64
with cltq instruction moved to *before* the subtraction (compared to the
unfolded version).

Triggering
----------
The bug was observed on all versions of gcc up to 4.6.0 on 64-bit Linux. 

When -pedantic *IS NOT* specified, the bug is triggered only with -O0. Under
-O1 and above the bug is not triggered.

When -pedantic *IS* specified, the bug is triggered under all optimization
levels: -O0 through -O3.

g++ 4.3.2 and 4.4.3 do not exhibit this bug.

Claim
-----
The up-cast to 64-bit was incorrectly moved inside the parentheses, violating
the effect of parenthesization on type sizes, and causing the subtraction to
become 64-bit.

Fix request
-----------
MathWorks requests the fix to be back-ported to 4.5 - a reasonably current, and
stable release within our customer community.


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

end of thread, other threads:[~2011-06-28  9:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-27 16:16 [Bug middle-end/49543] New: Cast move causes incorrect code and numerical results acarmeli at mathworks dot com
2011-06-27 16:20 ` [Bug middle-end/49543] " acarmeli at mathworks dot com
2011-06-27 17:40 ` pinskia at gcc dot gnu.org
2011-06-27 18:40 ` acarmeli at mathworks dot com
2011-06-27 19:26 ` pinskia at gcc dot gnu.org
2011-06-27 20:15 ` acarmeli at mathworks dot com
2011-06-28  9:31 ` rguenth at gcc dot gnu.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).