Bug 55881 - gcc generates incorrect code accessing a double parameter as a bit field.
gcc generates incorrect code accessing a double parameter as a bit field.
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
i686 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
Brian Brock
Depends On:
  Show dependency treegraph
Reported: 2001-11-08 03:25 EST by laurence.bond
Modified: 2007-04-18 12:38 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2001-11-08 03:25:41 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description laurence.bond 2001-11-08 03:25:36 EST
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
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        movw    -2(%ebp), %ax
        shrw    $4, %ax
        andl    $2047, %eax

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. Compile using gcc 2.96-85 using -O2 
2. Run the resulting executable.

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.
Comment 1 Jakub Jelinek 2001-11-08 05:08:05 EST
gcc has full right to do so, this sample code does invalid type punning.
info gcc on -fstrict-aliasing
Either fix your code, or use -fno-strict-aliasing.

Note You need to log in before you can comment on or make changes to this bug.