comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Does Ada support endiannes?
Date: Thu, 15 Dec 2011 13:01:17 +0000
Date: 2011-12-15T13:01:17+00:00	[thread overview]
Message-ID: <m2pqfq7zvm.fsf@pushface.org> (raw)
In-Reply-To: 71bc4e7f-7c23-4180-9a8d-5c880a16d0c2@p16g2000yqd.googlegroups.com

Gerd <GerdM.O@t-online.de> writes:

> The problem space requires integers (signed), so using unsigned is not
> the right option.
>
> Could you please explain: What do you think how the data send from 68k
> to x86 will be converted from one data layout to the other? Will it
> happen "magically" on the wire? I think, conversion must be done
> explicit either on one side or on the other side, and this requires
> some code.
>
> Currently I use the htonl ntohl functions for it, which (as stated
> above) is not a good choice as it limits the allowed values to
> unsigned.

Some code I wrote -- not quite the same problem -- which handles 8-byte
signed quantities and conversion to/from wire format, and works just
fine on i386, x86_64, powerpc, is

   type SNTP_Timestamp is delta 2.0 ** (-32) range -2.0 ** 31 .. 2.0 ** 31;
   for SNTP_Timestamp'Size use 64;

   subtype Timestamp_Slice is Ada.Streams.Stream_Element_Array (1 .. 8);

   function To_Timestamp_Slice
     (T : SNTP_Timestamp) return Timestamp_Slice is
      function Convert
      is new Ada.Unchecked_Conversion (SNTP_Timestamp,
                                       Timestamp_Slice);
      Tmp : constant Timestamp_Slice := Convert (T);
   begin
      if Big_Endian then
         return Tmp;
      else
         return (1 => Tmp (8),
                 2 => Tmp (7),
                 3 => Tmp (6),
                 4 => Tmp (5),
                 5 => Tmp (4),
                 6 => Tmp (3),
                 7 => Tmp (2),
                 8 => Tmp (1));
      end if;
   end To_Timestamp_Slice;


   function To_SNTP_Timestamp (T : Timestamp_Slice) return SNTP_Timestamp is
      function Convert is new Ada.Unchecked_Conversion (Timestamp_Slice,
                                                        SNTP_Timestamp);
   begin
      if Big_Endian then
         return Convert (T);
      else
         return Convert ((1 => T (8),
                          2 => T (7),
                          3 => T (6),
                          4 => T (5),
                          5 => T (4),
                          6 => T (3),
                          7 => T (2),
                          8 => T (1)));
      end if;
   end To_SNTP_Timestamp;

No real possibility of validation of this data type at the conversion
level, of course.

I think the chance of using wierd machines where this doesn't work is
pretty low.

And don't forget you can come a cropper because of compiler changes:
previous to Ada 2005, GNAT's representation of Ada.Calendar.Time used
the Unix epoch, so a conversion based on that seemed a pretty safe bet.



  reply	other threads:[~2011-12-15 13:01 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-12  8:57 Does Ada support endiannes? Gerd
2011-12-12  9:23 ` Niklas Holsti
2011-12-12 11:27 ` Dmitry A. Kazakov
2011-12-12 12:44   ` Gerd
2011-12-12 19:23     ` Jeffrey Carter
2011-12-13 14:25       ` Gerd
2011-12-13 14:19     ` Gautier write-only
2011-12-14 16:16       ` Gerd
2011-12-14 18:16         ` Dmitry A. Kazakov
2011-12-14 20:16         ` Gautier write-only
2011-12-15 11:27           ` Gerd
2011-12-15 13:01             ` Simon Wright [this message]
2011-12-15 13:37             ` Dmitry A. Kazakov
2011-12-15 20:12             ` Jeffrey Carter
2011-12-12 12:46   ` Gerd
2011-12-12 13:22     ` Dmitry A. Kazakov
2011-12-12 17:07       ` Charles H. Sampson
2011-12-12 18:33         ` Dmitry A. Kazakov
2011-12-14  5:19           ` Charles H. Sampson
2011-12-14  8:56             ` Dmitry A. Kazakov
2011-12-14  9:46               ` Simon Wright
2011-12-15  9:14               ` Charles H. Sampson
2011-12-15  9:46                 ` Dmitry A. Kazakov
2011-12-25 21:42                   ` Charles H. Sampson
2011-12-26 10:47                     ` Dmitry A. Kazakov
2011-12-27 10:38                       ` Georg Bauhaus
2011-12-27 12:35                         ` Dmitry A. Kazakov
2012-01-04  4:33                       ` Charles H. Sampson
2012-01-04 11:56                         ` Dmitry A. Kazakov
2011-12-12 13:33     ` Robert A Duff
replies disabled

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