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 machine-specific version of this package.
35 -- It uses instructions available on Intel 486 processors (or later).
37 with Interfaces
; use Interfaces
;
38 with System
.Machine_Code
; use System
.Machine_Code
;
39 with Ada
.Unchecked_Conversion
;
41 package body GNAT
.Byte_Swapping
is
43 -----------------------
44 -- Local Subprograms --
45 -----------------------
47 function Swapped32
(Value
: Unsigned_32
) return Unsigned_32
;
48 pragma Inline_Always
(Swapped32
);
54 function Swapped2
(Input
: Item
) return Item
is
56 function As_U16
is new Ada
.Unchecked_Conversion
57 (Source
=> Item
, Target
=> Unsigned_16
);
59 function As_Item
is new Ada
.Unchecked_Conversion
60 (Source
=> Unsigned_16
, Target
=> Item
);
62 X
: Unsigned_16
:= As_U16
(Input
);
66 Unsigned_16
'Asm_Output ("=q", X
),
67 Unsigned_16
'Asm_Input ("0", X
));
75 function Swapped4
(Input
: Item
) return Item
is
77 function As_U32
is new Ada
.Unchecked_Conversion
78 (Source
=> Item
, Target
=> Unsigned_32
);
80 function As_Item
is new Ada
.Unchecked_Conversion
81 (Source
=> Unsigned_32
, Target
=> Item
);
83 X
: Unsigned_32
:= As_U32
(Input
);
87 Unsigned_32
'Asm_Output ("=r", X
),
88 Unsigned_32
'Asm_Input ("0", X
));
96 function Swapped8
(Input
: Item
) return Item
is
98 function As_U64
is new Ada
.Unchecked_Conversion
99 (Source
=> Item
, Target
=> Unsigned_64
);
101 X
: constant Unsigned_64
:= As_U64
(Input
);
103 type Two_Words
is array (0 .. 1) of Unsigned_32
;
104 for Two_Words
'Component_Size use Unsigned_32
'Size;
106 function As_Item
is new Ada
.Unchecked_Conversion
107 (Source
=> Two_Words
, Target
=> Item
);
114 (Unsigned_32
'Asm_Output ("=r", Result
(0)),
115 Unsigned_32
'Asm_Output ("=r", Result
(1))),
117 (Unsigned_32
'Asm_Input ("0",
118 Swapped32
(Unsigned_32
(X
and 16#
0000_0000_FFFF_FFFF#
))),
119 Unsigned_32
'Asm_Input ("1",
120 Swapped32
(Unsigned_32
(Shift_Right
(X
, 32))))));
121 return As_Item
(Result
);
128 procedure Swap2
(Location
: System
.Address
) is
131 for X
'Address use Location
;
134 Asm
("xchgb %b0,%h0",
135 Unsigned_16
'Asm_Output ("=q", X
),
136 Unsigned_16
'Asm_Input ("0", X
));
143 procedure Swap4
(Location
: System
.Address
) is
146 for X
'Address use Location
;
150 Unsigned_32
'Asm_Output ("=r", X
),
151 Unsigned_32
'Asm_Input ("0", X
));
158 function Swapped32
(Value
: Unsigned_32
) return Unsigned_32
is
159 X
: Unsigned_32
:= Value
;
162 Unsigned_32
'Asm_Output ("=r", X
),
163 Unsigned_32
'Asm_Input ("0", X
));
171 procedure Swap8
(Location
: System
.Address
) is
174 for X
'Address use Location
;
176 type Two_Words
is array (0 .. 1) of Unsigned_32
;
177 for Two_Words
'Component_Size use Unsigned_32
'Size;
180 for Words
'Address use Location
;
185 (Unsigned_32
'Asm_Output ("=r", Words
(0)),
186 Unsigned_32
'Asm_Output ("=r", Words
(1))),
188 (Unsigned_32
'Asm_Input ("0",
189 Swapped32
(Unsigned_32
(X
and 16#
0000_0000_FFFF_FFFF#
))),
190 Unsigned_32
'Asm_Input ("1",
191 Swapped32
(Unsigned_32
(Shift_Right
(X
, 32))))));
194 end GNAT
.Byte_Swapping
;