[sldev] getting serious about software.
Dirk Moerenhout
blakar at gmail.com
Sun Jun 24 15:09:54 PDT 2007
The difference between / and >> is rounding. A shift will round the
result away from zero for a negative and towards zero for a positive.
/ will in both cases round towards zero. The compiler will hence add
additional instructions to give you the rounding you expect if you use
/ but it'll not if you use >>. In both cases the division itself is a
single instruction.
Still, as I said before, it's integer math and you won't be able to
beat the compiler. The only thing you can exploit is that you get to
pick between / and >> where >> will be faster if you are ok with the
rounding. Given you broke OpenJPEG by using shifts I guess it requires
rounding towards 0 :)
Dirk aka Blakar Ogre
On 6/24/07, Callum Lerwick <seg at haxxed.com> wrote:
> On Sat, 2007-06-23 at 11:53 +0200, Dirk Moerenhout wrote:
> > Your reference seems incomplete. I assume what you wanted to tell us
> > is that in the mean time you've learned that the compiler translates
> > temp/2 or temp>>1 into a single instruction (SAR on intel) which
> > handles the sign correctly (as long as you correctly typed your
> > variable as being signed)?
>
> Nope, three instructions. Simply shifting is incorrect with a negative
> value, and a lot of programmers don't seem to realize this. I didn't
> until I broke OpenJPEG...
>
> _______________________________________________
> Click here to unsubscribe or manage your list subscription:
> /index.html
>
>
>
More information about the SLDev
mailing list