cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted
Adventurer
Adventurer
3,735 Views
Registered: ‎06-18-2008

GCC runtime problem switch case variable declaration

Hello,

 

I use the GCC 4.1.1 on ppc405. (gcc version 4.1.1 20060524 (Xilinx EDK 10.1.03 Build EDK_K_SP2.5 11 Jun 2008))

  

In a switch statment I use the following snippet

 

unsigned int value;

... 

 

case SENSOR_DUALSLOPE_TIME:

{

float time = 2.0f;

value = *((uint32_t *)(&time));

break;

 

If  I compile my code with optimization -O1 the contents of value is right.

If  I compile my code with optimization -O2 or -O3 the contents of value is incorrect.

 

But if I use the following snippet:

 

case SENSOR_DUALSLOPE_TIME:

{

float time = 2.0f;

printf("%f", time); 

value = *((uint32_t *)(&time));

break;

 

all is right. 

 

Does anyone knows the how to fix it or reason why? I need to keep the optimization -O2 or -O3.

 

Thanks, Thomas 

 

 

0 Kudos
Reply
2 Replies
Highlighted
Xilinx Employee
Xilinx Employee
3,721 Views
Registered: ‎01-18-2008

I suspect this is an issue in your code due to gcc's strict aliasing options kicking in at -O2. Read about the -fstrict-aliasing option in gcc manual:

 

http://gcc.gnu.org/onlinedocs/gcc-4.0.2/gcc/Optimize-Options.html

 

You can explicitly disable it (-fno-strict-aliasing), but that is not a good practice.

Highlighted
Adventurer
Adventurer
3,686 Views
Registered: ‎06-18-2008

Hello,

 

thanks for the answer. Yes, so it is.

 

An explanation I found here: 

 

http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html

 

Thanks 

0 Kudos
Reply