Article View: comp.sys.amiga.tech
Article #10379Dividing by a power of two (Re: Aztec compiler ineffeciencies)
From: Miles Bader
Date: Sat, 26 Nov 1988 16:12
Date: Sat, 26 Nov 1988 16:12
20 lines
1217 bytes
1217 bytes
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
Technical Details
Message-ID:
<YXXhTUy00UkaQ85bl7@andrew.cmu.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!rutgers!rochester!pt.cs.cmu.edu!andrew.cmu.edu!bader+References:
<3013@sugar.uu.net>, <7949@dasys1.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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: <YXXhTUy00UkaQ85bl7@andrew.cmu.edu> 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>