comp.lang.ada
 help / color / mirror / Atom feed
From: Oliver Kellogg <okellogg@my-deja.com>
Subject: Re: Type conversion / computing problem
Date: 2000/02/22
Date: 2000-02-22T00:00:00+00:00	[thread overview]
Message-ID: <88um2f$brk$1@nnrp1.deja.com> (raw)
In-Reply-To: wccemabg2j1.fsf@world.std.com

Bob Duff pointed the way; here's a possible implementation
(I happen to have had a similar problem a while ago.)

with Ada.Text_IO;
with Interfaces, System, Unchecked_Conversion;
use Interfaces;

procedure RegTest is

   subtype Byte is Unsigned_8;
   subtype Var_Positive is Positive range 1 .. 8;
   type Byte_Array is array (Var_Positive range <>) of Byte;
   pragma Pack (Byte_Array);
   subtype Two_Bytes is Byte_Array (1 .. 2);
   subtype Four_Bytes is Byte_Array (1 .. 4);
   subtype RegType is Byte_Array (1 .. 8);
   function To_Bytes is new Unchecked_Conversion
            (Unsigned_64, RegType);

   function Maybe_Swap (Bytes : Byte_Array)
                              return Byte_Array is
      Tmp : Byte_Array (1 .. Bytes'Length);
   begin
      if System."=" (System.Default_Bit_Order,
                     System.Low_Order_First) then
         for I in Tmp'Range loop
            Tmp (I) := Bytes (Bytes'Last - I + 1);
         end loop;
      else
         Tmp := Bytes;
      end if;
      return Tmp;
   end Maybe_Swap;

   function Int2Reg (Value : Integer_32) return RegType is
      function To_Unsigned is new Unchecked_Conversion
                                   (Integer_64, Unsigned_64);
      U64 : Unsigned_64;
   begin
      U64 := To_Unsigned (Integer_64 (Value));
      return Maybe_Swap (To_Bytes (U64));
   end Int2Reg;

   function Reg2Int (Value : RegType) return Integer_32 is
      function To_Integer is new Unchecked_Conversion
                                   (Four_Bytes, Integer_32);
   begin
      return To_Integer (Maybe_Swap (Value (5 .. 8)));
      -- Interestingly, we can use fixed indexes here (5..8)
      -- regardless of host endianness.
      -- That is because on a little endian machine not only are
      -- the bytes in reverse sequence, but also the numbering of
      -- bytes is reversed. ("two wrongs make one right")
   end Reg2Int;

   package Byte_IO is new Ada.Text_IO.Modular_IO (Byte);
   RegVar : RegType;
   IntVar : Integer_32;

begin

   RegVar := (1 .. 7 => 0, 8 => 1);   -- one
   IntVar := Reg2Int (RegVar) + 1;    -- two
   RegVar := Int2Reg (IntVar);
   for I in 1 .. 8 loop
      -- output should be (0,0,0,0,0,0,0,2)
      Ada.Text_IO.Put (' ');
      Byte_IO.Put (RegVar (I), Base => 16);
   end loop;
   Ada.Text_IO.New_Line;

   RegVar := (1 .. 8 => 16#FF#);      -- minus one
   IntVar := Reg2Int (RegVar) - 1;    -- minus two
   RegVar := Int2Reg (IntVar);
   for I in 1 .. 8 loop
      -- output should be (FF,FF,FF,FF,FF,FF,FF,FE)
      Ada.Text_IO.Put (' ');
      Byte_IO.Put (RegVar (I), Base => 16);
   end loop;
   Ada.Text_IO.New_Line;

end RegTest;



Sent via Deja.com http://www.deja.com/
Before you buy.




  reply	other threads:[~2000-02-22  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-02-17  0:00 Type conversion / computing problem Andreas
2000-02-17  0:00 ` Robert A Duff
2000-02-22  0:00   ` Oliver Kellogg [this message]
  -- strict thread matches above, loose matches on Subject: below --
2000-02-17  0:00 Andreas
replies disabled

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