Xref: utzoo comp.misc:8451 comp.lang.misc:4385 comp.arch:14519 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uwm.edu!ogicse!zephyr.ens.tek.com!tekcrl!tekgvs!toma From: toma@tekgvs.LABS.TEK.COM (Tom Almy) Newsgroups: comp.misc,comp.lang.misc,comp.arch Subject: Re: Modulus Message-ID: <7058@tekgvs.LABS.TEK.COM> Date: 9 Mar 90 16:15:13 GMT References: <981@m1.cs.man.ac.uk> <12115@goofy.megatest.UUCP> <2573@castle.ed.ac.uk> <1710@tws8.cs.tcd.ie> Reply-To: toma@tekgvs.LABS.TEK.COM (Tom Almy) Followup-To: comp.misc Organization: Tektronix, Inc., Beaverton, OR. Lines: 33 In article <1710@tws8.cs.tcd.ie> emcmanus@cs.tcd.ie (Eamonn McManus) writes: >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. [example deleted] >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. Well Forth supports this type of division. It does so because in control applications you don't want the discontinuity around zero that you get with the truncating division. Yes, that does make it possible to divide by powers of two with simple shift instructions. But now division is messed up because hardware divide operations trucate rather than floor. So you get your choice -- fixup code for divisions by powers of two or fixup code for divisions by signed variables or negative constants. BTW there is considerable support in the Forth community to supply both styles of division. Smalltalk already supplies both: // and \\ for floored integer division and remainder and quo: and rem: for truncating operations. Other languages don't seem so generous. Tom Almy toma@tekgvs.labs.tek.com Standard Disclaimers Apply