🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #13603

Re: Optimization vs. the programmer

#13603
From: greg
Date: Mon, 13 Apr 1987 01:27
37 lines
2254 bytes
In article <3135@jade.BERKELEY.EDU> desj@brahms.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.

Only if division of a negative number by a positive one produces a
positive % result. On some machines -11%7 may be -4, while -11/7 is -1.
(In fact 'all machines covered by' K&R work this way).

You are then in trouble if -11%8 is 5 instead of -3. You may believe
this definition of % and / is not 'right' ( there was a fearful, loathsome
debate on net.lang.c about 14 mths ago about this ), but if they work
that way, they have to always work that way, even if the divisor is a
power of 2.

There is another analogous advantage to David's 'right' method (where dividing
a - by a + rounds the quotient to -ve and gives a +ve remainder):  A divide by
a power of two can become an arithmetic right shift.

( the ns32k, always eager to please, provides both forms of % and / ).

[ Just stating some facts. PLEASE don't start this debate again - it's
  a religious issue like byte order ]

>   -- David desJardins


-- 
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg
Have vAX, will hack...
Technical Details
Message-ID: <4560@utcsri.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!utcsri!greg
References: <479@danews.ATT.COM> <3300003@uiucdcsm> <3135@jade.BERKELEY.EDU>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!utgpu!utcsri!greg
From: greg@utcsri.UUCP
Newsgroups: comp.arch
Subject: Re: Optimization vs. the programmer
Message-ID: <4560@utcsri.UUCP>
Date: Sun, 12-Apr-87 20:27:43 EST
Article-I.D.: utcsri.4560
Posted: Sun Apr 12 20:27:43 1987
Date-Received: Sun, 12-Apr-87 22:35:49 EST
References: <479@danews.ATT.COM> <3300003@uiucdcsm> <3135@jade.BERKELEY.EDU>
Reply-To: greg@utcsri.UUCP (Gregory Smith)
Organization: CSRI, University of Toronto
Lines: 37