From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.7 required=5.0 tests=BAYES_00,INVALID_DATE, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a5aa52a6f866183 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1994-10-26 13:49:12 PST Path: nntp.gmd.de!xlink.net!howland.reston.ans.net!EU.net!uunet!newsgate.watson.ibm.com!watnews.watson.ibm.com!ncohen From: ncohen@watson.ibm.com (Norman H. Cohen) Newsgroups: comp.lang.ada Subject: Re: Flags in Ada? Date: 26 Oct 1994 17:43:16 GMT Organization: IBM T.J. Watson Research Center Distribution: world Message-ID: <38m4fk$1gt2@watnews1.watson.ibm.com> References: <38jb6s$tsr@watnews1.watson.ibm.com> <38khst$hn3@news.delphi.com> Reply-To: ncohen@watson.ibm.com NNTP-Posting-Host: rios8.watson.ibm.com Date: 1994-10-26T17:43:16+00:00 List-Id: 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