OSDN Git Service

2009-08-17 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-bitops.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                 GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                 --
4 --                                                                          --
5 --                       S Y S T E M . B I T _ O P S                        --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --         Copyright (C) 1996-2009, Free Software Foundation, Inc.          --
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 with System;                 use System;
33 with System.Unsigned_Types;  use System.Unsigned_Types;
34
35 with Ada.Unchecked_Conversion;
36
37 package body System.Bit_Ops is
38
39    subtype Bits_Array is System.Unsigned_Types.Packed_Bytes1 (Positive);
40    --  Dummy array type used to interpret the address values. We use the
41    --  unaligned version always, since this will handle both the aligned and
42    --  unaligned cases, and we always do these operations by bytes anyway.
43    --  Note: we use a ones origin array here so that the computations of the
44    --  length in bytes work correctly (give a non-negative value) for the
45    --  case of zero length bit strings). Note that we never allocate any
46    --  objects of this type (we can't because they would be absurdly big).
47
48    type Bits is access Bits_Array;
49    --  This is the actual type into which address values are converted
50
51    function To_Bits is new Ada.Unchecked_Conversion (Address, Bits);
52
53    LE : constant := Standard'Default_Bit_Order;
54    --  Static constant set to 0 for big-endian, 1 for little-endian
55
56    --  The following is an array of masks used to mask the final byte, either
57    --  at the high end (big-endian case) or the low end (little-endian case).
58
59    Masks : constant array (1 .. 7) of Packed_Byte := (
60      (1 - LE) * 2#1000_0000# + LE * 2#0000_0001#,
61      (1 - LE) * 2#1100_0000# + LE * 2#0000_0011#,
62      (1 - LE) * 2#1110_0000# + LE * 2#0000_0111#,
63      (1 - LE) * 2#1111_0000# + LE * 2#0000_1111#,
64      (1 - LE) * 2#1111_1000# + LE * 2#0001_1111#,
65      (1 - LE) * 2#1111_1100# + LE * 2#0011_1111#,
66      (1 - LE) * 2#1111_1110# + LE * 2#0111_1111#);
67
68    -----------------------
69    -- Local Subprograms --
70    -----------------------
71
72    procedure Raise_Error;
73    --  Raise Constraint_Error, complaining about unequal lengths
74
75    -------------
76    -- Bit_And --
77    -------------
78
79    procedure Bit_And
80      (Left   : Address;
81       Llen   : Natural;
82       Right  : Address;
83       Rlen   : Natural;
84       Result : Address)
85    is
86       LeftB   : constant Bits := To_Bits (Left);
87       RightB  : constant Bits := To_Bits (Right);
88       ResultB : constant Bits := To_Bits (Result);
89
90    begin
91       if Llen /= Rlen then
92          Raise_Error;
93       end if;
94
95       for J in 1 .. (Rlen + 7) / 8 loop
96          ResultB (J) := LeftB (J) and RightB (J);
97       end loop;
98    end Bit_And;
99
100    ------------
101    -- Bit_Eq --
102    ------------
103
104    function Bit_Eq
105      (Left  : Address;
106       Llen  : Natural;
107       Right : Address;
108       Rlen  : Natural) return Boolean
109    is
110       LeftB  : constant Bits := To_Bits (Left);
111       RightB : constant Bits := To_Bits (Right);
112
113    begin
114       if Llen /= Rlen then
115          return False;
116
117       else
118          declare
119             BLen : constant Natural := Llen / 8;
120             Bitc : constant Natural := Llen mod 8;
121
122          begin
123             if LeftB (1 .. BLen) /= RightB (1 .. BLen) then
124                return False;
125
126             elsif Bitc /= 0 then
127                return
128                  ((LeftB (BLen + 1) xor RightB (BLen + 1))
129                    and Masks (Bitc)) = 0;
130
131             else -- Bitc = 0
132                return True;
133             end if;
134          end;
135       end if;
136    end Bit_Eq;
137
138    -------------
139    -- Bit_Not --
140    -------------
141
142    procedure Bit_Not
143      (Opnd   : System.Address;
144       Len    : Natural;
145       Result : System.Address)
146    is
147       OpndB   : constant Bits := To_Bits (Opnd);
148       ResultB : constant Bits := To_Bits (Result);
149
150    begin
151       for J in 1 .. (Len + 7) / 8 loop
152          ResultB (J) := not OpndB (J);
153       end loop;
154    end Bit_Not;
155
156    ------------
157    -- Bit_Or --
158    ------------
159
160    procedure Bit_Or
161      (Left   : Address;
162       Llen   : Natural;
163       Right  : Address;
164       Rlen   : Natural;
165       Result : Address)
166    is
167       LeftB   : constant Bits := To_Bits (Left);
168       RightB  : constant Bits := To_Bits (Right);
169       ResultB : constant Bits := To_Bits (Result);
170
171    begin
172       if Llen /= Rlen then
173          Raise_Error;
174       end if;
175
176       for J in 1 .. (Rlen + 7) / 8 loop
177          ResultB (J) := LeftB (J) or RightB (J);
178       end loop;
179    end Bit_Or;
180
181    -------------
182    -- Bit_Xor --
183    -------------
184
185    procedure Bit_Xor
186      (Left   : Address;
187       Llen   : Natural;
188       Right  : Address;
189       Rlen   : Natural;
190       Result : Address)
191    is
192       LeftB   : constant Bits := To_Bits (Left);
193       RightB  : constant Bits := To_Bits (Right);
194       ResultB : constant Bits := To_Bits (Result);
195
196    begin
197       if Llen /= Rlen then
198          Raise_Error;
199       end if;
200
201       for J in 1 .. (Rlen + 7) / 8 loop
202          ResultB (J) := LeftB (J) xor RightB (J);
203       end loop;
204    end Bit_Xor;
205
206    -----------------
207    -- Raise_Error --
208    -----------------
209
210    procedure Raise_Error is
211    begin
212       raise Constraint_Error;
213    end Raise_Error;
214
215 end System.Bit_Ops;