Path: utzoo!attcan!uunet!convex!killer!ames!mailrus!bbn!rochester!pt.cs.cmu.edu!andrew.cmu.edu!jk3k+ From: jk3k+@andrew.cmu.edu (Joe Keane) Newsgroups: comp.arch Subject: Re: A simple question on RISC Message-ID: Date: 18 Nov 88 22:23:30 GMT References: <6544@xanth.cs.odu.edu> <75577@sun.uucp> <1618@imagine.PAWL.RPI.EDU> <419@augean.OZ> <392@ksr.UUCP>, <7723@aw.sei.cmu.edu> Organization: Carnegie Mellon Lines: 41 In-Reply-To: <7723@aw.sei.cmu.edu> Robert Firth writes: > The only language that defines a FOR loop that is always executed at least > once is Fortran, and it requires the sign of the step to be known at compile > time. C's do-while is uncommon, but they haven't removed it yet. > For all other languages, you have to hand code a compare at the top of the > loop anyway, so it's pretty pointless to code it again at the bottom. The naive way to code `for (i = lower; i < upper; i += step) foo;' is: mov lower,i test cmp i,upper blt end foo add step,i bra test end > (Well, actually you put the compare at the bottom and have an initial branch > to it, but you see what I mean.) Good idea; let's try it: mov lower,i bra entry body foo add step,i entry cmp i,upper bge body Statically, same number of instructions; dynamically, less except in the zero-trip case. But look, the add, compare, and branch are consecutive. Suppose we want to eliminate the unconditional branch. We can duplicate the test at the top. Or the compiler may figure out that the initial test can't fail (mostly if the loop has constant bounds). In either of these cases, the add, compare, and branch are still consecutive. --Joe