Another way (How not to write a loop)

Richard A. O'Keefe ok at quintus.UUCP
Sat Feb 20 14:18:11 AEST 1988


In article <722 at mplvax.nosc.MIL>, cdl at mplvax.nosc.MIL (Carl Lowenstein) writes:
> In article <2150 at bsu-cs.UUCP> dhesi at 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.



More information about the Comp.lang.c mailing list