public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "hongbo dot yang at mathworks dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/35774]  New: wrong code on casting int result to signed char
Date: Mon, 31 Mar 2008 14:34:00 -0000	[thread overview]
Message-ID: <bug-35774-15992@http.gcc.gnu.org/bugzilla/> (raw)

In the main function there is a cast casting int result to signed char.

typedef  int  int32_T;
typedef  unsigned int  uint32_T;
typedef  signed char  int8_T;

#include <stdio.h> 

int main(void)
{
    int32_T i;
    uint32_T numerator;

    for (i = 126; i < 256; i++) {

      numerator = (uint32_T)((int8_T)(-128 + i) + 128) * 63U;

      fprintf(stdout, "i = %d numerator = %u\n", i, numerator); 
    }
    return numerator;
}

This program is compiled with gcc -O0 -g,
I suppose the result of the variable "numerator" should be:
126 * 63
127 * 63
128 * 63
....

But the printed result is equal to:
126 * 63
127 * 63
- 128 * 63
....

The generated assembly code on mac with Intel Core-2-Duo processor is:

        movl    -24(%ebp), %eax
        movsbl  %al,%edx
        movl    %edx, %eax
        sall    $6, %eax
        subl    %edx, %eax
        movl    %eax, -20(%ebp)

It seems both -128 and +128 are ignored.

This problem doesn't appear on gcc 4.1.1.


-- 
           Summary: wrong code on casting int result to signed char
           Product: gcc
           Version: 4.0.1
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: hongbo dot yang at mathworks dot com
 GCC build triplet: i686-linux-gnu
  GCC host triplet: i686-linux-gnu
GCC target triplet: i686-linux-gnu


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


             reply	other threads:[~2008-03-31 14:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-31 14:34 hongbo dot yang at mathworks dot com [this message]
2008-03-31 16:10 ` [Bug c/35774] " rguenth at gcc dot gnu dot 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-35774-15992@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).