Another way (How not to write a loop)
Doug Gwyn
gwyn at brl-smoke.ARPA
Fri Feb 19 09:56:35 AEST 1988
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. In the
>Snobol (actually SPITBOL) example, the choice was between the
>equivalents of the following:
> count -=1; /* integer arithmetic with software test */
> if (count == 0)
> abort_program;
>and
> count +=x; /* fast hardware floating add */
> /* actually AUR SCNT,SINC on IBM 360 */
>Note that the language required one or the other of the above to
>be done before every statement executed.
So, the Snobol implementor made a stupid decision, then.
I don't have an IBM 360 at hand, but I do have a Gould PowerNode 9080,
which has a remarkably similar architecture (including floating-point
format) to the IBM 360's. I jury-rigged a crt0.o to enable floating-
point exception trapping (normally it has to be disabled, to avoid
trapping on integer overflow), and ran a large number of iterations
(that did nothing but iterate) using floating-point trapping to
terminate the loop, then an identical number of iterations testing an
integer counter. The floating-point version burned 4.3 seconds of CPU
time while the integer counter version's overhead was only 0.5 seconds.
Note that the time required to perform the integer counter test against
zero is utterly insignificant by comparison with whatever work the
"statement execution" would involve, and it is portable besides.
This looks like another instance of "misplaced efficiency concerns".
More information about the Comp.lang.c
mailing list