This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: portable signed right shift



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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]