Article View: comp.lang.modula2
Article #4604Re: "for" loops (was Re: C++ vs. Modula2)
From: Robert Firth
Date: Mon, 30 Jan 1989 13:58
Date: Mon, 30 Jan 1989 13:58
29 lines
1445 bytes
1445 bytes
In article <15694@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
>(This is what a compiler typically generates for a Modula loop with
>indeterminate bounds---actually, it is more like
>
> if (a > b) goto out;
> c = a;
> top: ...;
> if (c==b) goto out;
> c++;
> goto top;
> out: ;
There is a trick that works on most machines; it gives a rather tighter
loop than the above:
IF a>b THEN GOTO done END;
c := a-1;
loop:
c := c+1;
<body>
IF c#b THEN GOTO loop END;
done:
This even works when 'a' is the minimum of the range; the value wraps around
on the decrement but immediately wraps back again on the first increment.
For a zero-times loop this is one conditional branch taken; for an N-times
loop it is one conditional branch not taken and (N-1) taken, plus the
possible overhead of the extra "-1" in the control initialisation.
Technical Details
Message-ID:
<8374@aw.sei.cmu.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!rochester!pt.cs.cmu.edu!sei!sei.cmu.edu!firthReferences:
<19579@agate.BERKELEY.EDU> <8515@lanl.gov> <6419@polya.Stanford.EDU> <15694@mimsy.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!rochester!pt.cs.cmu.edu!sei!sei.cmu.edu!firth From: firth@sei.cmu.edu (Robert Firth) Newsgroups: comp.lang.modula2 Subject: Re: "for" loops (was Re: C++ vs. Modula2) Message-ID: <8374@aw.sei.cmu.edu> Date: 30 Jan 89 13:58:20 GMT References: <19579@agate.BERKELEY.EDU> <8515@lanl.gov> <6419@polya.Stanford.EDU> <15694@mimsy.UUCP> Sender: netnews@sei.cmu.edu Reply-To: firth@bd.sei.cmu.edu (Robert Firth) Organization: Carnegie-Mellon University, SEI, Pgh, Pa Lines: 29