comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <Stephen.A.Leake@nasa.gov>
Subject: Re: Endianness independance
Date: 03 Mar 2003 11:05:14 -0500
Date: 2003-03-03T16:19:09+00:00	[thread overview]
Message-ID: <uu1ekigg5.fsf@nasa.gov> (raw)
In-Reply-To: b3qa6q$thh$1@slb0.atl.mindspring.net

"Marin David Condic" <mcondic.auntie.spam@acm.org> writes:

> Well, but if hardware is not standardized, then why would you expect
> languages to be standardized? (With respect to data representations.)

The point is that Ada provides a _standard_, _single_ representation
clause that works for both kinds of hardware:

--  Abstract :
--
--  Define constants to reflect hardware bit and word endianness in
--  record representation clauses. Obviously, this file is highly
--  system-dependent.
--
with System;
package SAL.Endianness is
   pragma Pure;

   --  This is for gnat on a Mongoose processor
   System_Name : constant System.Name := System.SYSTEM_NAME_GNAT;

   type Byte_Order_Type is (Big_Endian, Little_Endian);
   Byte_Order : constant Byte_Order_Type := Big_Endian;

   --  The Mongoose documentation numbers bits with LSB = 0. However,
   --  the byte order is Big_Endian, so the Ada compiler assumes bits
   --  are numbered with MSB = 0, since that allows representation
   --  clauses with fields that cross natural boundaries.

   Bit_Order      : constant := -1; -- 1 or -1
   High_Bit_First : constant :=  1; -- 0 or  1
   Low_Bit_First  : constant :=  0; -- 1 or  0

   --  Use one of these, corresponding to the natural size of the
   --  record you are laying out.
   LSBit_8        : constant :=  7; -- 0 or  7
   LSBit_16       : constant := 15; -- 0 or 15
   LSBit_32       : constant := 31; -- 0 or 31
   LSBit_56       : constant := 55; -- 0 or 55

end SAL.Endianness;

package Foo is
   type B_Field_Control_Type is record
      --  [1] Table 4.2.8
      Spare_1 : Interfaces_More.Unsigned_29;
      Data    : Interfaces_More.Unsigned_3;
   end record;

   for B_Field_Control_Type use record
      Spare_1 at 0 range
         Low_Bit_First  * (LSBit_32 + Bit_Order *  3) + High_Bit_First * (LSBit_32 + Bit_Order * 31) ..
         High_Bit_First * (LSBit_32 + Bit_Order *  3) + Low_Bit_First  * (LSBit_32 + Bit_Order * 31);

      Data at 0 range
         Low_Bit_First  * (LSBit_32 + Bit_Order *  0) + High_Bit_First * (LSBit_32 + Bit_Order *  2) ..
         High_Bit_First * (LSBit_32 + Bit_Order *  0) + Low_Bit_First  * (LSBit_32 + Bit_Order *  2);

   end record;
   for B_Field_Control_Type'Size use 32;
end foo;

In C, you have to use the preprocessor to get this.

In Ada, it would be nice if you could say:

for B_Field_Control_Type'Bit_Order use Little_Bit_First;

But you can't, because the compiler would have to know whether the
data is "naturally" 32, 16, or 8 bit (that's what the "LSBit_32" is
for above).

-- 
-- Stephe



  parent reply	other threads:[~2003-03-03 16:05 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-28 15:11 Endianness independance Lionel.DRAGHI
2003-02-28 16:10 ` Stephen Leake
2003-02-28 18:26 ` Marin David Condic
2003-03-01 10:48   ` Amir Yantimirov
2003-03-01 12:00     ` Simon Wright
2003-03-01 12:53       ` Jeffrey Creem
2003-03-01 17:26         ` Simon Wright
2003-03-01 12:47     ` Marin David Condic
2003-03-02  9:49       ` Amir Yantimirov
2003-03-03 13:29         ` Marin David Condic
2003-03-03 16:05       ` Stephen Leake [this message]
2003-03-03 17:50         ` Marin David Condic
2003-03-04  2:33         ` Jeffrey Carter
2003-03-04 17:50           ` Stephen Leake
2003-03-05  2:15             ` Jeffrey Carter
2003-03-05 17:37               ` Stephen Leake
  -- strict thread matches above, loose matches on Subject: below --
2003-02-28 17:21 Lionel.DRAGHI
2003-02-28 20:37 ` Randy Brukardt
2003-03-03 13:33 Lionel.DRAGHI
2003-03-03 16:11 ` Stephen Leake
2003-03-03 17:52 Lionel.DRAGHI
2003-03-03 20:29 ` Pascal Obry
     [not found] <BB06F6B19AC7D51181D10050DA725A10138C71@eoleclb.clb.tcfr.thales>
2003-03-03 18:38 ` David C. Hoos
2003-03-04 11:34 Lionel.DRAGHI
     [not found] <BB06F6B19AC7D51181D10050DA725A10138C75@eoleclb.clb.tcfr.thales>
2003-03-04 12:46 ` David C. Hoos, Sr.
2003-03-04 16:38   ` John Harbaugh
2003-03-04 21:25   ` Simon Wright
2003-03-05 17:28     ` Warren W. Gay VE3WWG
2003-03-05 20:15       ` Simon Wright
2003-03-05 21:54         ` Warren W. Gay VE3WWG
2003-03-05 17:49 David C. Hoos
2003-03-05 20:16 ` Simon Wright
2003-03-05 21:58   ` Warren W. Gay VE3WWG
replies disabled

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