Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!gatech!bloom-beacon!mit-eddie!ll-xn!ames!ptsfa!ihnp4!inuxc!iuvax!pur-ee!uiucdcs!uiucdcsb!robison From: robison@uiucdcsb.cs.uiuc.edu Newsgroups: comp.arch Subject: Re: chewing up mips with graphics [ Message-ID: <165100010@uiucdcsb> Date: Sat, 4-Jul-87 13:07:00 EDT Article-I.D.: uiucdcsb.165100010 Posted: Sat Jul 4 13:07:00 1987 Date-Received: Tue, 7-Jul-87 06:49:46 EDT References: <1331@ogcvax.UUCP> Lines: 36 Nf-ID: #R:ogcvax.UUCP:1331:uiucdcsb:165100010:000:816 Nf-From: uiucdcsb.cs.uiuc.edu!robison Jul 4 12:07:00 1987 > One such problem (one which is serial) is the calculation of positive integer > powers. The fastest method is as follows: > > pow(b,i) /* raise b to the ith power */ > { > int j; > > for (j=1; i; i>>=1) { > if (i&1) /* least significant bit is set */ > j *= b; > b *= b; /* square b */ > } > return(j); > } > Look in The Art of Computer Programming, vol. 2, section 4.6.3. The above binary scheme is not the fastest possible. For example: 5 15 3 X = X requires 5 multiplications while 2 2 2 15 2 2 2 X = X * X * X * X requires 6 multiplications Arch D. Robison University of Illinois at Urbana-Champaign CSNET: robison@UIUC.CSNET UUCP: {ihnp4,pur-ee,convex}!uiucdcs!robison ARPA: robison@B.CS.UIUC.EDU (robison@UIUC.ARPA)