1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . B Y T E _ S W A P P I N G --
9 -- Copyright (C) 2006-2007, AdaCore --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- This is a general implementation that does not take advantage of
35 -- any machine-specific instructions.
37 with Interfaces
; use Interfaces
;
38 with Ada
.Unchecked_Conversion
;
40 package body GNAT
.Byte_Swapping
is
46 function Swapped2
(Input
: Item
) return Item
is
48 function As_U16
is new Ada
.Unchecked_Conversion
49 (Source
=> Item
, Target
=> Unsigned_16
);
51 function As_Item
is new Ada
.Unchecked_Conversion
52 (Source
=> Unsigned_16
, Target
=> Item
);
54 X
: constant Unsigned_16
:= As_U16
(Input
);
57 return As_Item
((Shift_Left
(X
, 8) and 16#FF00#
) or
58 (Shift_Right
(X
, 8) and 16#
00FF#
));
65 function Swapped4
(Input
: Item
) return Item
is
67 function As_U32
is new Ada
.Unchecked_Conversion
68 (Source
=> Item
, Target
=> Unsigned_32
);
70 function As_Item
is new Ada
.Unchecked_Conversion
71 (Source
=> Unsigned_32
, Target
=> Item
);
73 X
: constant Unsigned_32
:= As_U32
(Input
);
76 return As_Item
((Shift_Right
(X
, 24) and 16#
0000_00FF#
) or
77 (Shift_Right
(X
, 8) and 16#
0000_FF00#
) or
78 (Shift_Left
(X
, 8) and 16#
00FF_0000#
) or
79 (Shift_Left
(X
, 24) and 16#FF00_0000#
));
86 function Swapped8
(Input
: Item
) return Item
is
88 function As_U64
is new Ada
.Unchecked_Conversion
89 (Source
=> Item
, Target
=> Unsigned_64
);
91 function As_Item
is new Ada
.Unchecked_Conversion
92 (Source
=> Unsigned_64
, Target
=> Item
);
94 X
: constant Unsigned_64
:= As_U64
(Input
);
96 Low
, High
: aliased Unsigned_32
;
99 Low
:= Unsigned_32
(X
and 16#
0000_0000_FFFF_FFFF#
);
101 High
:= Unsigned_32
(Shift_Right
(X
, 32));
102 Swap4
(High
'Address);
104 (Shift_Left
(Unsigned_64
(Low
), 32) or Unsigned_64
(High
));
111 procedure Swap2
(Location
: System
.Address
) is
113 for X
'Address use Location
;
115 X
:= (Shift_Left
(X
, 8) and 16#FF00#
) or
116 (Shift_Right
(X
, 8) and 16#
00FF#
);
123 procedure Swap4
(Location
: System
.Address
) is
125 for X
'Address use Location
;
127 X
:= (Shift_Right
(X
, 24) and 16#
0000_00FF#
) or
128 (Shift_Right
(X
, 8) and 16#
0000_FF00#
) or
129 (Shift_Left
(X
, 8) and 16#
00FF_0000#
) or
130 (Shift_Left
(X
, 24) and 16#FF00_0000#
);
137 procedure Swap8
(Location
: System
.Address
) is
139 for X
'Address use Location
;
141 Low
, High
: aliased Unsigned_32
;
144 Low
:= Unsigned_32
(X
and 16#
0000_0000_FFFF_FFFF#
);
146 High
:= Unsigned_32
(Shift_Right
(X
, 32));
147 Swap4
(High
'Address);
148 X
:= Shift_Left
(Unsigned_64
(Low
), 32) or Unsigned_64
(High
);
151 end GNAT
.Byte_Swapping
;