Xref: utzoo comp.misc:8434 comp.lang.misc:4367 comp.arch:14484 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!ukc!tcdcs!swift.cs.tcd.ie!emcmanus From: emcmanus@cs.tcd.ie (Eamonn McManus) Newsgroups: comp.misc,comp.lang.misc,comp.arch Subject: Re: Modulus Summary: Rounding to negative infinity allows shifts for division Message-ID: <1710@tws8.cs.tcd.ie> Date: 7 Mar 90 00:38:09 GMT References: <981@m1.cs.man.ac.uk> <12115@goofy.megatest.UUCP> <2573@castle.ed.ac.uk> Organization: Computer Science Department, Trinity College Dublin Lines: 19 db@lfcs.ed.ac.uk (Dave Berry) writes: >1. Is the "normal" method for calculating integer division and modulus > intrinsically faster than the mathematically expected definition (that > rounds towards minus infinity), or is it just convention that it's > implemented this way? An interesting point about rounding to minus infinity is that it allows (sign-extending) right shifts to be used for integer division by powers of two, when using twos-complement representation. For example, -7/2 is -3 by the `normal' definition but -4 by the `mathematically expected' definition. Its twos-complement representation is 1...1001, which when shifted right produces 1...100 or -4 as required. If a programming language supported this definition of negative division, its compilers could optimize divisions by powers of two into shifts without having to know whether the quantity being divided was negative. On many (most?) processors shifts are faster than divisions even by powers of two. [Unsupported assertion.] -- Eamonn McManus One of the 0% of Americans who are not Americans.