[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

dividing by a power of two



Torsten Scherer writes:
> 
> > -	for(i=0;i<(limit+15/16);i++)
> > +	for(i=0;i<(limit+15)/16;i++)
> 
>  Another small improvement would be to write:
> 
> 	for(i=0;i<(limit+15)>>4;i++)
> 
>  in this and similar other cases. I'm not sure if optimizations like these
> are defined or claimed in newer versions of gcc, but 2.3.3 does not reduce
> the division to a bit shift. Well in fact it does some kind of optimization
> but the code it yet better if you directly use the shift operation.

I've noticed this too. The problem is that (x/16) is not equal to (x>>4)
when `x' is negative, so gcc has to test for the sign of `x'. This
annoyance probably disappears if the operand is unsigned (int or long).

Thierry.