comp.lang.ada
 help / color / mirror / Atom feed
From: ncohen@watson.ibm.com (Norman H. Cohen)
Subject: Re: Flags in Ada?
Date: 26 Oct 1994 17:43:16 GMT
Date: 1994-10-26T17:43:16+00:00	[thread overview]
Message-ID: <38m4fk$1gt2@watnews1.watson.ibm.com> (raw)
In-Reply-To: 38khst$hn3@news.delphi.com

In article <38jb6s$tsr@watnews1.watson.ibm.com>, ncohen@watson.ibm.com
(Norman H. Cohen) writes: 

|>    type Permission_Type is
|>       (Owner_Read, Owner_Write, Owner_Execute,
|>        Group_Read, Group_Write, Group_Execute,
|>        World_Read, World_Write, World_Execute);
|>
|>       -- These are the "bit names"
|>
|>    type Permission_Set_Type is array (Permission) of Boolean;
|>    pragma Pack (Permission_Set_Type);
|>
|>       -- This is the "word"

Oops.  That should have been

    type Permission_Set_Type is array (Permission_Type) of Boolean;
                                                 -----

Thanks to Peter Hermann for pointing this out.

In article <38khst$hn3@news.delphi.com>, tmoran@bix.com writes: 

|> >  pragma Pack (Permission_Set_Type);
|>    -- and a rep clause to say just where these 9 bits fit? ;)

The ";)" is a subtle hint that this doesn't really make sense, but for
those who might miss it, we ought to be explicit:  Permission_Set_Type is
an array type, not a record type, so one cannot write a record
representation clause for it.

It is not necessary to control where within the word the nine bits fit
unless you are passing Permission_Set_Type values across an interface to
or from hardware or some piece of software outside the program that
expects a specific bit placement.  If you do need to control the
placement of bits, and if you want to preserve the ability to apply
logical operations to the entire word (a requirement of the original
questioner), there are at least two alternatives: 

   1.  Expand Permission_Type to provide a name for every bit in the word
       and then check (by running a test or examining object code)
       whether the compiler implements packed arrays of booleans with the
       lowest index position in the least significant or most significant
       bit of the word.  You may have to reverse the order of the
       enumeration literals in Permission_Type.  This is obviously not
       portable.

   2.  Declare the types as

          type Permission_Type is range 0 .. Word_Size-1;
          type Permission_Set_Type is array (Permission_Type) of Boolean;
          pragma Pack(Permission_Set_Type);

       and determine at run time, perhaps by obtaining from across the
       interface a value Test_Value known to have only the least
       significant bit set, whether the least significant bit is
       component zero or component Word_Size-1 of the array, then define
       symbolic names for the Permission_Type values accordingly: 

           if Test_Value(0) then
              -- low order bits have low indices
              Owner_Read := 8;
              Owner_Write := 7;
              Owner_Execute := 6;
              Group_Read := 5;
              Group_Write := 4;
              Group_Execute := 3;
              World_Read := 2;
              World_Write := 1;
              World_Execute := 0;
           else
              -- low order bits have high indices
              Owner_Read := Permission_Type'Last - 8;
              Owner_Write := Permission_Type'Last - 7;
              Owner_Execute := Permission_Type'Last - 6;
              Group_Read := Permission_Type'Last - 5;
              Group_Write := Permission_Type'Last - 4;
              Group_Execute := Permission_Type'Last - 3;
              World_Read := Permission_Type'Last - 2;
              World_Write := Permission_Type'Last - 1;
              World_Execute := Permission_Type'Last;
           end if;

       Unfortunately, these nine variables are not static expressions, so
       they can't be used to name index positions in the kinds of array
       aggregates we would like to write.  Also, indexed components into
       packed arrays of Boolean values can be a lot slower on some
       architectures when the index value is not static, because of
       shifting instructions that have the number of bits to shift
       hard-wired into the instruction.

The pragma Packed tells the compiler that I want each Boolean value to
occupy only one bit, and that I want componentwise logical operations to
be implemented using the machine's underlying logical word instructions.
In the absence of such a hint, the compiler might choose to allocate a
byte for each array component, which speeds up the setting and extraction
of array components on some architectures, but slows down componentwise
logical operations.

--
Norman H. Cohen    ncohen@watson.ibm.com



  parent reply	other threads:[~1994-10-26 17:43 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-10-25 10:36 Flags in Ada? Andre Spiegel
1994-10-25 10:07 ` David Emery
1994-10-25 16:19 ` Norman H. Cohen
1994-10-26  3:19   ` tmoran
1994-10-26  9:59     ` David Emery
1994-10-26 22:32       ` Robert Dewar
1994-10-27 13:24         ` Norman H. Cohen
1994-10-27 15:15         ` John Volan
1994-10-31  9:29         ` David Emery
1994-10-27 22:34       ` Henry G. Baker
1994-10-26 14:33     ` Robert Dewar
1994-10-26 17:43     ` Norman H. Cohen [this message]
1994-10-26 15:54   ` Andre Spiegel
1994-10-26  0:36 ` Dale Stanbrough
1994-10-26 11:01   ` Robert Dewar
1994-10-27  8:23 ` Henri Altarac
1994-10-27 23:00   ` Robert Dewar
1994-10-31  9:32     ` David Emery
  -- strict thread matches above, loose matches on Subject: below --
1994-10-25 16:22 tmoran
1994-10-27  5:05 tmoran
1994-10-27 13:29 ` Robert Dewar
1994-10-27 17:15 ` Norman H. Cohen
1994-10-28  3:51   ` Robert Dewar
1994-10-27  5:06 tmoran
1994-10-27 13:47 ` Robert Dewar
1994-10-28  2:41   ` Tucker Taft
1994-10-30 13:31     ` Robert Dewar
1994-10-28  3:59 tmoran
1994-10-28 13:43 ` Robert Dewar
1994-10-31 14:19   ` Norman H. Cohen
1994-11-02 14:06     ` Mats Weber
1994-11-03 23:08       ` Robert Dewar
1994-11-03 11:26     ` Robert Dewar
replies disabled

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