Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!sri-spam!ames!ptsfa!ihnp4!cuae2!ltuxa!we53!sw013b!dj3b1!killer!jfh From: jfh@killer.UUCP Newsgroups: comp.arch Subject: Re: Optimization vs. the programmer Message-ID: <775@killer.UUCP> Date: Wed, 15-Apr-87 12:24:08 EST Article-I.D.: killer.775 Posted: Wed Apr 15 12:24:08 1987 Date-Received: Sun, 19-Apr-87 03:17:40 EST References: <479@danews.ATT.COM> <3300003@uiucdcsm> <3135@jade.BERKELEY.EDU> Organization: The Unix(tm) Connection, Dallas, Texas Lines: 46 Summary: Discussion of modulo and unsigned math ... In article <3135@jade.BERKELEY.EDU>, desj@lemon.BERKELEY.EDU (David desJardins) writes: > In article <3300003@uiucdcsm> grunwald@uiucdcsm.cs.uiuc.edu writes: > > > >Strength reduction is replacement of operations such as "i = j * 8" by > >"i = j << 3", or "i = j % 8" by "i = j & 0x3" (if j is unsigned). > ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ > I'm sure you mean "i = j & 0x7". But, more importantly, this is the > right remainder to compute even if j is signed (assuming two's-complement, > of course). It's too bad more people (especially hardware designers!) > don't realize this. > > -- David desJardins Okay - I have always assumed (No, not THAT word :-) that the code i = j & 0x7; only worked with j being unsigned (or at least positive by use, e.g. array subscripts). My understanding is the % returns the remainder of j / 8. Third grade math time ... Try an example ... 23 / 8 = 2 r 7 right? Now the tricky one ... -23 / 8 = -2 r -7 <- my intuition says 'Yes'. This is the way I understand. Do the expression (in integer math) Remainder = Dividend - (Quotient * Divisor) So - Remainder = -23 - (-2 * 8) = -23 - (-16) = -7. But how come when I ask my HP-41CV what it thinks, I get 1? If I ask for INT of -23 / 8 I get -2, not -3. (Yes, -3 is floor (-23.0 / 8.0)). New third grade math (binary example) ... Try an example ... (just remainders, no quotients) 0010111 ^ 0000111 = 0000111 = 7 base 10. Now the negative version ... 1101001 ^ 0000111 = 0000001 = 1 base 10. The first way seems to make sense - the second way works better but seems to require a different quotient be calculated when performing the modulo. - John (jfh@killer.UUCP) No disclaimer. Whattcha gonna do, sue me?