From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: Re: Endianness and Record Specification
Date: Fri, 21 Sep 2012 23:07:56 -0400
Date: 2012-09-21T23:07:56-04:00 [thread overview]
Message-ID: <85d31edab7.fsf@stephe-leake.org> (raw)
In-Reply-To: 6c9fa5ab-588f-4a4b-929a-23f850913ceb@googlegroups.com
awdorrin <awdorrin@gmail.com> writes:
> I think I found the cause of some of my confusion.
> The Gnat compiler treats the record specifications differently
> depending on if the code is compiled as Ada95 or Ada2005...
>
> Under '05 it produces the following output:
> mytest.adb:26:22: info: reverse bit order in machine scalar of length 16
> mytest.adb:26:22: info: little-endian range for component "Res" is 15 .. 15
>
> And the big endian specification is reordered. Under Ada95 this does not happen.
>
> Hmm...
Yes. Ada 2005 finally got bit-endianness right; all you have to do is
specify what the bit numbers in your record spec mean, by giving
'Bit_Order. Then the compiler does the Right Thing.
See http://www.ada-auth.org/ai-files/grab_bag/bitorder.pdf
So these two record specs represent the same bit layout:
type Other_Date_And_Time_Type is record
Years_Since_1980 : Unsigned_7;
Month : Unsigned_4;
Day_Of_Month : Unsigned_5;
Hour : Unsigned_5;
Minute : Unsigned_6;
Seconds : Unsigned_5;
end record;
for Other_Date_And_Time_Type'Bit_Order use System.High_Order_First;
for Other_Date_And_Time_Type use record
Years_Since_1980 at 0 range 0 .. 6;
Month at 0 range 7 .. 10;
Day_Of_Month at 0 range 11 .. 15;
Hour at 2 range 0 .. 4;
Minute at 2 range 5 .. 10;
Seconds at 2 range 11 .. 15;
end record;
type Date_And_Time_Type is record
Years_Since_1980 : Unsigned_7;
Month : Unsigned_4;
Day_Of_Month : Unsigned_5;
Hour : Unsigned_5;
Minute : Unsigned_6;
Seconds : Unsigned_5;
end record;
for Date_And_Time_Type'Bit_Order use System.Low_Order_First;
for Date_And_Time_Type use record
Years_Since_1980 at 0 range 9 .. 15;
Month at 0 range 5 .. 8;
Day_Of_Month at 0 range 0 .. 4;
Hour at 2 range 11 .. 15;
Minute at 2 range 5 .. 10;
Seconds at 2 range 0 .. 4;
end record;
GNAT is a little paranoid about this and outputs all kinds of "helpful"
informational messages. I think there are now ways to turn them all off,
once you are comfortable that the compiler is doing the right thing.
In Ada 95, the best you could expect was that the compiler would
complain that the endianness was "wrong". I used to have a work-around
for Ada 95, that looked something like this:
for Good_Date_And_Time_Type use record
Years_Since_1980 at 0 range
Low_Bit_First * (LSBit_16 + Bit_Order * 9) + High_Bit_First * (LSBit_32 + Bit_Order * 15) ..
High_Bit_First * (LSBit_16 + Bit_Order * 9) + Low_Bit_First * (LSBit_32 + Bit_Order * 15);
end record;
But I have deleted the spec that defines LSBit_16 and LSBit_32; the
depend on the endianness of the target.
--
-- Stephe
next prev parent reply other threads:[~2012-09-22 3:07 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-21 18:16 Endianness and Record Specification awdorrin
2012-09-21 19:21 ` awdorrin
2012-09-22 3:07 ` Stephen Leake [this message]
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
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox