comp.lang.ada
 help / color / mirror / Atom feed
From: adam@irvine.com (Adam Beneschan)
Subject: Re: -ve values in enumeration rep clauses.
Date: 1996/06/14
Date: 1996-06-14T00:00:00+00:00	[thread overview]
Message-ID: <4ps65i$v1g@krusty.irvine.com> (raw)
In-Reply-To: 5c3p6DALoKwxEwb0@djcull.demon.co.uk


Darel Cullen <Darel@djcull.demon.co.uk.demon.co.uk> writes:
 >Hi,
 >   Came across an interesting problem at work today.
 >
 >What happens if for example :-
 >
 >type A_Type is 
 >        ( A, B, C );
 >
 >for A_Type use
 >        (       
 >          A => -1,
 >          B => -2,
 >          C => -3
 >        );
 >
 >for A_Type'Size use 8;
 >
 >Now - to be able to extract the -ve values via unchecked_conversion
 >from a byte proves impossible because the use 8 creates an unsigned
 >byte, so whats the best way to represent negative values from a
 >rep-claused enumerated type ?
 >

I'm not following any of this.

First, the first rep clause for A_Type is illegal, because if you have
an enumeration rep clause, the integers have to be in ascending order
(assuming the enumeration literals are listed in order).  The integers
you have here are in descending order.

Second, I don't see how "use 8" creates an unsigned byte.  It simply
allocates 8 bits for each object of type A_Type.  From an Ada
standpoint, you can't call it "signed" or "unsigned", since the values
represented in those 8 bits are enumeration values and not integers.

If what you mean to say is that the compiler is generating
unsigned-byte instructions to deal A_Type, then your compiler probably
has a bug.  Even so, that shouldn't affect things too much if all the
integers in the rep clause are negative.  It would be worse if you
said 

   for A_Type use ( A => -1, B => 0, C => 1 );

and now, when you compare to A_Types, the generated code might
erroneously think A > C if it treates the bytes as unsigned during the
comparison.

I'm not sure how unchecked_conversion would be affected, though.  If
you say

     type Signed_Byte is integer range -128 .. 127;
     for Signed_Byte'size use 8;

     function To_Signed_Byte is new 
         Unchecked_Conversion (A_Type, Signed_Byte);

then when you convert an A_Type to a Signed_Byte, the compiler should
simply be copying the bit pattern, so it doesn't matter whether the
compiler thinks of A_Type as "signed" or "unsigned".  The
Unchecked_Conversion will make it signed.

If you're trying something like this:

     type Signed_Word is integer range -(2**31) .. 2**31-1;
     for Signed_Word'size use 32;

     function To_Signed_Word is new 
         Unchecked_Conversion (A_Type, Signed_Word);

I don't know what results you will get, and I don't recommend doing
this in any case.  I don't like using Unchecked_Conversion when the
two types don't have the same size.

In any case, if you want help with your problem, it would be best to
show us exactly what Ada code you were trying to run and what
unexpected results you got.  Your description doesn't make it clear
just what you were trying to do.

                                -- Adam





      parent reply	other threads:[~1996-06-14  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-06-14  0:00 -ve values in enumeration rep clauses Darel Cullen
1996-06-14  0:00 ` James Rhodes
1996-06-15  0:00   ` Robert Dewar
1996-06-14  0:00 ` Pascal Ledru
1996-06-14  0:00 ` Adam Beneschan [this message]
replies disabled

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