Re: where exactly c++,c fail and Ada gets thru'



Maciej Sobczak wrote:

Martin Krischik wrote:

The point is - of corse - it that the semantic tree of the Ada
compiler contains the information that X will be in the range of (1 ..
5) and then the optimizer can easily calculate that (1 .. 5) + 3 < 10
and then optimize away the check.

The sematinc tree of the C++ will contain the information that X is in
the range of (-2*32 .. +2*32-1) and that is a pretty useless
information to the optimizer.

Except that the optimizer is not limited to use only this information.
With the two example ranges (1..5 and 1..10) the result of inlining will
be that the following operations - all operating on the single value -
will be placed one after another:

1. compare against 5
2. add 3
3. compare against 10

It doesn't take a rocket scientist to deduce that the third operation
has a dead branch and can be therefore eliminated.

But it is the callers responsibility to check the input parameter before
calling the function. That way range checks need only be performed on input
and type convertions. Remember that Ada defines + as:

function "+" is (L, R: in Integer'Base) return Integer'Base;

Back to my example: If you call F(3) no check is needed. The caller knows
that 3 is in range and the callee knows that X + 3 is in range. Of course:
If the optimizer spots that is a different story.

Now I do wonder if REH managed some template/boolean magic to get the same
result on C++.

Martin
--
mailto://krischik@xxxxxxxxxxxxxxxxxxxxx
Ada programming at: http://ada.krischik.com
.