OSDN Git Service

2011-08-05 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-bytswa.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                    G N A T . B Y T E _ S W A P P I N G                   --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                     Copyright (C) 2006-2010, AdaCore                     --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
17 --                                                                          --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception,   --
20 -- version 3.1, as published by the Free Software Foundation.               --
21 --                                                                          --
22 -- You should have received a copy of the GNU General Public License and    --
23 -- a copy of the GCC Runtime Library Exception along with this program;     --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25 -- <http://www.gnu.org/licenses/>.                                          --
26 --                                                                          --
27 -- GNAT was originally developed  by the GNAT team at  New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 --  This is a general implementation that does not take advantage of
33 --  any machine-specific instructions.
34
35 with Interfaces; use Interfaces;
36 with Ada.Unchecked_Conversion;
37
38 package body GNAT.Byte_Swapping is
39
40    --------------
41    -- Swapped2 --
42    --------------
43
44    function Swapped2 (Input : Item) return Item is
45
46       function As_U16 is new Ada.Unchecked_Conversion
47          (Source => Item, Target => Unsigned_16);
48
49       function As_Item is new Ada.Unchecked_Conversion
50          (Source => Unsigned_16, Target => Item);
51
52       X : constant Unsigned_16 := As_U16 (Input);
53
54    begin
55       return As_Item ((Shift_Left (X, 8)  and 16#FF00#) or
56                       (Shift_Right (X, 8) and 16#00FF#));
57    end Swapped2;
58
59    --------------
60    -- Swapped4 --
61    --------------
62
63    function Swapped4 (Input : Item) return Item is
64
65       function As_U32 is new Ada.Unchecked_Conversion
66          (Source => Item, Target => Unsigned_32);
67
68       function As_Item is new Ada.Unchecked_Conversion
69          (Source => Unsigned_32, Target => Item);
70
71       X : constant Unsigned_32 := As_U32 (Input);
72
73    begin
74       return As_Item ((Shift_Right (X, 24) and 16#0000_00FF#) or
75                       (Shift_Right (X, 8)  and 16#0000_FF00#) or
76                       (Shift_Left (X, 8)   and 16#00FF_0000#) or
77                       (Shift_Left (X, 24)  and 16#FF00_0000#));
78    end Swapped4;
79
80    --------------
81    -- Swapped8 --
82    --------------
83
84    function Swapped8 (Input : Item) return Item is
85
86       function As_U64 is new Ada.Unchecked_Conversion
87          (Source => Item, Target => Unsigned_64);
88
89       function As_Item is new Ada.Unchecked_Conversion
90          (Source => Unsigned_64, Target => Item);
91
92       X : constant Unsigned_64 := As_U64 (Input);
93
94       Low, High : aliased Unsigned_32;
95
96    begin
97       Low := Unsigned_32 (X and 16#0000_0000_FFFF_FFFF#);
98       Swap4 (Low'Address);
99       High := Unsigned_32 (Shift_Right (X, 32));
100       Swap4 (High'Address);
101       return As_Item
102          (Shift_Left (Unsigned_64 (Low), 32) or Unsigned_64 (High));
103    end Swapped8;
104
105    -----------
106    -- Swap2 --
107    -----------
108
109    procedure Swap2 (Location : System.Address) is
110       X : Unsigned_16;
111       for X'Address use Location;
112    begin
113       X := (Shift_Left (X, 8)  and 16#FF00#) or
114            (Shift_Right (X, 8) and 16#00FF#);
115    end Swap2;
116
117    -----------
118    -- Swap4 --
119    -----------
120
121    procedure Swap4 (Location : System.Address) is
122       X : Unsigned_32;
123       for X'Address use Location;
124    begin
125       X := (Shift_Right (X, 24) and 16#0000_00FF#) or
126            (Shift_Right (X, 8)  and 16#0000_FF00#) or
127            (Shift_Left (X, 8)   and 16#00FF_0000#) or
128            (Shift_Left (X, 24)  and 16#FF00_0000#);
129    end Swap4;
130
131    -----------
132    -- Swap8 --
133    -----------
134
135    procedure Swap8 (Location : System.Address) is
136       X : Unsigned_64;
137       for X'Address use Location;
138
139       Low, High : aliased Unsigned_32;
140
141    begin
142       Low := Unsigned_32 (X and 16#0000_0000_FFFF_FFFF#);
143       Swap4 (Low'Address);
144       High := Unsigned_32 (Shift_Right (X, 32));
145       Swap4 (High'Address);
146       X := Shift_Left (Unsigned_64 (Low), 32) or Unsigned_64 (High);
147    end Swap8;
148
149 end GNAT.Byte_Swapping;