Bug 55881 - gcc generates incorrect code accessing a double parameter as a bit field.
Summary: gcc generates incorrect code accessing a double parameter as a bit field.
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc   
(Show other bugs)
Version: 7.0
Hardware: i686 Linux
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Brian Brock
Depends On:
TreeView+ depends on / blocked
Reported: 2001-11-08 08:25 UTC by laurence.bond
Modified: 2007-04-18 16:38 UTC (History)
0 users

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

Attachments (Terms of Use)

Description laurence.bond 2001-11-08 08:25:36 UTC
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 10:08:05 UTC
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.