From Bugzilla Helper: User-Agent: Mozilla/4.76 [en] (X11; U; Linux 2.4.0-4GB i686) Description of problem: gcc 2.96-85 produces incorrect code for the function extract in the example C code. Rather than accessing the data from the value passed in, the code takes it from uninitialised data on the stack. So the function could return any value - usually one unrelated to the value passd into the function. Test program: #include <stdio.h> struct ieee { unsigned int m2:32; unsigned int m1:20; unsigned int e:11; unsigned int s:1; }; unsigned int extract(double x) { struct ieee *x_as_struct = (struct ieee *) &x; return x_as_struct->e; } int main(int argc, char **argv[]) { printf("Exponent = %u\n",extract(540881114.0)); return 0; } The code generated by gcc for the extrcat function: .globl extract .type extract,@function extract: pushl %ebp movl %esp, %ebp subl $8, %esp movw -2(%ebp), %ax shrw $4, %ax andl $2047, %eax leave ret Version-Release number of selected component (if applicable): How reproducible: Always Steps to Reproduce: 1. Compile using gcc 2.96-85 using -O2 2. Run the resulting executable. 3. Actual Results: Exponent = 1024 Expected Results: Exponent = 1052 Additional info: I have checked against GCC 2.95-2, egcs 2.91.66. These produce the correct result. The option -fno-strict-aliasing will make the compiler produce the correct code.
gcc has full right to do so, this sample code does invalid type punning. See info gcc on -fstrict-aliasing Either fix your code, or use -fno-strict-aliasing.