optimization/6064: byte swap function returns bad value

dp@fc.hp.com dp@fc.hp.com
Tue Mar 26 08:16:00 GMT 2002


>Number:         6064
>Category:       optimization
>Synopsis:       byte swap function returns bad value
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Mar 26 08:16:01 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     David Pinedo
>Release:        gcc-2.96-85
>Organization:
>Environment:
Red Hat Linux 7.1 plus some errata
gcc rpm is 2.96-85
Pentium III processor
>Description:
Compile and run the following program:

    unsigned int byteSwap(unsigned int * word)
    {
        char* byte;
        unsigned int *uip;
        char newWord[4];
        byte = (char*) word;
        newWord[3] = *byte++;
        newWord[2] = *byte++;
        newWord[1] = *byte++;
        newWord[0] = *byte++;
        uip = (unsigned int*)newWord;
        return *uip;
    }
 
    main(int argc, char **argv)
    {
        unsigned int ui;
        ui=0x11223344;
        printf("%08x\n", byteSwap(&ui));
    }

If -O2 optimization is used, the output is: 4433f894 (incorrect)

If -O1 optimization is used, the output is: 44332211 (correct)

With -O2 optimization, if the variable newWord is declared static,
the output is correct.
>How-To-Repeat:

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-prs mailing list