Re: gcc and bitwise right shift
Re: gcc and bitwise right shift
- Subject: Re: gcc and bitwise right shift
- From: Thierry Faucounau <email@hidden>
- Date: Wed, 18 Mar 2009 22:42:49 +0100
On Mar 18, 2009, at 12:14 PM, Jeremy Pereira wrote:
On 17 Mar 2009, at 19:09, Thierry Faucounau wrote:
As far as I understood it, bitwise shifts using << or >> in C were
logical shifts (ie. bringing in zeros). However, I just hit a
difference in behavior between the windows compiler and gcc and I
haev to say, I sort of side with VC++ on this one.
This is beside the main point, but your understanding is incorrect.
The C compiler uses an arithmetic shift for right shifts which means
the sign is preserved. e.g.
Yep, I likely should have written expected rather than understood as I
was dealing with unsigned numbers and what turns out to be a rather
common but undefined Windows pattern of shifting by max_width - value.
Basically the case I had hit was the use of Val >> (64 - Amount) for a
64 bit number which produced different results when Amount was 0.
Obviously when you are porting games, the fact that the results of
such a seemingly basic operation differed and had just caused you a
few hours of debugging was all that mattered, and what I was actually
looking for was confirmation that either gcc had a bug or that the
Windows code was relying on compiler/platform specific behavior.
In hindsight I probably should have looked into it further but I have
found there is no better catalyst for proper research of an issue that
publicly embarrassing yourself on a mailing list (which my second
reply only amplified :) )
Interestingly enough, I also found this gcc bug
<http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36503>
Which describes the 32 bit version of the pattern that was causing me
grief: x86 can use x >> -y for x >> 32-y both of which it turns out
are undefined by the standard (assuming y = 0 in the 32 bit case) as
far as I now actually understand it :)
Thanks again to Glen for pointing me to the information I needed to
confirm that I was fixing an incorrect assumption on the part of the
PC code and not hitting some odd gcc optimization bug.
Thierry
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden