comp.lang.ada
 help / color / mirror / Atom feed
* Optimizing Boundary Checks
@ 2003-06-13 11:49 Preben Randhol
  2003-06-13 12:03 ` Erlo Haugen
                   ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: Preben Randhol @ 2003-06-13 11:49 UTC (permalink / raw)


After reading some posts here lately I have understood that the compiler
can optimise out boundary checks on say arrays if you do this:

   type Index_Range is range 1 .. 10;
   type Items is array (Index_Range) of Natural;

But if you write:

   type Items is array (1 .. 10) of Natural;

is boundary checks then on?

-- 
Preben Randhol                    http://www.pvv.org/~randhol/



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 11:49 Optimizing Boundary Checks Preben Randhol
@ 2003-06-13 12:03 ` Erlo Haugen
  2003-06-13 12:38   ` Vinzent Hoefler
  2003-06-13 12:42   ` Preben Randhol
  2003-06-13 13:34 ` Martin Dowie
  2003-06-13 13:45 ` Dale Stanbrough
  2 siblings, 2 replies; 26+ messages in thread
From: Erlo Haugen @ 2003-06-13 12:03 UTC (permalink / raw)


On Fri, 13 Jun 2003 11:49:03 +0000 (UTC)
Preben Randhol <randhol+abuse@pvv.org> wrote:

> After reading some posts here lately I have understood that the compiler
> can optimise out boundary checks on say arrays if you do this:
> 
>    type Index_Range is range 1 .. 10;
>    type Items is array (Index_Range) of Natural;
> 
> But if you write:
> 
>    type Items is array (1 .. 10) of Natural;
> 
> is boundary checks then on?

Yes, but in this case (principally) every acces to the array has to be checked,
whereas in the former case, the index is guaranteed to be within range.



-- 
Remove the underscores



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 12:03 ` Erlo Haugen
@ 2003-06-13 12:38   ` Vinzent Hoefler
  2003-06-13 12:47     ` Preben Randhol
  2003-06-13 12:42   ` Preben Randhol
  1 sibling, 1 reply; 26+ messages in thread
From: Vinzent Hoefler @ 2003-06-13 12:38 UTC (permalink / raw)


Erlo Haugen wrote:

>On Fri, 13 Jun 2003 11:49:03 +0000 (UTC)
>Preben Randhol <randhol+abuse@pvv.org> wrote:
>
>> After reading some posts here lately I have understood that the compiler
>> can optimise out boundary checks on say arrays if you do this:
>> 
>>    type Index_Range is range 1 .. 10;
>>    type Items is array (Index_Range) of Natural;
>> 
>> But if you write:
>> 
>>    type Items is array (1 .. 10) of Natural;
>> 
>> is boundary checks then on?
>
>Yes, but in this case (principally) every acces to the array has to be checked,
>whereas in the former case, the index is guaranteed to be within range.

Well, the compiler *could* see that the type Index_Range always
satisfies the same constraint as for the array. I don't know for sure,
but I'd think, it does, because somehow it must store the constraints
for any type anyway and I strongly doubt that an optimizer would work
on type names instead of actual numbers describing their constraints.

OTOH, I think, using such unnamed indices is a quite bad idea anyway,
because it breaks the type safety. So simply: Don't do it. :-)


Vinzent.

-- 
Parents strongly cautioned  --  this  posting  is  intended for mature
audiences  over  18.  It  may  contain some material that many parents
would not find suitable for children and may include intense violence,
sexual situations, coarse language and suggestive dialogue.



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 12:03 ` Erlo Haugen
  2003-06-13 12:38   ` Vinzent Hoefler
@ 2003-06-13 12:42   ` Preben Randhol
  2003-06-13 12:48     ` Preben Randhol
  1 sibling, 1 reply; 26+ messages in thread
From: Preben Randhol @ 2003-06-13 12:42 UTC (permalink / raw)


Erlo Haugen wrote:
> On Fri, 13 Jun 2003 11:49:03 +0000 (UTC)
> Preben Randhol <randhol+abuse@pvv.org> wrote:
> 
>> After reading some posts here lately I have understood that the compiler
>> can optimise out boundary checks on say arrays if you do this:
>> 
>>    type Index_Range is range 1 .. 10;
>>    type Items is array (Index_Range) of Natural;
>> 
>> But if you write:
>> 
>>    type Items is array (1 .. 10) of Natural;
>> 
>> is boundary checks then on?
> 
> Yes, but in this case (principally) every acces to the array has to be
> checked, whereas in the former case, the index is guaranteed to be
> within range.

I see. This is because the boundary checks are on the Index_Range type
and thus one do not need them on Items too.

How is it if Index_Range was defined as a subtype of Integer?

-- 
Preben Randhol                    http://www.pvv.org/~randhol/



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 12:38   ` Vinzent Hoefler
@ 2003-06-13 12:47     ` Preben Randhol
  2003-06-13 13:28       ` Vinzent Hoefler
  0 siblings, 1 reply; 26+ messages in thread
From: Preben Randhol @ 2003-06-13 12:47 UTC (permalink / raw)


Vinzent Hoefler wrote:
> 
> Well, the compiler *could* see that the type Index_Range always
> satisfies the same constraint as for the array. I don't know for sure,
> but I'd think, it does, because somehow it must store the constraints
> for any type anyway and I strongly doubt that an optimizer would work
> on type names instead of actual numbers describing their constraints.

For the second example I was thinking that the Index_Range was not
defined at all.

> OTOH, I think, using such unnamed indices is a quite bad idea anyway,
> because it breaks the type safety. So simply: Don't do it. :-)

Sure. :-)

-- 
Preben Randhol                    http://www.pvv.org/~randhol/



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 12:42   ` Preben Randhol
@ 2003-06-13 12:48     ` Preben Randhol
  2003-06-13 13:28       ` Vinzent Hoefler
  2003-06-13 13:33       ` Peter Amey
  0 siblings, 2 replies; 26+ messages in thread
From: Preben Randhol @ 2003-06-13 12:48 UTC (permalink / raw)


Preben Randhol wrote:
> Erlo Haugen wrote:
>> On Fri, 13 Jun 2003 11:49:03 +0000 (UTC)
>> Preben Randhol <randhol+abuse@pvv.org> wrote:
>> 
>>> After reading some posts here lately I have understood that the compiler
>>> can optimise out boundary checks on say arrays if you do this:
>>> 
>>>    type Index_Range is range 1 .. 10;
>>>    type Items is array (Index_Range) of Natural;
>>> 
>>> But if you write:
>>> 
>>>    type Items is array (1 .. 10) of Natural;
>>> 
>>> is boundary checks then on?
>> 
>> Yes, but in this case (principally) every acces to the array has to be
>> checked, whereas in the former case, the index is guaranteed to be
>> within range.
> 
> I see. This is because the boundary checks are on the Index_Range type
> and thus one do not need them on Items too.
> 
> How is it if Index_Range was defined as a subtype of Integer?

Reason for asking is that I get:

t.adb:10:11: warning: value not in range of subtype of "Standard.integer" defined at line 6
t.adb:10:11: warning: "constraint_error" will be raised at run time

if I do:

   type Items is array (1 .. 10) of Natural;
   Boxes : Items;

begin
   
   Boxes(11) := 1;


-- 
Preben Randhol                    http://www.pvv.org/~randhol/



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 12:47     ` Preben Randhol
@ 2003-06-13 13:28       ` Vinzent Hoefler
  0 siblings, 0 replies; 26+ messages in thread
From: Vinzent Hoefler @ 2003-06-13 13:28 UTC (permalink / raw)


Preben Randhol wrote:

>Vinzent Hoefler wrote:
>> 
>> Well, the compiler *could* see that the type Index_Range always
>> satisfies the same constraint as for the array. I don't know for sure,
>> but I'd think, it does, because somehow it must store the constraints
>> for any type anyway and I strongly doubt that an optimizer would work
>> on type names instead of actual numbers describing their constraints.
>
>For the second example I was thinking that the Index_Range was not
>defined at all.

Well, I guess, it's the same, it depends on what you code, I'd say. If
you say

|for i in 1 .. 10 loop
|   foo(i) := ...

and foo's index constraint is 1 .. 10, too, then the
compiler/optimizer *might* detect it doesn't need a range check here.

In cases where the actual value cannot be (easily) determined at
compiler time, the compiler is required to generate a range check.
Say, we call some function that returns an Integer, it is not easily
determinable that the function always returns values in bounds
(obviously, the contract doesn't tell us that here). So the compiler
generates a range check here, of course. Just see my other posting. :)


Vinzent.

-- 
Parents strongly cautioned  --  this  posting  is  intended for mature
audiences  over  18.  It  may  contain some material that many parents
would not find suitable for children and may include intense violence,
sexual situations, coarse language and suggestive dialogue.



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 12:48     ` Preben Randhol
@ 2003-06-13 13:28       ` Vinzent Hoefler
  2003-06-13 13:56         ` Preben Randhol
  2003-06-13 13:33       ` Peter Amey
  1 sibling, 1 reply; 26+ messages in thread
From: Vinzent Hoefler @ 2003-06-13 13:28 UTC (permalink / raw)


Preben Randhol wrote:

>Reason for asking is that I get:
>
>t.adb:10:11: warning: value not in range of subtype of "Standard.integer" defined at line 6
>t.adb:10:11: warning: "constraint_error" will be raised at run time
>
>if I do:
>
>   type Items is array (1 .. 10) of Natural;
>   Boxes : Items;
>
>begin
>   
>   Boxes(11) := 1;

Well, yes. It can be determined at the time the code is compiled,
because

|1 < 11 or 11 > 10

simply results in True here, because everything is constant. Here the
compiler even is allowed to not do anything at all with the array,
instead it is enough to just generate the call to the exception
handler.

But if you'd like to try

|i : Integer;
| ...
|i := 11;
|Boxes (i) := 1;

instead, then I guess, there will be no warning, because at the time
the code is compiled, "i" will *not* be replaced by the constant 11
and the actual value of "i" is probably already out of scope for the
compiler (highly depends on the implementation, I think). Although,
any optimizing stage later probably still only generates a call to the
exception handler if it is smart enough. If it is not, then there must
be a range check here because

|1 <= Integer'First or 10 >= Integer'Last

results in False.

Of course, my understanding of when a compiler generates a range check
might be completely wrong, I am by no means a compiler writer. :)


Vinzent.

-- 
Parents strongly cautioned  --  this  posting  is  intended for mature
audiences  over  18.  It  may  contain some material that many parents
would not find suitable for children and may include intense violence,
sexual situations, coarse language and suggestive dialogue.



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 12:48     ` Preben Randhol
  2003-06-13 13:28       ` Vinzent Hoefler
@ 2003-06-13 13:33       ` Peter Amey
  1 sibling, 0 replies; 26+ messages in thread
From: Peter Amey @ 2003-06-13 13:33 UTC (permalink / raw)


[snip]
>>
>>How is it if Index_Range was defined as a subtype of Integer?
> 
> 
> Reason for asking is that I get:
> 
> t.adb:10:11: warning: value not in range of subtype of "Standard.integer" defined at line 6
> t.adb:10:11: warning: "constraint_error" will be raised at run time
> 
> if I do:
> 
>    type Items is array (1 .. 10) of Natural;
>    Boxes : Items;
> 
> begin
>    
>    Boxes(11) := 1;
> 
> 

Your array declaration declares an index subtype just as if you had done 
so explicitly with

subtype ItemRange is Integer range 1 ..10;
type Items is array (ItemRange) of Natural;

The array index in your "Boxes..." statement must be in the array index 
subtype and Gnat correctly reports that it isn't.  Gnat can't tell you 
the name of the subtype because you didn't give it one!  This is another 
reason why it is best to avoid use of anonymous types.

Peter




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 11:49 Optimizing Boundary Checks Preben Randhol
  2003-06-13 12:03 ` Erlo Haugen
@ 2003-06-13 13:34 ` Martin Dowie
  2003-06-13 13:48   ` Vinzent Hoefler
  2003-06-13 13:45 ` Dale Stanbrough
  2 siblings, 1 reply; 26+ messages in thread
From: Martin Dowie @ 2003-06-13 13:34 UTC (permalink / raw)


"Preben Randhol" <randhol+abuse@pvv.org> wrote in message
news:slrnbejegn.in.randhol+abuse@kiuk0152.chembio.ntnu.no...
> After reading some posts here lately I have understood that the compiler
> can optimise out boundary checks on say arrays if you do this:

Keyword in the last line is 'can'. I've run across compilers that
will do no optimization (i.e. leave checks in) on code such as:

   for I in My_Type loop
      Make_Call(My_Array (I));
   end loop;

but would take them out if written:

   for I in My_Array'Range loop
      Make_Call(My_Array(I));
   end loop;

Not sure if the checks in the first example would have
disappeared if we'd been allowed to switch compiler
optimizations on...

I guess the you just have to get that assembler listing and
check what each compiler you use does!





^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 11:49 Optimizing Boundary Checks Preben Randhol
  2003-06-13 12:03 ` Erlo Haugen
  2003-06-13 13:34 ` Martin Dowie
@ 2003-06-13 13:45 ` Dale Stanbrough
  2 siblings, 0 replies; 26+ messages in thread
From: Dale Stanbrough @ 2003-06-13 13:45 UTC (permalink / raw)


Preben Randhol wrote:

> After reading some posts here lately I have understood that the compiler
> can optimise out boundary checks on say arrays if you do this:
> 
>    type Index_Range is range 1 .. 10;
>    type Items is array (Index_Range) of Natural;
> 
> But if you write:
> 
>    type Items is array (1 .. 10) of Natural;
> 
> is boundary checks then on?

There are no boundary checks for a type declaration of course, so 
the question is

   "in which situations would a check be generated"

Clearly this depends on the compiler, and the code. For example the
code
  
   x : Items;

   for i in x'range loop
      put (x (i));
   end loop;

is quite easy for a compiler to know that no checks are needed.
There are increasingly more difficult levels of analysis for the
compiler depending on the situation.

Dale



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 13:34 ` Martin Dowie
@ 2003-06-13 13:48   ` Vinzent Hoefler
  2003-06-13 14:37     ` Martin Dowie
  0 siblings, 1 reply; 26+ messages in thread
From: Vinzent Hoefler @ 2003-06-13 13:48 UTC (permalink / raw)


Martin Dowie wrote:

>Keyword in the last line is 'can'. I've run across compilers that
>will do no optimization (i.e. leave checks in) on code such as:
>
>   for I in My_Type loop
>      Make_Call(My_Array (I));
>   end loop;
>
>but would take them out if written:
>
>   for I in My_Array'Range loop
>      Make_Call(My_Array(I));
>   end loop;

This I find interesting. (Of course, assuming that My_Type is the
Index type of My_Array).

>Not sure if the checks in the first example would have
>disappeared if we'd been allowed to switch compiler
>optimizations on...

I'd guess so.

>I guess the you just have to get that assembler listing and
>check what each compiler you use does!

Yes. I'd say, the only thing we know is where the compiler is required
to do range checks, not where it is permitted to suppress them.


Vinzent.



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 13:28       ` Vinzent Hoefler
@ 2003-06-13 13:56         ` Preben Randhol
  2003-06-13 14:42           ` Vinzent Hoefler
  0 siblings, 1 reply; 26+ messages in thread
From: Preben Randhol @ 2003-06-13 13:56 UTC (permalink / raw)


Vinzent Hoefler wrote:
> 
> But if you'd like to try
> 
>|i : Integer;
>| ...
>|i := 11;
>|Boxes (i) := 1;
> 
> instead, then I guess, there will be no warning, because at the time
> the code is compiled, "i" will *not* be replaced by the constant 11
> and the actual value of "i" is probably already out of scope for the
> compiler (highly depends on the implementation, I think). Although,
> any optimizing stage later probably still only generates a call to the
> exception handler if it is smart enough. If it is not, then there must
> be a range check here because

So to sum up:

If I had done:

   subtype Index_Range is Integer range 1 .. 10;
   type Items is array (Index_Range) of Natural;

   Boxes : Items;
   Index : Integer := 11;
begin

   Boxes (Index) := 1;

then I get an constraint error at run-time as the compiler will accept
the code. And in this case range checks on the array must be on. If I
change to:

   type Index_Range is range 1 .. 10;

Then the range checks on the array *can* be switched off as the range
checks of Index_Range will do.

Is this right?

I'm asking because there was an post some days ago saying that my
example above could have range checks on the array off IIRC and that
puzzels me.

Preben
-- 
Preben Randhol                    http://www.pvv.org/~randhol/



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 13:48   ` Vinzent Hoefler
@ 2003-06-13 14:37     ` Martin Dowie
  2003-06-13 15:12       ` Vinzent Hoefler
  0 siblings, 1 reply; 26+ messages in thread
From: Martin Dowie @ 2003-06-13 14:37 UTC (permalink / raw)


"Vinzent Hoefler" <ada.rocks@jlfencey.com> wrote in message
news:bcckq8$i67ah$1@ID-175126.news.dfncis.de...
> >   for I in My_Array'Range loop
[snip]
> This I find interesting. (Of course, assuming that My_Type is the
> Index type of My_Array).

Yes it is. I've always tended to use this form anyway, as it
copes with the (unlikely) case where the index type changing or
(even more unlikely case of) the array type changing!





^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 13:56         ` Preben Randhol
@ 2003-06-13 14:42           ` Vinzent Hoefler
  2003-06-13 15:25             ` Preben Randhol
  0 siblings, 1 reply; 26+ messages in thread
From: Vinzent Hoefler @ 2003-06-13 14:42 UTC (permalink / raw)


Preben Randhol wrote:

>So to sum up:
>
>If I had done:
>
>   subtype Index_Range is Integer range 1 .. 10;
>   type Items is array (Index_Range) of Natural;
>
>   Boxes : Items;
>   Index : Integer := 11;
>begin
>
>   Boxes (Index) := 1;
>
>then I get an constraint error at run-time as the compiler will accept
>the code.

Yes.

>And in this case range checks on the array must be on.

Of course.

Well, I think there's a catch: it could be possible that even with
range checks turned off, you *could* get an exception here, for
instance if the optimizer can already boil the code down that far.
Suppressing checks doesn't mean you will never get an exception at
runtime, see the note in RM 11.5(29).

>If I
>change to:
>
>   type Index_Range is range 1 .. 10;
>
>Then the range checks on the array *can* be switched off as the range
>checks of Index_Range will do.
>
>Is this right?

Yes, perfectly.

>I'm asking because there was an post some days ago saying that my
>example above could have range checks on the array off IIRC and that
>puzzels me.

I guess, you are refering to
<slrnbdpdan.87p.randhol+abuse@kiuk0152.chembio.ntnu.no>?

Well, the catch here was, that a subtype declaration actually allows
indexing an array with a "larger" type (like plain integer) than the
actual constraints of the indices really are. If range checking is
required or not then, simply depends on the type you use for indexing
in a particular expression.


Vinzent.

-- 
Parents strongly cautioned  --  this  posting  is  intended for mature
audiences  over  18.  It  may  contain some material that many parents
would not find suitable for children and may include intense violence,
sexual situations, coarse language and suggestive dialogue.



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 14:37     ` Martin Dowie
@ 2003-06-13 15:12       ` Vinzent Hoefler
  2003-06-14  2:01         ` Jeffrey Carter
                           ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: Vinzent Hoefler @ 2003-06-13 15:12 UTC (permalink / raw)


Martin Dowie wrote:

>"Vinzent Hoefler" <ada.rocks@jlfencey.com> wrote in message
>news:bcckq8$i67ah$1@ID-175126.news.dfncis.de...
>> >   for I in My_Array'Range loop
>[snip]
>> This I find interesting. (Of course, assuming that My_Type is the
>> Index type of My_Array).
>
>Yes it is. I've always tended to use this form anyway, as it
>copes with the (unlikely) case where the index type changing or
>(even more unlikely case of) the array type changing!

Yes, and finding this *very* useful, it reminds me, I would like to
have this feature for *every* variable, not just for arrays:

As an example, currently

|x : My_Enum;
|
|for i in x'Range loop ...

doesn't work, you have to write

|for in in My_Enum'Range loop

each time. Is there any convincing explanation why it isn't allowed to
use the actual type of a variable instead of the type itself? I can
imagine situations (especially with subtypes) where it could be handy
to use the variables' type instead of an explicit type specification.


Vinzent.

-- 
Parents strongly cautioned  --  this  posting  is  intended for mature
audiences  over  18.  It  may  contain some material that many parents
would not find suitable for children and may include intense violence,
sexual situations, coarse language and suggestive dialogue.



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 14:42           ` Vinzent Hoefler
@ 2003-06-13 15:25             ` Preben Randhol
  2003-06-13 15:34               ` Vinzent Hoefler
  0 siblings, 1 reply; 26+ messages in thread
From: Preben Randhol @ 2003-06-13 15:25 UTC (permalink / raw)


Vinzent Hoefler wrote:
> 
> I guess, you are refering to
><slrnbdpdan.87p.randhol+abuse@kiuk0152.chembio.ntnu.no>?

No to:

<mailman.1.1054650999.19078.comp.lang.ada@ada.eu.org>

-- 
Preben Randhol                    http://www.pvv.org/~randhol/



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 15:25             ` Preben Randhol
@ 2003-06-13 15:34               ` Vinzent Hoefler
  2003-06-14 10:45                 ` Preben Randhol
  0 siblings, 1 reply; 26+ messages in thread
From: Vinzent Hoefler @ 2003-06-13 15:34 UTC (permalink / raw)


Preben Randhol wrote:

>Vinzent Hoefler wrote:
>> 
>> I guess, you are refering to
>><slrnbdpdan.87p.randhol+abuse@kiuk0152.chembio.ntnu.no>?
>
>No to:
>
><mailman.1.1054650999.19078.comp.lang.ada@ada.eu.org>

Yes, of course, I meant that. The posting I mentioned was your answer
to this. The classical off-by-one error, I'd say. ;-)


Vinzent.



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 15:12       ` Vinzent Hoefler
@ 2003-06-14  2:01         ` Jeffrey Carter
  2003-06-16 11:23           ` Vinzent Hoefler
  2003-06-14  8:34         ` Dmitry A. Kazakov
  2003-06-14 17:20         ` Robert I. Eachus
  2 siblings, 1 reply; 26+ messages in thread
From: Jeffrey Carter @ 2003-06-14  2:01 UTC (permalink / raw)


Vinzent Hoefler wrote [about 'range for array objects]:
> 
> Yes, and finding this *very* useful, it reminds me, I would like to
> have this feature for *every* variable, not just for arrays:
> 
> As an example, currently
> 
> |x : My_Enum;
> |
> |for i in x'Range loop ...
> 
> doesn't work, you have to write
> 
> |for in in My_Enum'Range loop
> 
> each time.

Obviously, this doesn't compile; yet another argument against using all 
lower case for varibles names. Converting it to the probably intended

for I in My_Enum'Range loop

you still don't have to write this. For loops require a discrete 
subtype, and a discrete subtype is a discrete subtype, so you simply write

for I in My_Enum loop

since My_Enum must be a discrete subtype name for My_Enum'range to be 
legal in a for loop. (Remember that the name in a type declaration is a 
subtype name.)

This doesn't address the question of an attribute to obtain the subtype 
of an object, which in some cases could be useful, and could reduce 
errors in some cases when code changes.

-- 
Jeff Carter
"Now go away or I shall taunt you a second time."
Monty Python & the Holy Grail




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 15:12       ` Vinzent Hoefler
  2003-06-14  2:01         ` Jeffrey Carter
@ 2003-06-14  8:34         ` Dmitry A. Kazakov
  2003-06-14 17:20         ` Robert I. Eachus
  2 siblings, 0 replies; 26+ messages in thread
From: Dmitry A. Kazakov @ 2003-06-14  8:34 UTC (permalink / raw)


Vinzent Hoefler wrote:

> As an example, currently
> 
> |x : My_Enum;
> |
> |for i in x'Range loop ...
> 
> doesn't work, you have to write
> 
> |for in in My_Enum'Range loop
> 
> each time. Is there any convincing explanation why it isn't allowed to
> use the actual type of a variable instead of the type itself? I can
> imagine situations (especially with subtypes) where it could be handy
> to use the variables' type instead of an explicit type specification.

Another example where getting the actual type could be useful:

procedure Foo (X, Y : Object'Class) is
begin
   if X in Y'Class then -- Illegal ín Ada
      ...

   [However the same effect could be achieved if Tag were comparable.]

declare
   Z : Y'Class; -- Steals discriminants and the tag
                -- of possibly limited Y

-- 
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 15:34               ` Vinzent Hoefler
@ 2003-06-14 10:45                 ` Preben Randhol
  2003-06-14 14:59                   ` James Rogers
  0 siblings, 1 reply; 26+ messages in thread
From: Preben Randhol @ 2003-06-14 10:45 UTC (permalink / raw)


Vinzent Hoefler wrote:
> Preben Randhol wrote:
> 
>>Vinzent Hoefler wrote:
>>> 
>>> I guess, you are refering to
>>><slrnbdpdan.87p.randhol+abuse@kiuk0152.chembio.ntnu.no>?
>>
>>No to:
>>
>><mailman.1.1054650999.19078.comp.lang.ada@ada.eu.org>
> 
> Yes, of course, I meant that. The posting I mentioned was your answer
> to this. The classical off-by-one error, I'd say. ;-)

But still I'm a bit confused. The original poster was he right or did he
have to use type ... in stead of subtype to get the boundary checks
optimized out?

Sorry to reitterate my qeustions, just want to understand this 100% :-)

-- 
Preben Randhol                    http://www.pvv.org/~randhol/



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-14 10:45                 ` Preben Randhol
@ 2003-06-14 14:59                   ` James Rogers
  0 siblings, 0 replies; 26+ messages in thread
From: James Rogers @ 2003-06-14 14:59 UTC (permalink / raw)


Preben Randhol <randhol+abuse@pvv.org> wrote in 
news:slrnbelv59.sl.randhol+abuse@kiuk0152.chembio.ntnu.no:

> But still I'm a bit confused. The original poster was he right or did he
> have to use type ... in stead of subtype to get the boundary checks
> optimized out?
> 
> Sorry to reitterate my qeustions, just want to understand this 100% :-)
> 

To get the boundary checks optimized out you must use a type.
Note that some compilers still do not optimize out the boundary
checks with a type. They are looking for a specific range notation
rather than inferring the range from the type.

Jim Rogers



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-13 15:12       ` Vinzent Hoefler
  2003-06-14  2:01         ` Jeffrey Carter
  2003-06-14  8:34         ` Dmitry A. Kazakov
@ 2003-06-14 17:20         ` Robert I. Eachus
  2003-06-16 11:23           ` Vinzent Hoefler
  2 siblings, 1 reply; 26+ messages in thread
From: Robert I. Eachus @ 2003-06-14 17:20 UTC (permalink / raw)


Vinzent Hoefler wrote:

> Yes, and finding this *very* useful, it reminds me, I would like to
> have this feature for *every* variable, not just for arrays:
> 
> As an example, currently
> 
> |x : My_Enum;
> |
> |for i in x'Range loop ...
> 
> doesn't work, you have to write
> 
> |for in in My_Enum'Range loop
> 
> each time. Is there any convincing explanation why it isn't allowed to
> use the actual type of a variable instead of the type itself?

No real reason, just no one saw the need for that particular attrbiute. 
  You might want to post a request for the feature in Ada0Y to 
Ada-Comment@ada-auth.org.

 > I can imagine situations (especially with subtypes) where it could be
 > handy to use the variables' type instead of an explicit type
 > specification.

You will need a better justification than that!

But there is one. ;-)  A discrete variable declaration can have an 
explict range in its subtype_indication:

Size: Integer range 0 .. 1000;

There is no way to identify the subtype of Size to use the 'Range 
attribute, but there are times when it would come in handy. If this is 
added to the language, then probably the attributes Object'First and 
Object'Last should also be added for scalar objects.

Of course, there is a real useful idiom which would need another 
language change.  Right now the initial value, if it exists, is part of 
the declaration of an object.  Again, no particular reason for that, but 
RM8.3(14) would need to add another exception to its list.  It sure 
would be nice though, to be able to say:

Size: Integer range
    Some_Expression..Some_Other_Complex_Expression := Size'First;




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-14  2:01         ` Jeffrey Carter
@ 2003-06-16 11:23           ` Vinzent Hoefler
  0 siblings, 0 replies; 26+ messages in thread
From: Vinzent Hoefler @ 2003-06-16 11:23 UTC (permalink / raw)


Jeffrey Carter wrote:

>Vinzent Hoefler wrote [about 'range for array objects]:
>> 
>> Yes, and finding this *very* useful, it reminds me, I would like to
>> have this feature for *every* variable, not just for arrays:
>> 
>> As an example, currently
>> 
>> |x : My_Enum;
>> |
>> |for i in x'Range loop ...
>> 
>> doesn't work, you have to write
>> 
>> |for in in My_Enum'Range loop
>> 
>> each time.
>
>Obviously, this doesn't compile;

What a luck that an Ada compiler is less forgiving when it comes to
typos. :)

>yet another argument against using all 
>lower case for varibles names.

Well, yes, usually I don't.

But the casual i's, j's and k's are pretty standard. :)

>Converting it to the probably intended
>
>for I in My_Enum'Range loop
>
>you still don't have to write this. For loops require a discrete 
>subtype, and a discrete subtype is a discrete subtype, so you simply write
>
>for I in My_Enum loop
>
>since My_Enum must be a discrete subtype name for My_Enum'range to be 
>legal in a for loop.

*whoops* Yes.

>This doesn't address the question of an attribute to obtain the subtype 
>of an object, which in some cases could be useful, and could reduce 
>errors in some cases when code changes.

That's sounds exactly like th thing I thought about.


Vinzent.

-- 
Parents strongly cautioned  --  this  posting  is  intended for mature
audiences  over  18.  It  may  contain some material that many parents
would not find suitable for children and may include intense violence,
sexual situations, coarse language and suggestive dialogue.



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-14 17:20         ` Robert I. Eachus
@ 2003-06-16 11:23           ` Vinzent Hoefler
  2003-06-18 20:58             ` Brian Gaffney
  0 siblings, 1 reply; 26+ messages in thread
From: Vinzent Hoefler @ 2003-06-16 11:23 UTC (permalink / raw)


Robert I. Eachus wrote:

>Vinzent Hoefler wrote:
>
> > Yes, and finding this *very* useful, it reminds me, I would like to
> > have this feature for *every* variable, not just for arrays:
> >
> > As an example, currently
> >
> > |x : My_Enum;
> > |
> > |for i in x'Range loop ...
> >
> > doesn't work, you have to write
> >
> > |for in in My_Enum'Range loop
> >
> > each time. Is there any convincing explanation why it isn't allowed to
> > use the actual type of a variable instead of the type itself?
>
>No real reason, just no one saw the need for that particular attrbiute. 
>  You might want to post a request for the feature in Ada0Y to 
>Ada-Comment@ada-auth.org.

Perhaps I'll do. But then I would come up with a better justification
than just "it comes handy".

> > I can imagine situations (especially with subtypes) where it could be
> > handy to use the variables' type instead of an explicit type
> > specification.
>
>You will need a better justification than that!

Of course.

>But there is one. ;-)  A discrete variable declaration can have an 
>explict range in its subtype_indication:
>
>Size: Integer range 0 .. 1000;

Well, IMO that's bad style anyway.

>Of course, there is a real useful idiom which would need another 
>language change.  Right now the initial value, if it exists, is part of 
>the declaration of an object.  Again, no particular reason for that, but 
>RM8.3(14) would need to add another exception to its list.  It sure 
>would be nice though, to be able to say:
>
>Size: Integer range
>    Some_Expression..Some_Other_Complex_Expression := Size'First;

Well, as I said, anonymous types are bad style. The reason I would
like to have that feature is that sometimes I don't want to iterate
over the array of some Index_Type, instead I only want to initialize a
part of the array from a Sub_Index_Type. Currently I can write that by
specifying the type explicitely, but sometimes I change those types
and corresponding variables, so then the code breaks when I forget to
change the type specification in the loop.
And sometimes I even don't care about the actual variables type name
and just want to iterate over it's defined range whatever this
currently is.


Vinzent.

-- 
Parents strongly cautioned  --  this  posting  is  intended for mature
audiences  over  18.  It  may  contain some material that many parents
would not find suitable for children and may include intense violence,
sexual situations, coarse language and suggestive dialogue.



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: Optimizing Boundary Checks
  2003-06-16 11:23           ` Vinzent Hoefler
@ 2003-06-18 20:58             ` Brian Gaffney
  0 siblings, 0 replies; 26+ messages in thread
From: Brian Gaffney @ 2003-06-18 20:58 UTC (permalink / raw)


Vinzent Hoefler <ada.rocks@jlfencey.com> wrote in message news:<bck9e8$i1s1h$5@ID-175126.news.dfncis.de>...
> Robert I. Eachus wrote:
> 
> >Vinzent Hoefler wrote:
> >
> > > I can imagine situations (especially with subtypes) where it could be
> > > handy to use the variables' type instead of an explicit type
> > > specification.
> >
> >You will need a better justification than that!
> 
> Of course.
> 
> >But there is one. ;-)  A discrete variable declaration can have an 
> >explict range in its subtype indication:
> >
> >Size: Integer range 0 .. 1000;
> 
> Well, IMO that's bad style anyway.
> 
Vinzent,
   It doesn't matter if you like the justification, as long as you get
the feature you want ;-)

   I would think there would be another argument for this, similar to
the argument for using constants.  Just as you don't want hard-coded
values sprinkled through your code because you might need to change
their values, you may not want hard-coded types since you might want
to change the (sub)type of an object.

   I don't think either of these arguments is very strong, but I can't
imagine implementing this would be too difficult (famous last words!).

                     --Brian



^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2003-06-18 20:58 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-13 11:49 Optimizing Boundary Checks Preben Randhol
2003-06-13 12:03 ` Erlo Haugen
2003-06-13 12:38   ` Vinzent Hoefler
2003-06-13 12:47     ` Preben Randhol
2003-06-13 13:28       ` Vinzent Hoefler
2003-06-13 12:42   ` Preben Randhol
2003-06-13 12:48     ` Preben Randhol
2003-06-13 13:28       ` Vinzent Hoefler
2003-06-13 13:56         ` Preben Randhol
2003-06-13 14:42           ` Vinzent Hoefler
2003-06-13 15:25             ` Preben Randhol
2003-06-13 15:34               ` Vinzent Hoefler
2003-06-14 10:45                 ` Preben Randhol
2003-06-14 14:59                   ` James Rogers
2003-06-13 13:33       ` Peter Amey
2003-06-13 13:34 ` Martin Dowie
2003-06-13 13:48   ` Vinzent Hoefler
2003-06-13 14:37     ` Martin Dowie
2003-06-13 15:12       ` Vinzent Hoefler
2003-06-14  2:01         ` Jeffrey Carter
2003-06-16 11:23           ` Vinzent Hoefler
2003-06-14  8:34         ` Dmitry A. Kazakov
2003-06-14 17:20         ` Robert I. Eachus
2003-06-16 11:23           ` Vinzent Hoefler
2003-06-18 20:58             ` Brian Gaffney
2003-06-13 13:45 ` Dale Stanbrough

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox