[patch] (X << C) >> C folding

James A. Morrison ja2morri@csclub.uwaterloo.ca
Mon Jun 13 03:27:00 GMT 2005


Richard Henderson <rth@redhat.com> writes:

> On Sun, Jun 12, 2005 at 01:58:07PM -0400, James A. Morrison wrote:
> > 	* fold-const (fold_binary): Transform (X << C) >> C into X & (-1>>C)
> > 	for unsigned types.
> 
> Not ok.  The correct mask has C 0 bits at the top AND bottom.  So, 
> for instance with C=4, MASK=0x0ffffff0 for a 32-bit type.
> 
> 
> r~

 Huh?  Lets look at an 8-bit number: 11101101 that is shifted by 3 (first
left, then right), so we get:
11101101 -> 01101000 -> 00001101, which is not masked out at the bottom. 

 So we would AND by 1111 1111 shifted by 3 which is 00011111 or 31, which
is also the same as (1 << (8 - 3) - 1) as indicated on Dan's list of
simplifications at http://gcc.gnu.org/ml/gcc/2001-05/msg00445.html .

 Am I missing something?

-- 
Thanks,
Jim

http://www.csclub.uwaterloo.ca/~ja2morri/
http://phython.blogspot.com
http://open.nit.ca/wiki/?page=jim



More information about the Gcc-patches mailing list