This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: portable signed right shift
- To: crosby at qwes dot math dot cmu dot edu (Scott A Crosby)
- Subject: Re: portable signed right shift
- From: Toshi Morita <tm2 at best dot com>
- Date: Wed, 28 Mar 2001 02:09:11 -0800 (PST)
- Cc: gcc at gcc dot gnu dot org
Scott A Crosby wrote:
> On Tue, 27 Mar 2001, Toshi Morita wrote:
>
> > >
> > > OK, great. So what is an efficient, portable way to get a signed
> > > right shift?
> > >
> >
> > Cast to unsigned, shift(s) right, set the high bit(s) if orignially negative,
> > then cast to signed.
> >
>
> Wouldn't it be easier to do:
>
> y = x / (1<< N)
>
> And trust the compiler to optimize it into a right-shift if that's
> appropriate for the architecture?
>
> Scott.
It's never appropriate to optimize it to only an arithmetic shift.
Consider the case of -1:
-1 / 2 = 0
but
-1 >> 1 = -1 (where >> denotes an arithmetic shift right)
So you can't fake an arithmetic shift right with a signed divide
by a power of two.
Toshi