comp.lang.ada
 help / color / mirror / Atom feed
From: garym@telesoft.UUCP (Gary Morris @flash)
Subject: Re: unsigned integers in Ada
Date: 27 Apr 88 07:30:18 GMT	[thread overview]
Message-ID: <8804270839.AA00686@ucsd.edu> (raw)

> From: ncar!noao!mcdsun!mcdchg!clyde!rds%moss.ATT.COM@AMES.ARC.NASA.GOV
> Subject: unsigned integers in Ada
> 
> How does one implement an unsigned integer in Ada and have a maximum
> value equal to 2**(number of bits) in any given implementation?
The LRM requires that predefined integer types be symmetrical around zero
(LRM 3.5.4 (7)).  It is illegal to provide an unsigned integer type in
package standard.  So the largest type provided on a 32 bit machine will
normally be -(2**31)..(2**31)-1.  That means that you can't declare a type
that is range 0..(2**32)-1 unless the compiler supports a standard integer
type large enough to contain that range (and it must be symmetrical around
zero)!  The following declaration would require 33 bit or larger (ie: 64
bit) integer defined in package standard in order for the compiler to accept
it:

	type Unsigned_Long is range 0..(2**32)-1;	-- 64 bit base type
	for  Unsigned_Long'size use 32;			-- packed in 32 bits

The expressions using the Unsigned_Long would have to be evaluated using
64-bit arithmetic for intermediate results.  Due to these restrictions,
unsigned types in Ada are just a form of packing.  The compiler is not
allowed to perform unsigned operations on the type as you would in assembly
making operations on such types less efficient.

It would be interesting to hear why the language designers chose to restrict
types this way.  The following declaration is legal on most compilers
because most support a integer type in package standard that is 32 bits.
The operations are still done using the 32 bit base type that contains the
positive range you want but must also be symmetrical around zero:

	type Unsigned_Word is range 0..(2**16)-1;	-- 32 bit base type
	for  Unsigned_Word'size use 16;			-- packed in 16 bits

>      subtype Pointer is INTEGER range 0..(2**32)-1;
>      for Pointer'SIZE use 32;
> 
> is illegal because it is not a <first named subtype> LRM 13.1 (3)
> WHAT IS A FIRST NAMED SUBTYPE?? And can someone give an example?
You can't change the representation of a subtype unless it is a first-named
subtype since subtypes derive their representation from the base type.
A <first named subtype> is a subtype declared by a type declaration where
the base type is anonymous.  For example:
	type T is range L .. R;		-- example from LRM 3.5.4 (5)

--GaryM
P.S. Thanks go to JPD for explaining this issue.

Gary Morris		UUCP:	ucbvax!ucsd!telesoft!garym
TeleSoft, San Diego		telesoft!garym@ucsd.edu
(619) 457-2700		ARPA:	telesoft!garym@ucsd.ARPA

             reply	other threads:[~1988-04-27  7:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1988-04-27  7:30 Gary Morris @flash [this message]
  -- strict thread matches above, loose matches on Subject: below --
1988-04-20 23:56 unsigned integers in Ada rds
1988-04-26 14:32 ` stt
replies disabled

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