comp.lang.ada
 help / color / mirror / Atom feed
From: Stefan.Lucks@uni-weimar.de
Subject: Re: integers of 1, 2, 4 bytes
Date: Sun, 15 Jun 2014 21:26:28 +0200
Date: 2014-06-15T21:26:28+02:00	[thread overview]
Message-ID: <alpine.DEB.2.10.1406152055230.4330@debian> (raw)
In-Reply-To: <c0654eFaetvU1@mid.individual.net>

[-- Attachment #1: Type: TEXT/PLAIN, Size: 2903 bytes --]

On Sun, 15 Jun 2014, hreba wrote:

> My Ada book says that range checks are applied to constrained subtypes 
> (like that one) but not to unconstrained subtypes, but but overflow checks 
> are applied nevertheless. So I came up with
>
>  type Int16 is range -32768 .. 32767;
>  for Int16'Size use 16;
>  type INT is new Int16'Base;

If you *want* to get rid of range checks, there are pragmas to disable 
them. (Not that I would consider this a good idea at all ...). And I am 
not a language lawyer, but I claim that the Ada compiler will perform 
exactly the same range checks for INT, as it does for Int16 (see below).

So please just use Int16 as defined in

type Int16 is range -32768 .. 32767;
for Int16'Size use 16;

and disable range checking when and where you really need aggressively 
optimized codes (e.g., in the innermost loop ...).

So why does Ada perform range checks for INT? The reason is that INT is 
not "unconstrained", but actually in the range INT'First .. INT'Last for 
some INT'First and INT'Last of the compilers choice. INT'First might be 
smaller than Int16'First, and INT'Last might exceed Int16'Last, but there 
are still ranges to be checked. And, given the Size attribute, there 
appears no choice for the compiler anyway: INT'FIRST=Int16'First and 
INT'Last=Int16'Last imply exactly the same range chekcs for INT as for 
Int16.

> as a more efficient definition.

If you have specific performance constraints, you should try to optimize 
specific subprograms or code fragments, e.g., by disabling range checks 
within the innermost loop or so.

> Now it seems the range specification doesn't matter any more and that I 
> could equally write
>
>  type Int16 is range -1 .. 1;
>  for Int16'Size use 16;
>  type INT is new Int16'Base;
>
> Would you really define a 16 bit integer type this way?

Firstly, this makes your program hard to read, so this is poor software 
engineering. Secondly, it does not do what you want it to do -- i.e., it 
does not omit the range checks. And finally, the Ada compiler has a great 
deal of freedom what Int16'Base actually is. While I find it hard to 
imagine that an Ada compiler for any target machine with 2-complement 
arithmetic would not set Int16'Base to the range -2**15 .. +2**15-1, the 
Ada compiler might still surprise you.

> And how about non-negative types? Would the following have the values
> 0 .. 255?

Now for non-negative integers, there are modular types, which actually 
support arithmetic operations without range checking:

   type Byte is mod 2**8;
   for Byte'Size use 8;

(And similar for 2- and 4-byte arithmetic ...)


------  I  love  the  taste  of  Cryptanalysis  in  the morning!  ------
     <http://www.uni-weimar.de/cms/medien/mediensicherheit/home.html>
--Stefan.Lucks (at) uni-weimar.de, Bauhaus-Universität Weimar, Germany--

  parent reply	other threads:[~2014-06-15 19:26 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-15 17:57 integers of 1, 2, 4 bytes hreba
2014-06-15 18:43 ` Dmitry A. Kazakov
2014-06-15 20:52   ` hreba
2014-06-15 19:07 ` Niklas Holsti
2014-06-15 21:25   ` hreba
2014-06-15 22:39     ` Georg Bauhaus
2014-06-16 13:37       ` AdaMagica
2014-06-16  4:46     ` J-P. Rosen
2014-06-15 19:26 ` Stefan.Lucks [this message]
2014-06-15 21:31   ` hreba
2014-06-15 21:55     ` Niklas Holsti
2014-06-15 22:09 ` Jeffrey Carter
2014-06-16 15:38 ` Adam Beneschan
2014-06-16 17:27 ` gautier_niouzes
replies disabled

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