comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Avoiding constraint checks w/ 'Base
Date: Thu, 8 Dec 2005 21:11:04 -0600
Date: 2005-12-08T21:11:04-06:00	[thread overview]
Message-ID: <kNmdnTnEw5NaaQXeRVn-og@megapath.net> (raw)
In-Reply-To: AL5mf.14882$H84.243@trnddc04

"Anonymous Coward" <anonymous@coward.org> wrote in message
news:AL5mf.14882$H84.243@trnddc04...
> In article <1134065313.469475.267400@g47g2000cwa.googlegroups.com>,
> Matthew Heaney wrote:
> >
> > So for example, if T is:
> >
> >   type T is 1 .. 42;
> >
> > then T'Base is
> >
> >   type T'Base is -42 .. 42;
> >
> > Note that built-in operators go through the base type, and T's "+"
> > op for example is implicitly declared as:
> >
> >   function "+" (L, R : T'Base) return T'Base;
> >
> > There are no constraint checks on T'Base, so for example:
> >
> > declare
> >   O1 : T := T'(1) + T'(2);
> >   O2 : T'Base := T'(1) + T'(2)
> > begin
> >
> > then in the first assignment to O1, there is a constraint check to
> > ensure that the result of 1 + 2 is in the range of T, but in the
> > second assignment to O2, there is no check.
>
> I like the idea of avoiding constraint checks - especially when I know
> the result will be in range.  Even if I can't be sure that the sum of
> the two operands is in range, it would be an advantage to be able to
> handle the situation without throwing an exception.
>
> So I have some questions.  In your example, based on what you've said
> the ARM guarantees about minimal T'Base ranges, why would the
> initialization for O2 go unchecked, knowing that it could just as well
> go out of bounds?  Is the 'Base attribute also code for "trust me, I
> know what I'm doing?"

It's won't. The "+" operation is required to check for overflow, and if the
result isn't in the range of T'Base, that check will fail. OTOH, the
*assignment* into O2 can never fail; if "+" has generated a value, it has to
fit in O2. (Compilers are allowed to store the larger value somewhere rather
than raising an exception -- this allows optimizations -- but it isn't
allowed to store the *wrong* value - unless the checks are Suppressed.)

> I think it's unfortunate that the 'Base does not necessarily use the
> full object size, so we are still forced to declare a new type, like
> "T_Safe" in this example:

Compilers generally don't choose inefficient representations. So, in
practice, 'Base will use the full object size (certainly that is the
intent). But I don't know why the "full object size" matters in the example.
If you were runing on the U2200, a 6-bit integer is supported by the
hardware, and the compiler might have chosen that for the size of your
objects. In that case, the "full object size" isn't enough to hold your
intermediate result anyway. And while worrying about the U2200 is unlikely,
change the "42" in the example to "100", and then the example would work on
ObjectAda, but not on GNAT - because the "full object size" is 32 on OA, and
8 on GNAT.

Moral: to be totally portable, never depend on anything you know about the
hardware -- because it will (or could) change. Correlary: It's always better
to declare an appropriate type, than to depend on tricks with 'Base.

                        Randy.






  reply	other threads:[~2005-12-09  3:11 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-08 15:21 'Base ada_student
2005-12-08 18:08 ` 'Base Matthew Heaney
2005-12-08 18:44   ` 'Base Martin Dowie
2005-12-08 18:49     ` 'Base Martin Dowie
2005-12-08 19:24     ` 'Base Matthew Heaney
2005-12-08 20:27       ` 'Base Martin Dowie
2005-12-08 19:51     ` 'Base Jeffrey R. Carter
2005-12-08 20:07       ` 'Base Matthew Heaney
2005-12-09  2:57         ` 'Base Randy Brukardt
2005-12-09  2:13   ` Avoiding constraint checks w/ 'Base Anonymous Coward
2005-12-09  3:11     ` Randy Brukardt [this message]
2005-12-09 13:11   ` 'Base krischik
2005-12-09 13:52     ` 'Base Matthew Heaney
2005-12-09 20:42       ` 'Base Randy Brukardt
2005-12-08 19:11 ` 'Base Martin Krischik
2005-12-09 20:42   ` 'Base ada_student
2005-12-09 21:39     ` 'Base Pascal Obry
2005-12-10  3:30     ` 'Base Matthew Heaney
2005-12-10 14:50       ` 'Base ada_student
2005-12-10  7:52     ` 'Base Martin Krischik
2005-12-10 12:55       ` 'Base Larry Kilgallen
2005-12-10 13:37         ` 'Base Björn Persson
2005-12-11 11:00           ` 'Base Martin Krischik
2005-12-10 15:01         ` 'Base Robert A Duff
2005-12-11 10:59         ` 'Base Martin Krischik
2005-12-12  9:14       ` 'Base Ole-Hjalmar Kristensen
2005-12-12 19:08         ` 'Base Martin Krischik
2005-12-13 19:24           ` 'Base tmoran
2005-12-13 21:00         ` 'Base Georg Bauhaus
2005-12-14 19:43         ` 'Base Per Sandberg
2005-12-15 20:08           ` 'Base Martin Krischik
2005-12-16 19:19             ` 'Base Jeffrey R. Carter
2005-12-17  7:52               ` 'Base Martin Krischik
replies disabled

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