public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "acarmeli at mathworks dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/49543] New: Cast move causes incorrect code and numerical results
Date: Mon, 27 Jun 2011 16:16:00 -0000	[thread overview]
Message-ID: <bug-49543-4@http.gcc.gnu.org/bugzilla/> (raw)

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.


             reply	other threads:[~2011-06-27 16:16 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-27 16:16 acarmeli at mathworks dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-49543-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).