comp.lang.ada
 help / color / mirror / Atom feed
From: awdorrin <awdorrin@gmail.com>
Subject: Re: GNAT 4.4.5 Record Bit_Order Endian issues
Date: Fri, 18 Jan 2013 07:11:07 -0800 (PST)
Date: 2013-01-18T07:11:07-08:00	[thread overview]
Message-ID: <61a24758-b289-40b0-9ee2-ad7feddff5c6@googlegroups.com> (raw)
In-Reply-To: <8a7a9e6f-6f87-44a7-942b-2275a9ecf049@googlegroups.com>

So, I think I figured out how to solve my problem.

Within the legacy code, the representation clause was used to define the memory locations, then the record was being copied to/from a buffer via an imported 'memcpy' call. It worked since the system was Big Endian, but obviously it is non-portable to a Little Endian system.

What I realize now is, if I define the record using 'Bit_Order = System.High_Order_First; and then define the record elements in terms of a chosen machine scalar type (ie. 32-bit)

Now, the record specification will define 'equivalent' record structures, when endian swapping is taken into account.

The following code is probably not the most elegant way to solve this (and I'm open to suggestions) but here it is:

if Standard'Default_Bit_Order = BIG_ENDIAN then
  -- legacy big endian byte copy
  MEMCPY( Target => Track_Address, Source => Track_Rec'Address, Size => Track_Rec'Size/8);
else
  -- little endian convert to big endian by 32-bit word
  declare
    type EndCvtArray is array(UInt32 range <> ) of UInt32;
    WrdCnt : constant UInt32 := Track_Rec'Size/32;
    Target : EndCvtArray(1..WrdCnt);
    for Target'Address use Track_Address; -- overlay
    Source : EndCvtArray(1..WrdCnt);
    for Source'Address use Track_Rec'Address; -- overlay
  begin
    for i in 1..WrdCnt loop
      Target(i) := SwapUInt32(Source(i));
    end loop;
  end;
end if;

function SwapUIn32 is new GNAT.Byte_Swapping.Swapped4(UInt32);

I'm guessing there might be some cleaner way to do this, perhaps using Stream IO. But I tried the code and it appears to work.

Once I understood that the representation clause wasn't defining the memory locations, this made a lot more sense.




  reply	other threads:[~2013-01-18 15:11 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-14 17:43 GNAT 4.4.5 Record Bit_Order Endian issues awdorrin
2013-01-15  0:38 ` Randy Brukardt
2013-01-15  1:57 ` Adam Beneschan
2013-01-15 16:57 ` AdaMagica
2013-01-15 22:24 ` Stephen Leake
2013-01-16 10:44   ` Simon Wright
2013-01-16 19:00     ` AdaMagica
2013-01-16 21:34       ` Simon Wright
2013-01-16 23:14     ` Randy Brukardt
2013-01-17  3:49     ` Stephen Leake
2013-01-17 15:32       ` awdorrin
2013-01-18  9:49         ` Stephen Leake
2013-01-18 13:04           ` Robert A Duff
2013-01-19  1:43             ` Stephen Leake
2013-01-19 12:48               ` AdaMagica
2013-01-22  0:14                 ` Randy Brukardt
2013-01-17 17:28       ` Simon Wright
2013-01-18  9:56         ` Stephen Leake
2013-01-17 18:04 ` awdorrin
2013-01-17 19:50   ` awdorrin
2013-01-18  9:58     ` Stephen Leake
2013-01-17 20:58   ` Simon Wright
2013-01-17 21:29     ` awdorrin
2013-01-17 22:16       ` awdorrin
2013-01-18  6:15         ` J-P. Rosen
2013-01-18 15:28           ` Niklas Holsti
2013-01-18  9:37         ` Stephen Leake
2013-01-18 12:24         ` awdorrin
2013-01-18 15:11           ` awdorrin [this message]
2013-01-19  1:48             ` Stephen Leake
2013-01-18 17:19           ` Simon Wright
2013-01-22  9:49           ` quinot
2013-01-28 13:39             ` quinot
  -- strict thread matches above, loose matches on Subject: below --
2013-01-22  3:21 Stephen Leake
2013-01-22  5:14 ` Jeffrey Carter
2013-01-23  1:29   ` Stephen Leake
2013-01-22 22:40 ` Randy Brukardt
2013-01-23  1:38   ` Stephen Leake
2013-01-23 10:58     ` Simon Wright
replies disabled

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