Path: utzoo!attcan!uunet!husc6!ukma!gatech!mcnc!rti!sas!toebes
From: toebes@sas.UUCP (John Toebes)
Newsgroups: comp.sys.amiga.tech
Subject: Re: Dividing by a power of two (Re: Aztec compiler ineffeciencies)
Message-ID: <724@sas.UUCP>
Date: 28 Nov 88 14:58:56 GMT
References: <3013@sugar.uu.net> <7949@dasys1.UUCP> 
Reply-To: toebes@sas.UUCP (John Toebes)
Organization: SAS Institute Inc, Cary NC
Lines: 32

In article  bader+@andrew.cmu.edu (Miles Bader) writes:
> 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
Actually it is much simpler than that.  The Lattice 5.0 compiler generates the
3 instructions (sometimes 4) inline to perform the division by 2,4, and 8.
These are the most frequent numbers to occur from analysis of many programs.
Specifically:
      x/= 2
      move x,d0   ;unless it was already there
      bpl  lab    ;skip if positive
      addq #1,d0  ;adjust for rounding
lab:  asr.l #1,d0 ; and do the divide

There are lots of other sequences of multiply/divide by constants (take 5 for
example) that can be optimized.
/*---------------------All standard Disclaimers apply---------------------*/
/*----Working for but not officially representing SAS or Lattice Inc.-----*/
/*----John A. Toebes, VIII             usenet:...!mcnc!rti!sas!toebes-----*/
/*------------------------------------------------------------------------*/