Re: eliminating array bounds checking overhead

"Fred J. Scipione" <fjscipio@rochester.rr.com>
27 Apr 2000 10:54:40 -0400

          From comp.compilers

Related articles
[3 earlier articles]
Re: eliminating array bounds checking overhead jprice@scdt.intel.com (2000-04-27)
Re: eliminating array bounds checking overhead blaak@infomatch.com (Ray Blaak) (2000-04-27)
Re: eliminating array bounds checking overhead Sid-Ahmed-Ali.TOUATI@inria.fr (Sid Ahmed Ali TOUATI) (2000-04-27)
Re: eliminating array bounds checking overhead rhyde@shoe-size.com (Randall Hyde) (2000-04-27)
Re: eliminating array bounds checking overhead nr@labrador.eecs.harvard.edu (2000-04-27)
Re: eliminating array bounds checking overhead terryg@uswest.net (Terry Greyzck) (2000-04-27)
Re: eliminating array bounds checking overhead fjscipio@rochester.rr.com (Fred J. Scipione) (2000-04-27)
Re: eliminating array bounds checking overhead sandeep@ddi.com (Sandeep Dutta) (2000-04-29)
Re: eliminating array bounds checking overhead tej@melbpc.org.au (Tim Josling) (2000-04-30)
Re: eliminating array bounds checking overhead markw65@my-deja.com (Mark Williams) (2000-04-30)
Re: eliminating array bounds checking overhead d95josef@dtek.chalmers.se (2000-04-30)
Re: eliminating array bounds checking overhead mayur_naik@my-deja.com (2000-04-30)
Re: eliminating array bounds checking overhead terryg@uswest.net (Terry Greyzck) (2000-05-01)
[7 later articles]
| List of all articles for this month |

From: "Fred J. Scipione" <fjscipio@rochester.rr.com>
Newsgroups: comp.compilers
Date: 27 Apr 2000 10:54:40 -0400
Organization: Time Warner Road Runner - Rochester NY
References: 00-04-194
Keywords: optimize

<mayur_naik@my-deja.com> wrote in message news:00-04-194@comp.compilers...
> I wish to perform an array lookup of the form:
>
> if (i > lo && i < hi)
> printf("%d", A[i]);
> else
> printf("index out of bounds");
>
> The above code is executed several times. The probability that 'i' is
> between 'lo' and 'hi' is VERY high and I want to eliminate the
> overhead of the 2 less-than tests. Does any language or any machine
.... <snip> ...


The following thought, though evading your subsequent question about
hardware support, may be pertainant.


It is possible to direct the compiler to avoid a second branch
instruction by rewriting the test as follows:


  if ((unsigned)(i - (lo+1)) < hi)


Of course, if "lo" is not a constant, the savings is likely to be very
marginal (in-line flow for an addition and subtraction vs a comparison
and a conditional branch which usually flows in-line). It is probably
not reasonable to expect a compiler to always make this transformation
during optimization. However, the generated output for an "array
bounds checking" operation probably should produce the equivalent
code, espically when "lo" and "hi" are known constants.
[It's still pretty fast if you can put lo+1 ad hi in registers. -John]





Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.