User account creation filtered due to spam.

Bug 40686 - Optimization Problem With Data Conversion
Summary: Optimization Problem With Data Conversion
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-07-08 18:18 UTC by Raymond Lu
Modified: 2009-07-08 18:22 UTC (History)
32 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Raymond Lu 2009-07-08 18:18:04 UTC
Our HDF5 software has been having some data conversion problem with GCC's optimization for a few years.  One example is to convert data from short to int.  You can find the program at

 ftp://ftp.hdfgroup.uiuc.edu/pub/outgoing/slu/tmp/ctest.c

When I use "gcc -O2" or "gcc -O3" to compile it, I get wrong values after the conversion.  When I use "gcc -O0" or "gcc -O1", the values are correct.

I'll greatly appreciate your help if you can have a look at it.
Comment 1 Andrew Pinski 2009-07-08 18:22:49 UTC
You are violating C/C++ aliasing rules:
	  d = (uint8_t*)&aligned; 
 
          /* This line causes the trouble. */
	  *((int*)d) = (int)(*((short*)s));

You are writing into a long long via an int which causes undefined behavior.

There are more aliasing problems in this file too with the loop at:
    printf("before conversion:\n");
And
    printf("after conversion:\n");

As those are accessing an char via a short or an int.

*** This bug has been marked as a duplicate of 21920 ***