1 |
706 |
jeremybenn |
------------------------------------------------------------------------------
|
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-2012, 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 uses GCC intrinsics to take
|
33 |
|
|
-- advantage of any machine-specific instructions.
|
34 |
|
|
|
35 |
|
|
with Ada.Unchecked_Conversion; use Ada;
|
36 |
|
|
|
37 |
|
|
package body GNAT.Byte_Swapping is
|
38 |
|
|
|
39 |
|
|
type U16 is mod 2**16;
|
40 |
|
|
type U32 is mod 2**32;
|
41 |
|
|
type U64 is mod 2**64;
|
42 |
|
|
|
43 |
|
|
function Bswap_16 (X : U16) return U16 is (X / 256 or X * 256);
|
44 |
|
|
-- The above is an idiom recognized by GCC
|
45 |
|
|
|
46 |
|
|
function Bswap_32 (X : U32) return U32;
|
47 |
|
|
pragma Import (Intrinsic, Bswap_32, "__builtin_bswap32");
|
48 |
|
|
|
49 |
|
|
function Bswap_64 (X : U64) return U64;
|
50 |
|
|
pragma Import (Intrinsic, Bswap_64, "__builtin_bswap64");
|
51 |
|
|
|
52 |
|
|
--------------
|
53 |
|
|
-- Swapped2 --
|
54 |
|
|
--------------
|
55 |
|
|
|
56 |
|
|
function Swapped2 (Input : Item) return Item is
|
57 |
|
|
function As_U16 is new Unchecked_Conversion (Item, U16);
|
58 |
|
|
function As_Item is new Unchecked_Conversion (U16, Item);
|
59 |
|
|
|
60 |
|
|
function Bswap_16 (X : U16) return U16 is (X / 256 or X * 256);
|
61 |
|
|
-- ??? Need to have function local here to allow inlining
|
62 |
|
|
pragma Compile_Time_Error (Item'Max_Size_In_Storage_Elements /= 2,
|
63 |
|
|
"storage size must be 2 bytes");
|
64 |
|
|
begin
|
65 |
|
|
return As_Item (Bswap_16 (As_U16 (Input)));
|
66 |
|
|
end Swapped2;
|
67 |
|
|
|
68 |
|
|
--------------
|
69 |
|
|
-- Swapped4 --
|
70 |
|
|
--------------
|
71 |
|
|
|
72 |
|
|
function Swapped4 (Input : Item) return Item is
|
73 |
|
|
function As_U32 is new Unchecked_Conversion (Item, U32);
|
74 |
|
|
function As_Item is new Unchecked_Conversion (U32, Item);
|
75 |
|
|
pragma Compile_Time_Error (Item'Max_Size_In_Storage_Elements /= 4,
|
76 |
|
|
"storage size must be 4 bytes");
|
77 |
|
|
begin
|
78 |
|
|
return As_Item (Bswap_32 (As_U32 (Input)));
|
79 |
|
|
end Swapped4;
|
80 |
|
|
|
81 |
|
|
--------------
|
82 |
|
|
-- Swapped8 --
|
83 |
|
|
--------------
|
84 |
|
|
|
85 |
|
|
function Swapped8 (Input : Item) return Item is
|
86 |
|
|
function As_U64 is new Unchecked_Conversion (Item, U64);
|
87 |
|
|
function As_Item is new Unchecked_Conversion (U64, Item);
|
88 |
|
|
pragma Compile_Time_Error (Item'Max_Size_In_Storage_Elements /= 8,
|
89 |
|
|
"storage size must be 8 bytes");
|
90 |
|
|
begin
|
91 |
|
|
return As_Item (Bswap_64 (As_U64 (Input)));
|
92 |
|
|
end Swapped8;
|
93 |
|
|
|
94 |
|
|
-----------
|
95 |
|
|
-- Swap2 --
|
96 |
|
|
-----------
|
97 |
|
|
|
98 |
|
|
procedure Swap2 (Location : System.Address) is
|
99 |
|
|
X : U16;
|
100 |
|
|
for X'Address use Location;
|
101 |
|
|
begin
|
102 |
|
|
X := Bswap_16 (X);
|
103 |
|
|
end Swap2;
|
104 |
|
|
|
105 |
|
|
-----------
|
106 |
|
|
-- Swap4 --
|
107 |
|
|
-----------
|
108 |
|
|
|
109 |
|
|
procedure Swap4 (Location : System.Address) is
|
110 |
|
|
X : U32;
|
111 |
|
|
for X'Address use Location;
|
112 |
|
|
begin
|
113 |
|
|
X := Bswap_32 (X);
|
114 |
|
|
end Swap4;
|
115 |
|
|
|
116 |
|
|
-----------
|
117 |
|
|
-- Swap8 --
|
118 |
|
|
-----------
|
119 |
|
|
|
120 |
|
|
procedure Swap8 (Location : System.Address) is
|
121 |
|
|
X : U64;
|
122 |
|
|
for X'Address use Location;
|
123 |
|
|
begin
|
124 |
|
|
X := Bswap_64 (X);
|
125 |
|
|
end Swap8;
|
126 |
|
|
end GNAT.Byte_Swapping;
|