Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!cbmvax!jesup From: jesup@cbmvax.commodore.com (Randell Jesup) Newsgroups: comp.arch Subject: Re: Integer Multiply/Divide Message-ID: <9193@cbmvax.commodore.com> Date: 2 Jan 90 23:26:50 GMT References: <84768@linus.UUCP> <4057@brazos.Rice.edu> Reply-To: jesup@cbmvax.commodore.com (Randell Jesup) Organization: Commodore, West Chester, PA Lines: 16 In article <4057@brazos.Rice.edu> preston@titan.rice.edu (Preston Briggs) writes: >Division is slightly more complex, due to sign handling. >For most languages, it isn't safe to replace a divide by 2^N >with a shift right (doesn't work for negative numbers). >The same problem arises when replacing I % 4 with I & 3. Easy solution: x/2^n = (x >> n) + (x & 1 & (bit 31 of x)). Of course, the >> operation must be "arithmetic" and not "logical" (i.e. sign-extending). For a machine with slow divides and a barrel shifter (many risc chips), this may be faster than a divide subroutine/instruction. Also note it avoids any branches. -- Randell Jesup, Keeper of AmigaDos, Commodore Engineering. {uunet|rutgers}!cbmvax!jesup, jesup@cbmvax.cbm.commodore.com BIX: rjesup Common phrase heard at Amiga Devcon '89: "It's in there!"