comp.lang.ada
 help / color / mirror / Atom feed
* Endianness and Record Specification
@ 2012-09-21 18:16 awdorrin
  2012-09-21 19:21 ` awdorrin
                   ` (3 more replies)
  0 siblings, 4 replies; 19+ messages in thread
From: awdorrin @ 2012-09-21 18:16 UTC (permalink / raw)


I have been reading over google searches all day and I feel like I'm missing something here.

Is there a straight forward method to use Standard'Default_Bit_Order to define a specification for a record, that will work on either a Little Endian or Big Endian system?

For instance, I have a 16-bit field that is comprised of 5 bit-fields:
Reserved:     1 bit
Error Flag:   1 bit
Word Count:   6 bits
Bus Id:       1 bit
Message Type: 7 bits

So, 0x0401 is [0000 0100 0000 0001] or [0][0][000100][0][0000001]
Word Count = 4, Message Type = 1

On a Big Endian system the record is defined as:

type TW_REC is record
  Message   : Integer;
  BusId     : Integer;
  WordCount : Integer;
  Error     : Integer;
  Reserved  : Integer;
end record
for TW_REC use record
  Reserved  at 0 range 0..0;
  Error     at 0 range 1..1;
  WordCount at 0 range 2..7;
  BusId     at 1 range 0..0;
  Message   at 1 range 1..7;
end record;

Now, I know I can rewrite this on a Little Endian system to:
for TW_REC'Bit_Order use System.High_Order_First;
for TW_REC use record
  Reserved  at 1 range 0..0;
  Error     at 1 range 1..1;
  WordCount at 1 range 2..7;
  BusId     at 0 range 0..0;
  Message   at 0 range 1..7;
end record;

Taking advantage of the High_Order_First flag to keep the bit ordering the same,
but is there a way to either calculate the byte position, or 'conditionally compile' the right record specification, depending on the system on which it is being compiled?

At first I was thinking I could do:

if Standard'Default_Bit_Order = 1 then
  -- defined Little endian rec spec
else
  -- define Bit endian rec spec
end if;

But, the compiler doesn't let me do that... :-)

I would think I should be able to somehow use the Default_Bit_Order to define some sort of relative byte position, depending on the system endianness, but I'm drawing a blank and hoping someone might have a quick example they could provide.

Thanks!
-Al






^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2012-12-05  3:40 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-21 18:16 Endianness and Record Specification awdorrin
2012-09-21 19:21 ` awdorrin
2012-09-22  3:07   ` Stephen Leake
2012-09-21 22:18 ` Simon Wright
2012-09-22  7:43 ` Quentin Ochem
2012-10-23 21:08   ` awdorrin
2012-10-24 10:20     ` Stephen Leake
2012-11-02 13:13       ` awdorrin
2012-12-04 17:17         ` Anh Vo
2012-12-04 17:37           ` Niklas Holsti
2012-12-04 18:31             ` Anh Vo
2012-12-04 23:31               ` Randy Brukardt
2012-12-05  0:12                 ` Anh Vo
2012-12-05  2:00                   ` Jeffrey Carter
2012-12-05  3:40                     ` Anh Vo
2012-09-22  9:32 ` Robin Vowels
2012-09-22  9:59   ` Dmitry A. Kazakov
2012-09-24  2:44     ` Robin Vowels
2012-09-24  7:48       ` Simon Wright

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