public inbox for gcc-prs@sourceware.org help / color / mirror / Atom feed
From: gandalf@winds.org To: gcc-gnats@gcc.gnu.org Subject: c/5593: GCC miscompiles bitshifts on unsigned struct members when creating a 64-bit value Date: Tue, 05 Feb 2002 11:46:00 -0000 [thread overview] Message-ID: <20020205194158.27114.qmail@sources.redhat.com> (raw) >Number: 5593 >Category: c >Synopsis: GCC miscompiles bitshifts on unsigned struct members when creating a 64-bit value >Confidential: no >Severity: serious >Priority: medium >Responsible: unassigned >State: open >Class: wrong-code >Submitter-Id: net >Arrival-Date: Tue Feb 05 11:46:01 PST 2002 >Closed-Date: >Last-Modified: >Originator: gandalf@winds.org >Release: gcc-2.95.3, gcc-3.0.3, gcc-3.1.0 (as of 20020205) >Organization: >Environment: Error occurs on both i686 (IA32) and Alpha (64-bit) environments >Description: Four equations in the following code attempt to leftshift the 'flags' field to create a 64-bit 'long long' value. The function equation4() is in error, producing a value that has the upper 32 bits set to '1' due to an unexpected problem with signed 32-bit integers. This problem is observed under both -O0 and -O3 optimizations. /* Begin Code */ struct field { int num; unsigned int flags:16; unsigned int container:8; unsigned int quantity:8; }; long long equation1(void) { int num=0x1234; unsigned int flags=0x8000; unsigned int container=0; unsigned int quantity=0; return ((long long)num << 32)| (flags << 16)| (container << 8)| quantity; } long long equation2(struct field *data) { return ((long long)data->num << 32)| (unsigned int)(data->flags << 16)| (data->container << 8)| data->quantity; } long long equation3(struct field *data) { return ((long long)data->num << 32)| (data->flags << 12)| (data->container << 8)| data->quantity; } long long equation4(struct field *data) { return ((long long)data->num << 32)| (data->flags << 16)| (data->container << 8)| data->quantity; } int main(int argc, char *argv[]) { struct field test={0x1234, 0x8000, 0, 0}; printf("0x%016qx\n", equation1()); printf("0x%016qx\n", equation2(&test)); printf("0x%016qx\n", equation3(&test)); printf("0x%016qx\n", equation4(&test)); } /* End Code */ Program Output: --------------- 0x0000123480000000 0x0000123480000000 0x0000123408000000 0xffffffff80000000 Analysis: --------- Equation4() above is the equation with the error. The other three equations are provided as a comparison to hone-in on what the actual problem is, as per the following descriptions: Equation1() is different from equation 4 in that the variables being left- shifted are local, unsigned variables declared using 'char' and 'short' instead of being members of a structure that are all defined as 'unsigned int' with a number-of-bits limitation (the :8 and :16). Equation2() is different in that the expression (data->flags << 16) is prefixed by an (unsigned int) cast. Equation3() is different in that the data->flags field is being left-shifted by 12 instead of by 16. Whatever the case, because 'data->flags' is an unsigned 16-bit integer, left- shifting by 16 in a 64-bit value is erroneously being sign-extended unless the entire shift operation is explicitly casted as unsigned. --- Byron Stanoszek Ph: (330) 644-3059 Systems Programmer Fax: (330) 644-8110 Commercial Timesharing Inc. Email: bstanoszek@comtime.com >How-To-Repeat: >Fix: >Release-Note: >Audit-Trail: >Unformatted: ----gnatsweb-attachment---- Content-Type: application/octet-stream; name="error.c" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="error.c" c3RydWN0IGZpZWxkIHsKICBpbnQgbnVtOwogIHVuc2lnbmVkIGludCBmbGFnczoxNjsKICB1bnNp Z25lZCBpbnQgY29udGFpbmVyOjg7CiAgdW5zaWduZWQgaW50IHF1YW50aXR5Ojg7Cn07Cgpsb25n IGxvbmcgZXF1YXRpb24xKHZvaWQpCnsKICBpbnQgbnVtPTB4MTIzNDsKICB1bnNpZ25lZCBpbnQg ZmxhZ3M9MHg4MDAwOwogIHVuc2lnbmVkIGludCBjb250YWluZXI9MDsKICB1bnNpZ25lZCBpbnQg cXVhbnRpdHk9MDsKCiAgcmV0dXJuICgobG9uZyBsb25nKW51bSA8PCAzMil8CiAgICAgICAgICAo ZmxhZ3MgPDwgMTYpfAogICAgICAgICAgKGNvbnRhaW5lciA8PCA4KXwKICAgICAgICAgIHF1YW50 aXR5Owp9Cgpsb25nIGxvbmcgZXF1YXRpb24yKHN0cnVjdCBmaWVsZCAqZGF0YSkKewogIHJldHVy biAoKGxvbmcgbG9uZylkYXRhLT5udW0gPDwgMzIpfAogICAgICAgICAgKHVuc2lnbmVkIGludCko ZGF0YS0+ZmxhZ3MgPDwgMTYpfAogICAgICAgICAgKGRhdGEtPmNvbnRhaW5lciA8PCA4KXwKICAg ICAgICAgIGRhdGEtPnF1YW50aXR5Owp9Cgpsb25nIGxvbmcgZXF1YXRpb24zKHN0cnVjdCBmaWVs ZCAqZGF0YSkKewogIHJldHVybiAoKGxvbmcgbG9uZylkYXRhLT5udW0gPDwgMzIpfAogICAgICAg ICAgKGRhdGEtPmZsYWdzIDw8IDEyKXwKICAgICAgICAgIChkYXRhLT5jb250YWluZXIgPDwgOCl8 CiAgICAgICAgICBkYXRhLT5xdWFudGl0eTsKfQoKbG9uZyBsb25nIGVxdWF0aW9uNChzdHJ1Y3Qg ZmllbGQgKmRhdGEpCnsKICByZXR1cm4gKChsb25nIGxvbmcpZGF0YS0+bnVtIDw8IDMyKXwKICAg ICAgICAgIChkYXRhLT5mbGFncyA8PCAxNil8CiAgICAgICAgICAoZGF0YS0+Y29udGFpbmVyIDw8 IDgpfAogICAgICAgICAgZGF0YS0+cXVhbnRpdHk7Cn0KCmludCBtYWluKGludCBhcmdjLCBjaGFy ICphcmd2W10pCnsKICBzdHJ1Y3QgZmllbGQgdGVzdD17MHgxMjM0LCAweDgwMDAsIDAsIDB9OwoK ICBwcmludGYoIjB4JTAxNnF4XG4iLCBlcXVhdGlvbjEoKSk7CiAgcHJpbnRmKCIweCUwMTZxeFxu IiwgZXF1YXRpb24yKCZ0ZXN0KSk7CiAgcHJpbnRmKCIweCUwMTZxeFxuIiwgZXF1YXRpb24zKCZ0 ZXN0KSk7CiAgcHJpbnRmKCIweCUwMTZxeFxuIiwgZXF1YXRpb240KCZ0ZXN0KSk7Cn0K
next reply other threads:[~2002-02-05 19:46 UTC|newest] Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top 2002-02-05 11:46 gandalf [this message] 2002-02-10 19:02 rth 2002-11-05 8:15 bangerth 2002-11-08 1:06 Christian Ehrhardt 2002-11-08 20:46 Byron Stanoszek 2002-11-09 4:16 neil
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=20020205194158.27114.qmail@sources.redhat.com \ --to=gandalf@winds.org \ --cc=gcc-gnats@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: linkBe 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).