From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,effb80d4bb7716dd X-Google-Attributes: gid103376,public From: adam@irvine.com Subject: Re: Pointer Arithmetic (was: Wanted: Ada STL....) Date: 1999/02/03 Message-ID: <798d2s$cce$1@nnrp1.dejanews.com>#1/1 X-Deja-AN: 440007167 References: <790f4q$3l@bgtnsc01.worldnet.att.net> <797na3$obg$1@nnrp1.dejanews.com> <36B78CDD.1372@lanl.gov> X-Http-Proxy: 1.0 x2.dejanews.com:80 (Squid/1.1.22) for client 192.160.8.21 Organization: Deja News - The Leader in Internet Discussion X-Article-Creation-Date: Wed Feb 03 02:42:37 1999 GMT Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/3.0 (X11; I; Linux 2.0.34 i686) Date: 1999-02-03T00:00:00+00:00 List-Id: In article <36B78CDD.1372@lanl.gov>, William Clodius wrote: > For most machines in use the most efficient implementations of arrays is > as a single contiguous block that is accessed by mapping indices > directly to the equivalent pointer operations. (The exceptions are more > easilly mapped to arrays and not to pointers.) It is true that mapping > involving an index other than the most rapidly varying one can be > thought of as involving a multiplication, but the same is true of the > equivalent pointer arithmetic. In both cases the same functionality can > be obtained by adding a (n easilly determined) finite integer to the > current memory index. The analysis for the compiler to do this is so > simple, I would not be surprized if Ada Lovelace discussed it for > Babage's machine in the 1800s. Any compiler that does not perform that > analysis is broken. > > It is sometimes thought that because it is relatively easy for the > programmer to move some of the pointer arithmetic outside the loop, that > pointer arithmetic is more efficient, but the algorithms necessary to > perform that motion by the compiler have been available since the first > Fortran compiler, ca. 1957. Any compiler that does not perform that > analysis is a toy and not a professional system. It is more likely that > a programmer will not perform the necessary analysis for the pointers, > than that a compiler will not perform the analysis. I'm not quite following everything you say. I agree that if you're using the index variable inside a FOR loop, it should be easy for the compiler to optimize the index operations so that no multiplication is used, e.g. for I in Arr'range loop Do_Something_With (Arr (I)); end loop; But what if the operation of stepping through the array isn't so neatly packaged? Note that while I was talking about stepping through an array, I never said anything about this stepping taking place inside a single loop. It's certainly conceivable (if you have a package that declares an iterator, for example) that the index manipulation might not even be handled within the same subprogram call. You might have to provide a routine that steps to the next element of the array, then saves the index in a global variable or an object's "work area" before the subprogram exits, and then use the saved index the next time the subprogram is called. It seems to me that it would be a lot harder for a compiler to perform the analysis required to determine that the index should be implemented with pointer arithmetic. Granted, when a program starts getting this complex, it's hard for me to come up with a real-life example where the few nanoseconds saved by avoiding multiplications would really make a difference. -- Adam -----------== Posted via Deja News, The Discussion Network ==---------- http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own