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]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.