comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Endianness independance
Date: 05 Mar 2003 20:15:27 +0000
Date: 2003-03-05T20:15:27+00:00	[thread overview]
Message-ID: <x7vllztfu3k.fsf@smaug.pushface.org> (raw)
In-Reply-To: 3E6633C7.8000503@cogeco.ca

"Warren W. Gay VE3WWG" <ve3wwg@cogeco.ca> writes:

> If you look carefully at GLADE, you will notice that all that is
> being done there is "byte swapping". There is more to independent
> network formats than that (it just happens to work with _most_
> modern platforms at the moment).

I don't know what version of GLADE you are looking at, but the code
for 3.15p does a lot more than this. I am no expert as to whether it
achieves the purpose or not on eg a VAX (but I don't believe that GNAT
supports VAXes anyway).

This is reading a single-length Float from the net:

   function I_F (Stream : access RST) return Float is
      I       : constant Precision := Single;
      E_Size  : Integer  renames Fields (I).E_Size;
      E_Bias  : Integer  renames Fields (I).E_Bias;
      E_Last  : Integer  renames Fields (I).E_Last;
      F_Size  : Integer  renames Fields (I).F_Size;
      F_Mask  : SE       renames Fields (I).F_Mask;
      E_Bytes : SEO      renames Fields (I).E_Bytes;
      F_Bytes : SEO      renames Fields (I).F_Bytes;

      E : Unsigned;
      P : Boolean;
      X : Float;
      S : SEA (1 .. F_L);
      L : SEO;

   begin
      Ada.Streams.Read (Stream.all, S, L);

      if L /= S'Last then
         raise Data_Error;
      end if;

      --  Extract Fraction, Exponent and Sign.
      X := Float (S (F_L + 1 - F_Bytes) and F_Mask);
      for N in F_L + 2 - F_Bytes .. F_L loop
         X := X * FB + Float (S (N));
      end loop;
      X := Scaling (X, -F_Size); --  Float

      if BS <= S (1) then
         P := False;
         E := Unsigned (S (1) - BS);
      else
         P := True;
         E := Unsigned (S (1));
      end if;

      for N in 2 .. E_Bytes loop
         E := E * BB + Unsigned (S (N));
      end loop;
      E := Shift_Right (E, Integer (E_Bytes) * SU - E_Size - 1);

      --  Look for special cases.
      if X = 0.0 then

         --  Signed zeros.
         if E = 0 then
            if P then
               return Float'Copy_Sign (0.0, 1.0);
            else
               return Float'Copy_Sign (0.0, -1.0);
            end if;

         else

            --  Signed infinites.
            if E = Unsigned (E_Last) then
               if P then
                  return Float'Safe_Last;
               else
                  return Float'Safe_First;
               end if;
            end if;
         end if;
      end if;

      --  Denormalized float.
      if E = 0 then

         X := Scaling (X, 1 - E_Bias); --  Flaot

      --  Normalized float.
      else

         X := Scaling (X + 1.0, Integer (E) - E_Bias); --  Float

      end if;

      if P then
         X := Float'Copy_Sign (X, 1.0);
      else
         X := Float'Copy_Sign (X, -1.0);
      end if;

      return X;
   end I_F;



  reply	other threads:[~2003-03-05 20:15 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <BB06F6B19AC7D51181D10050DA725A10138C75@eoleclb.clb.tcfr.thales>
2003-03-04 12:46 ` Endianness independance 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 [this message]
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
  -- strict thread matches above, loose matches on Subject: below --
2003-03-04 11:34 Lionel.DRAGHI
     [not found] <BB06F6B19AC7D51181D10050DA725A10138C71@eoleclb.clb.tcfr.thales>
2003-03-03 18:38 ` David C. Hoos
2003-03-03 17:52 Lionel.DRAGHI
2003-03-03 20:29 ` Pascal Obry
2003-03-03 13:33 Lionel.DRAGHI
2003-03-03 16:11 ` Stephen Leake
2003-02-28 17:21 Lionel.DRAGHI
2003-02-28 20:37 ` Randy Brukardt
2003-02-28 15:11 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
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
replies disabled

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