Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!rutgers!rochester!pt.cs.cmu.edu!andrew.cmu.edu!bader+ From: bader+@andrew.cmu.edu (Miles Bader) Newsgroups: comp.sys.amiga.tech Subject: Dividing by a power of two (Re: Aztec compiler ineffeciencies) Message-ID: Date: 26 Nov 88 16:12:16 GMT References: <3013@sugar.uu.net>, <7949@dasys1.UUCP> Organization: Carnegie Mellon Lines: 20 In-Reply-To: <7949@dasys1.UUCP> rodd@dasys1.UUCP (Rod Dorman) writes: > As many compiler writers have found in the past and I'm sure many will > rediscover in the future, this only works for positive values. Try > shifting -1 and you'll get -1 as a result, *not* zero as one would expect. I think the problem is that shifting always rounds down, as opposed to towards zero, like we expect. So even in cases where you have a signed, unkown value to divide, it would probably still be more effecient to inline code. So, when dividing a signed value, x, by a power of two, y: if(x<0 && (x&(y-1))!=0) x|=y; /* pre-round negative numbers */ x>>=log2(y); Which isn't quite as bad as it looks, since y is a known constant power of two. -Miles