Bug 26923 - byte swap incorrect
Summary: byte swap incorrect
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.0.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-03-29 16:34 UTC by Younes
Modified: 2006-03-29 19:26 UTC (History)
46 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Younes 2006-03-29 16:34:49 UTC
A peace of my code uses the two next macros (SWAP and InvSwap) to swap bytes, it worked for a long time under gcc-3.3 but not under gcc-4.0.3

under gcc-3.3:
InvWord(0xABCD) = 0xCDAB

under gcc-4-0.3:
InvWord(0xABCD) = 0xCD00   !!!!


############ FILE STARTS HERE  ##################
#define SWAP(x,y)  (x)^=(y)^=(x)^=(y)
#define InvWord(x) SWAP(*((char *)(&(x))),*((char *)(&(x))+1))
int main()
{
unsigned short data_length;
data_length = 0xABCD;
InvWord(data_length);
return 0;
}


############ FILE ENDS HERE  ###################
I've also tried to use __bswap_16 but id did nothing :(

thanks in advance
Comment 1 Pawel Sikora 2006-03-29 18:51:02 UTC
It's not a gcc bug. The code relies on the results of intermediate
subexpressions.  According to Stroustrup, The C++ Programming Language, section
6.2.2, "The order of evaluation of subexpressions within
an expression is undefined."

You should use sequence points e.g.:

        a ^= b, b ^= a, a ^= b;

This is a dup of PR11751
Comment 2 Andrew Pinski 2006-03-29 19:26:07 UTC

*** This bug has been marked as a duplicate of 11751 ***