From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 20 Apr 92 14:00:38 GMT From: firth@sei.cmu.edu (Robert Firth) Subject: Re: loop indices Message-ID: <43874@as0c.sei.cmu.edu> List-Id: In article <70482@ut-emx.uucp> hasan@emx.utexas.edu (David A. Hasan) writes: > v1 : Vector( p1 .. (p1+size-1) ); > v2 : Vector( p2 .. (p2+size-1) ); > BEGIN > FOR i IN 1..size LOOP > v2(i) := v1(i); -------------------(!) > END LOOP; > END; Well, I tend to be guided by style rather than efficiency, but in this case I think one can have both. The loop I'd write is for I in 0 .. SIZE-1 loop V2(I+V2'FIRST) := V1(I+V1'FIRST); end loop; I might alos revise the declarations, but that's a minor point. Now, any reasonably good compiler should be able to work out that the address expressions in the loop contain invariant subexpressions, and essentially hoist the addition of V'FIRST out of the loop. Even better, many compilers generate an array root address that already designates the first true element (rather than the mythical element zero), so the addition isn't needed at all. Finally, if the compiler can do simple algebra, it can also see that no range check is needed on the array accesses.