Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!mordor!sri-spam!sri-unix!quintus!ok From: ok@quintus.UUCP (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: Another way (How not to write a loop) Message-ID: <670@cresswell.quintus.UUCP> Date: 20 Feb 88 04:18:11 GMT References: <1988Feb11.200149.25172@sq.uucp> <2550046@hpisod2.HP.COM> <722@mplvax.nosc.MIL> Organization: Quintus Computer Systems, Mountain View, CA Lines: 34 In article <722@mplvax.nosc.MIL>, cdl@mplvax.nosc.MIL (Carl Lowenstein) writes: > In article <2150@bsu-cs.UUCP> dhesi@bsu-cs.UUCP (Rahul Dhesi) writes: > >Floating point numbers should not be used for counting except when such > >use gains us a remarkable degree of badly-needed efficiency. > > count -=1; /* integer arithmetic with software test */ > > if (count == 0) abort_program; > This is inefficient? I would hope that any decent compiler could turn > these lines into the something like (usual case one instruction): > / integer arithmetic with hardware test > ISZ COUNT / PDP-8 instruction, but even CISC's like > JMP ABORT / IBM 7090's and later have an equivalent The /360 can do this with COUNT EQU {some register number} S COUNT,=1 BZ ABORT This does, however, involve a memory reference. If you don't want to do a memory reference, you can dedicate a register to holding 1, or you can keep the counter *negative* (no negative offsets!) and do LA COUNT,1(COUNT) * doesn't set the condition codes LTR COUNT,COUNT * so they must be set explicitly BZ ABORT Bearing in mind that the /360 places heavy demands on its general registers (immediate operands are scarce, there are no absolute address, address offsets are 0..4095, &c), maintaining the counter and its increment in floating-point registers could have contributed greatly to efficiency, not in this particular statement, but by making more general registers available elsewhere. The real point is that this was NOT a genuine instance of counting with floating-point values. What they *really* wanted was an integer count, using floating-point instructions was a hack which required exceptionally careful coding. The advice "don't write loops like for (x = 23.1; x <= 23.7; x += 0.1) .... " remains valid.